KMToolbarView.swift 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. //
  2. // KMToolbarView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/10/24.
  6. //
  7. import Cocoa
  8. private let KMToolbarSpaceItemWidth = 30.0
  9. private let KMToolbarLineItemWidth = 1.0
  10. private let KMToolbarItemSpace = 8.0
  11. @objc protocol KMToolbarViewDelegate: NSObjectProtocol {
  12. @objc optional func toolbar(_ toolbar: KMToolbarView, itemFor identifier: String) -> KMToolbarItemView
  13. @objc optional func toolbarAllowedItemIdentifiers(_ toolbar: KMToolbarView) -> [String]
  14. @objc optional func toolbarDefaultItemIdentifiers(_ toolbar: KMToolbarView) -> [String]
  15. @objc optional func toolbarLeftDefaultItemIdentifiers(_ toolbar: KMToolbarView) -> [String]
  16. @objc optional func toolbarRightDefaultItemIdentifiers(_ toolbar: KMToolbarView) -> [String]
  17. }
  18. @objcMembers class KMToolbarView: NSView {
  19. private var _toolbarIdentifier: String?
  20. var toolbarIdentifier: String? {
  21. get {
  22. return self._toolbarIdentifier
  23. }
  24. }
  25. private var _items: [KMToolbarItemView] = []
  26. var items: [KMToolbarItemView] {
  27. get {
  28. return self._items
  29. }
  30. }
  31. weak var delegate: KMToolbarViewDelegate?
  32. private var _visibleItems: [KMToolbarItemView] = []
  33. private var _invisibleItems: [KMToolbarItemView] = []
  34. private var _contentView: NSView?
  35. var contentView: NSView {
  36. get {
  37. if (self._contentView == nil) {
  38. self._contentView = NSView()
  39. }
  40. return self._contentView!
  41. }
  42. }
  43. lazy var moreButton: NSButton = {
  44. let _moreButton = NSButton()
  45. _moreButton.bezelStyle = .regularSquare
  46. _moreButton.isBordered = false
  47. _moreButton.font = .systemFont(ofSize: 20)
  48. _moreButton.title = " »"
  49. _moreButton.isHidden = true
  50. _moreButton.target = self
  51. _moreButton.action = #selector(_moreButtonAction)
  52. _moreButton.wantsLayer = true
  53. _moreButton.layer?.backgroundColor = NSColor.clear.cgColor
  54. return _moreButton
  55. }()
  56. private lazy var centerView: NSView = {
  57. let view = NSView()
  58. return view
  59. }()
  60. private lazy var leftView: NSView = {
  61. let view = NSView()
  62. // view.wantsLayer = true
  63. // view.layer?.backgroundColor = NSColor(red: 247/255.0, green: 248/255.0, blue: 250/255.0 , alpha: 1).cgColor
  64. return view
  65. }()
  66. private class KMToolbarView_rightView: NSView {}
  67. private lazy var rightView: NSView = {
  68. let view = KMToolbarView_rightView()
  69. // view.wantsLayer = true
  70. // view.layer?.backgroundColor = NSColor(red: 247/255.0, green: 248/255.0, blue: 250/255.0, alpha: 1).cgColor
  71. return view
  72. }()
  73. static let kLeftMargin: CGFloat = 8
  74. static let kHSpace: CGFloat = 8
  75. static let kRightMargin: CGFloat = 8
  76. static let kMoreWidth: CGFloat = 30
  77. private var _leftWidth: CGFloat = 0
  78. private var _centerWidth: CGFloat = 0
  79. private var _rightWidth: CGFloat = 0
  80. convenience init(identifier: String) {
  81. self.init()
  82. self._toolbarIdentifier = identifier
  83. self._addTrackingArea()
  84. NotificationCenter.default.addObserver(self, selector: #selector(toolbarCustomChangeNotification), name: KMToolbarCustomChangeNotification, object: nil)
  85. // self.leftView.wantsLayer = true
  86. // self.contentView.wantsLayer = true
  87. // self.rightView.wantsLayer = true
  88. // self.leftView.layer?.backgroundColor = NSColor.red.cgColor
  89. // self.contentView.layer?.backgroundColor = NSColor.blue.cgColor
  90. // self.rightView.layer?.backgroundColor = NSColor.green.cgColor
  91. }
  92. deinit {
  93. Swift.debugPrint("KMToolbarView deinit")
  94. NotificationCenter.default.removeObserver(self)
  95. }
  96. // MARK: - Private Methods
  97. private func _addTrackingArea() {
  98. let trackingArea = NSTrackingArea(rect: self.bounds, options: [.inVisibleRect, .activeInKeyWindow, .mouseMoved], owner: self)
  99. self.addTrackingArea(trackingArea)
  100. }
  101. override func mouseMoved(with event: NSEvent) {
  102. // self.window?.mouseMoved(with: event)
  103. super.mouseMoved(with: event)
  104. }
  105. @objc private func _moreButtonAction() {
  106. let menu = NSMenu()
  107. for item in self._invisibleItems {
  108. if (item.itemIdentifier == KMToolbarDividerItemIdentifier) {
  109. menu.addItem(NSMenuItem.separator())
  110. } else if (item.menuFormRepresentation != nil) {
  111. menu.addItem(item.menuFormRepresentation!)
  112. }
  113. }
  114. menu.popUp(positioning: menu.items.first, at: NSMakePoint(CGRectGetMaxX(self.moreButton.frame), CGRectGetMaxY(self.moreButton.frame)-5), in: self)
  115. }
  116. private func _updateMainView(_ left: Bool = true, _ right: Bool = true) {
  117. if (self.centerView.superview == nil) {
  118. self.addSubview(self.centerView)
  119. }
  120. if left {
  121. self._updateLeftView()
  122. }
  123. if right {
  124. self._updateRightView()
  125. }
  126. // self.centerView.mas_remakeConstraints { make in
  127. // make?.right.equalTo()(0)
  128. // make?.top.equalTo()(self.mas_top)?.offset()(0)
  129. // make?.bottom.equalTo()(self.mas_bottom)?.offset()(0)
  130. // make?.left.mas_equalTo()(0)
  131. // }
  132. if(self.contentView.superview == nil) {
  133. self.centerView.addSubview(self.contentView)
  134. }
  135. // self.contentView.mas_remakeConstraints { make in
  136. // make?.centerX.equalTo()(0)
  137. // make?.centerY.equalTo()(0)
  138. // }
  139. for item in self.items {
  140. item.removeFromSuperview()
  141. }
  142. self._items = []
  143. var posX = 0.0
  144. var items: [KMToolbarItemView] = []
  145. var itemIdentifiers = self.delegate?.toolbarDefaultItemIdentifiers?(self) ?? []
  146. var lastItem: KMToolbarItemView?
  147. var iWidth: CGFloat = 0
  148. for i in 0 ..< (itemIdentifiers.count-self._invisibleItems.count) {
  149. let itemIdentifier = itemIdentifiers[i]
  150. var item: KMToolbarItemView?
  151. if itemIdentifier == KMToolbarDividerItemIdentifier {
  152. item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  153. } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  154. item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  155. item?.layer?.backgroundColor = .clear
  156. item?.imageViewBox.borderColor = .clear
  157. item?.layer?.cornerRadius = 0.0
  158. } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) {
  159. item = data
  160. }
  161. guard let data = item else {
  162. continue
  163. }
  164. self.contentView.addSubview(data)
  165. // if (lastItem != nil) {
  166. // if (i == itemIdentifiers.count - 1) {
  167. // item?.mas_makeConstraints({ make in
  168. // make?.top.bottom().equalTo()(0)
  169. // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace)
  170. // make?.right.equalTo()(self.contentView.mas_right)?.offset()(0)
  171. // })
  172. // } else {
  173. // item?.mas_makeConstraints({ make in
  174. // make?.top.bottom().equalTo()(0)
  175. // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace)
  176. // })
  177. // }
  178. //
  179. // } else {
  180. // if (item?.image != nil) {
  181. // item?.mas_makeConstraints({ make in
  182. // make?.top.bottom().equalTo()(0)
  183. // make?.left.equalTo()(0)
  184. // })
  185. // } else {
  186. // item?.mas_makeConstraints({ make in
  187. // make?.top.bottom().equalTo()(0)
  188. // make?.left.equalTo()(0)
  189. // make?.height.offset()(40)
  190. // })
  191. // }
  192. // }
  193. items.append(data)
  194. iWidth += data.itemWidth
  195. if lastItem != nil {
  196. // + 前一个的间距
  197. iWidth += Self.kHSpace
  198. }
  199. lastItem = item
  200. // posX += item!.frame.size.width
  201. }
  202. self._items = items
  203. self._centerWidth = iWidth
  204. self.addSubview(self.moreButton)
  205. }
  206. // UI 20 16
  207. // item w 28 h 24
  208. private func _updateView(_ left: Bool = true, _ right: Bool = true) {
  209. if left {
  210. self._updateLeftView()
  211. }
  212. if right {
  213. self._updateRightView()
  214. }
  215. if(self.contentView.superview == nil) {
  216. self.addSubview(self.contentView)
  217. }
  218. // self.contentView.mas_remakeConstraints { make in
  219. // make?.centerX.equalTo()(0)
  220. // make?.centerY.equalTo()(0)
  221. // }
  222. for item in self.items {
  223. item.removeFromSuperview()
  224. }
  225. self._items = []
  226. var posX = 0.0
  227. var items: [KMToolbarItemView] = []
  228. var itemIdentifiers = self.delegate?.toolbarDefaultItemIdentifiers?(self) ?? []
  229. var lastItem: KMToolbarItemView?
  230. // -self._invisibleItems.count
  231. var iWidth: CGFloat = 0
  232. for i in 0 ..< itemIdentifiers.count {
  233. let itemIdentifier = itemIdentifiers[i]
  234. var item: KMToolbarItemView?
  235. if itemIdentifier == KMToolbarDividerItemIdentifier {
  236. item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  237. } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  238. item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  239. item?.layer?.backgroundColor = .clear
  240. item?.imageViewBox.borderColor = .clear
  241. item?.layer?.cornerRadius = 0.0
  242. } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) {
  243. item = data
  244. }
  245. guard let data = item else {
  246. continue
  247. }
  248. self.contentView.addSubview(data)
  249. // if (lastItem != nil) {
  250. // if (i == itemIdentifiers.count - 1) {
  251. // item?.mas_makeConstraints({ make in
  252. // make?.top.bottom().equalTo()(0)
  253. // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace)
  254. // make?.right.equalTo()(self.contentView.mas_right)?.offset()(0)
  255. // })
  256. // } else {
  257. // if itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  258. // item?.mas_makeConstraints({ make in
  259. // make?.top.bottom().equalTo()(0)
  260. // make?.left.equalTo()(lastItem!.mas_right)?.offset()(2)
  261. // })
  262. // } else {
  263. // if lastItem?.itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  264. // item?.mas_makeConstraints({ make in
  265. // make?.top.bottom().equalTo()(0)
  266. // make?.left.equalTo()(lastItem!.mas_right)?.offset()(2)
  267. // })
  268. // } else {
  269. // item?.mas_makeConstraints({ make in
  270. // make?.top.bottom().equalTo()(0)
  271. // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace)
  272. // })
  273. // }
  274. // }
  275. // }
  276. // } else {
  277. // if (item?.image != nil) {
  278. // item?.mas_makeConstraints({ make in
  279. // make?.top.bottom().equalTo()(0)
  280. // make?.left.equalTo()(0)
  281. // })
  282. // } else {
  283. // item?.mas_makeConstraints({ make in
  284. // make?.top.bottom().equalTo()(0)
  285. // make?.left.equalTo()(0)
  286. // make?.height.offset()(40)
  287. // })
  288. // }
  289. // }
  290. items.append(data)
  291. iWidth += data.itemWidth
  292. if lastItem != nil {
  293. // + 前一个的间距
  294. iWidth += Self.kHSpace
  295. }
  296. lastItem = item
  297. // posX += item!.frame.size.width
  298. }
  299. self._items = items
  300. self._centerWidth = iWidth
  301. self.addSubview(self.moreButton)
  302. }
  303. private func _updateLeftView() {
  304. // 刷新前移除视图
  305. for item in self.leftView.subviews {
  306. item.removeFromSuperview()
  307. }
  308. if (self.leftView.superview != nil) {
  309. self.leftView.removeFromSuperview()
  310. }
  311. var iWidth: CGFloat = 0
  312. if let itemIdentifiers = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self), itemIdentifiers.count > 0 {
  313. if (self.leftView.superview == nil) {
  314. self.addSubview(self.leftView)
  315. }
  316. self.leftView.mas_remakeConstraints { make in
  317. make?.left.equalTo()(self.mas_left)?.offset()(0)
  318. make?.top.equalTo()(self.mas_top)?.offset()(0)
  319. make?.bottom.equalTo()(self.mas_bottom)?.offset()(0)
  320. }
  321. var lastItem: KMToolbarItemView?
  322. for i in 0 ..< itemIdentifiers.count {
  323. let itemIdentifier = itemIdentifiers[i]
  324. var item: KMToolbarItemView?
  325. if itemIdentifier == KMToolbarDividerItemIdentifier {
  326. item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  327. } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  328. item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  329. item?.layer?.backgroundColor = .clear
  330. item?.imageViewBox.borderColor = .clear
  331. item?.layer?.cornerRadius = 0.0
  332. } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) {
  333. item = data
  334. }
  335. guard let data = item else {
  336. continue
  337. }
  338. self.leftView.addSubview(data)
  339. // if (lastItem != nil) {
  340. // if (i == itemIdentifiers.count - 1) {
  341. // item?.mas_makeConstraints({ make in
  342. // make?.height.mas_equalTo()(40)
  343. // make?.centerY.mas_equalTo()(0)
  344. // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace)
  345. // make?.right.equalTo()(self.leftView.mas_right)?.offset()(0)
  346. // })
  347. // } else {
  348. // item?.mas_makeConstraints({ make in
  349. // make?.height.mas_equalTo()(40)
  350. // make?.centerY.mas_equalTo()(0)
  351. // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace)
  352. // })
  353. // }
  354. //
  355. // } else {
  356. // if (i == itemIdentifiers.count - 1) {
  357. // item?.mas_makeConstraints({ make in
  358. // make?.height.mas_equalTo()(40)
  359. // make?.centerY.mas_equalTo()(0)
  360. // make?.left.equalTo()(self.leftView.mas_left)?.offset()(KMToolbarItemSpace)
  361. // make?.right.equalTo()(self.leftView.mas_right)?.offset()(-20)
  362. // })
  363. // } else {
  364. // item?.mas_makeConstraints({ make in
  365. // make?.height.mas_equalTo()(40)
  366. // make?.centerY.mas_equalTo()(0)
  367. // make?.left.equalTo()(KMToolbarItemSpace)
  368. // })
  369. // }
  370. // }
  371. iWidth += data.itemWidth
  372. if lastItem != nil {
  373. // + 前一个的间距
  374. iWidth += Self.kHSpace
  375. }
  376. lastItem = item
  377. }
  378. iWidth += (Self.kLeftMargin + Self.kHSpace)
  379. }
  380. self._leftWidth = iWidth
  381. }
  382. private func _updateRightView() {
  383. // 刷新前移除视图
  384. for item in self.rightView.subviews {
  385. item.removeFromSuperview()
  386. }
  387. if (self.rightView.superview != nil) {
  388. self.rightView.removeFromSuperview()
  389. }
  390. var iWidth: CGFloat = 0
  391. if let itemIdentifiers = self.delegate?.toolbarRightDefaultItemIdentifiers?(self), itemIdentifiers.isEmpty == false {
  392. if (self.rightView.superview == nil) {
  393. self.addSubview(self.rightView)
  394. }
  395. // self.rightView.mas_remakeConstraints { make in
  396. // make?.right.equalTo()(self.mas_right)?.offset()(0)
  397. // make?.top.equalTo()(self.mas_top)?.offset()(0)
  398. // make?.bottom.equalTo()(self.mas_bottom)?.offset()(0)
  399. // }
  400. var lastItem: KMToolbarItemView?
  401. for i in 0 ..< itemIdentifiers.count {
  402. let itemIdentifier = itemIdentifiers[i]
  403. var item: KMToolbarItemView?
  404. if itemIdentifier == KMToolbarDividerItemIdentifier {
  405. item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  406. } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  407. item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  408. item?.layer?.backgroundColor = .clear
  409. item?.imageViewBox.borderColor = .clear
  410. item?.layer?.cornerRadius = 0.0;
  411. } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) {
  412. item = data
  413. }
  414. guard let data = item else {
  415. continue
  416. }
  417. self.rightView.addSubview(data)
  418. // if (lastItem != nil) {
  419. // if (i == itemIdentifiers.count - 1) {
  420. // item?.mas_makeConstraints({ make in
  421. // make?.height.offset()(40)
  422. // make?.centerY.mas_equalTo()(0)
  423. // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace)
  424. // make?.right.equalTo()(self.rightView.mas_right)?.offset()(-KMToolbarItemSpace)
  425. // })
  426. // } else {
  427. // item?.mas_makeConstraints({ make in
  428. // make?.height.offset()(40)
  429. // make?.centerY.mas_equalTo()(0)
  430. // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace)
  431. // })
  432. // }
  433. // } else {
  434. // if (i == itemIdentifiers.count - 1) {
  435. // item?.mas_makeConstraints({ make in
  436. // make?.centerY.mas_equalTo()(0)
  437. // make?.height.offset()(40)
  438. // make?.left.equalTo()(self.rightView.mas_left)?.offset()(KMToolbarItemSpace)
  439. // make?.right.equalTo()(self.rightView.mas_right)?.offset()(-KMToolbarItemSpace)
  440. // })
  441. // } else {
  442. // item?.mas_makeConstraints({ make in
  443. // make?.height.offset()(40)
  444. // make?.centerY.mas_equalTo()(0)
  445. // make?.left.equalTo()(0)
  446. // })
  447. // }
  448. // }
  449. iWidth += data.itemWidth
  450. if lastItem != nil {
  451. // + 前一个的间距
  452. iWidth += Self.kHSpace
  453. }
  454. lastItem = item
  455. }
  456. iWidth += Self.kRightMargin
  457. }
  458. self._rightWidth = iWidth
  459. }
  460. override func updateLayer() {
  461. super.updateLayer()
  462. if KMAppearance.isDarkMode() {
  463. self.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  464. } else {
  465. self.layer?.backgroundColor = .clear
  466. }
  467. for item in self._items {
  468. item.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  469. item.customizeView?.interfaceThemeDidChanged(self.appearance?.name ?? .aqua)
  470. item.nameBtn.setTitleColor(KMAppearance.titleColor())
  471. }
  472. }
  473. // MARK: - View Methods
  474. override func viewWillMove(toWindow newWindow: NSWindow?) {
  475. super.viewWillMove(toWindow: newWindow)
  476. if (newWindow != nil && self._contentView == nil) {
  477. let leftitemIdentifiers = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self) ?? []
  478. if (leftitemIdentifiers.count > 0) {
  479. self._updateMainView()
  480. }else {
  481. self._updateView()
  482. }
  483. self.resizeSubviews(withOldSize: self.frame.size)
  484. // NotificationCenter.default.addObserver(self, selector: #selector(toolbarCustomChangeNotification), name: KMToolbarCustomChangeNotification, object: nil)
  485. let topLine = NSBox(frame: CGRectMake(0, 0, self.frame.size.width, 0.5))
  486. topLine.boxType = .separator
  487. topLine.fillColor = .black
  488. self.addSubview(topLine)
  489. topLine.mas_makeConstraints { make in
  490. make?.left.right().equalTo()(self)
  491. make?.bottom.equalTo()(self.mas_bottom)?.offset()(-0.5)
  492. make?.height.offset()(0.5)
  493. }
  494. let bottomLine = NSBox(frame: CGRectMake(0, 0, self.frame.size.width, 0.5))
  495. bottomLine.boxType = .separator
  496. bottomLine.fillColor = .black
  497. self.addSubview(bottomLine)
  498. bottomLine.mas_makeConstraints { make in
  499. make?.left.right().equalTo()(self)
  500. make?.top.equalTo()(self.mas_top)?.offset()(0.1)
  501. make?.height.offset()(0.5)
  502. }
  503. // DispatchQueue.main.async {
  504. // self._layoutSubViewsIfNeed()
  505. // }
  506. }
  507. if (newWindow != nil) {
  508. self._addTrackingArea()
  509. }
  510. }
  511. private func _right_layoutSubViewIfNeed() {
  512. if self._rightWidth < 0 { // 没有item
  513. return
  514. }
  515. if self.rightView.superview == nil { // 没有item
  516. return
  517. }
  518. if self.rightView.subviews.isEmpty { // 没有item
  519. return
  520. }
  521. let width = NSWidth(self.bounds)
  522. let contentW = self._leftWidth + self._centerWidth + self._rightWidth
  523. if width >= contentW { // 左 1 中 1 右 1
  524. self._updateRightView()
  525. return
  526. }
  527. if width >= self._leftWidth + self._centerWidth { // 左 1 中 1 右 0
  528. let leftMargin = self._leftWidth-self._centerWidth
  529. self.rightView.mas_remakeConstraints { make in
  530. make?.right.mas_equalTo()(-Self.kMoreWidth)
  531. make?.top.bottom().mas_equalTo()(0)
  532. make?.width.mas_equalTo()(width-leftMargin)
  533. }
  534. var itemX: CGFloat = leftMargin
  535. for sv in self.rightView.subviews {
  536. guard let item = sv as? KMToolbarItemView else {
  537. continue
  538. }
  539. let itemMaxX = itemX + item.itemWidth + Self.kHSpace
  540. if itemMaxX > width { // 隐藏
  541. item.isHidden = true
  542. self._invisibleItems.append(item)
  543. } else { // 显示
  544. item.isHidden = false
  545. }
  546. }
  547. return
  548. }
  549. // 右边完全显示不下
  550. self.rightView.mas_remakeConstraints { make in
  551. make?.right.mas_equalTo()(0)
  552. make?.top.bottom().mas_equalTo()(0)
  553. make?.width.mas_equalTo()(0)
  554. }
  555. }
  556. private func _center_layoutSubViewIfNeed() {
  557. if self._centerWidth < 0 { // 没有item
  558. return
  559. }
  560. if self.contentView.superview == nil { // 没有item
  561. return
  562. }
  563. if self.contentView.subviews.isEmpty { // 没有item
  564. return
  565. }
  566. let width = NSWidth(self.bounds)
  567. let contentW = self._leftWidth + self._centerWidth + self._rightWidth
  568. if width >= contentW { // 左 1 中 1 右 1
  569. if self.contentView.superview == self {
  570. self._updateView(false, false)
  571. } else {
  572. self._updateMainView(false, false)
  573. }
  574. return
  575. }
  576. if width >= self._leftWidth + self._centerWidth { // 左 1 中 1 右 0 (中 完全显示)
  577. if self.contentView.superview == self {
  578. self._updateView(false, false)
  579. } else {
  580. self._updateMainView(false, false)
  581. }
  582. return
  583. }
  584. if width >= self._leftWidth { // 左 1 中 1 右 0 (中 不完全显示)
  585. let leftMargin = self._leftWidth
  586. self.contentView.mas_remakeConstraints { make in
  587. make?.right.mas_equalTo()(-Self.kMoreWidth)
  588. make?.top.bottom().mas_equalTo()(0)
  589. make?.width.mas_equalTo()(width-leftMargin)
  590. }
  591. var itemX: CGFloat = leftMargin
  592. for sv in self.contentView.subviews {
  593. guard let item = sv as? KMToolbarItemView else {
  594. continue
  595. }
  596. let itemMaxX = itemX + item.itemWidth + Self.kHSpace
  597. if itemMaxX > width { // 隐藏
  598. item.isHidden = true
  599. self._invisibleItems.append(item)
  600. } else { // 显示
  601. item.isHidden = false
  602. }
  603. }
  604. return
  605. }
  606. // 中间完全显示不下
  607. if self.centerView.superview != nil {
  608. self.centerView.mas_remakeConstraints { make in
  609. make?.center.mas_equalTo()(0)
  610. make?.width.mas_equalTo()(0)
  611. }
  612. }
  613. self.contentView.mas_remakeConstraints { make in
  614. make?.center.mas_equalTo()(0)
  615. make?.width.mas_equalTo()(0)
  616. }
  617. }
  618. private func _layoutSubViewsIfNeed() {
  619. if (self._contentView == nil) {
  620. return
  621. }
  622. let moreButtonWidth = 18.0
  623. // let leftButtonWidth = 10.0
  624. // let leftW = NSWidth(self.leftView.frame)
  625. // let contentW = NSWidth(self.contentView.frame)
  626. // let rightW = NSWidth(self.rightView.frame)
  627. let leftW = self._leftWidth
  628. let contentW = self._centerWidth
  629. let rightW = self._rightWidth
  630. // KMPrint("width: \(leftW), \(contentW), \(rightW)")
  631. // KMPrint("\(self.frame.size.width)")
  632. // self._right_layoutSubViewIfNeed()
  633. // self._center_layoutSubViewIfNeed()
  634. let width = NSWidth(self.bounds)
  635. let height = NSHeight(self.bounds)
  636. let leftMargin = Self.kLeftMargin
  637. let hSpace = Self.kHSpace
  638. let itemH: CGFloat = 40
  639. var itemY: CGFloat = (height-itemH)*0.5
  640. let contentLength = leftW + contentW + rightW
  641. if (contentLength <= 0) {
  642. return
  643. }
  644. self._invisibleItems.removeAll()
  645. if width >= contentLength { // 左 + 中 + 右 显示(全)
  646. self.moreButton.isHidden = true
  647. let offset = width - contentLength
  648. self.leftView.frame = NSMakeRect(0, 0, leftW, height)
  649. var itemX: CGFloat = leftMargin
  650. for sv in self.leftView.subviews {
  651. guard let item = sv as? KMToolbarItemView else {
  652. continue
  653. }
  654. item.isHidden = false
  655. itemY = (height-item.itemHeight)*0.5
  656. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  657. itemX += item.itemWidth + hSpace
  658. }
  659. if self.isMain() {
  660. self.centerView.frame = NSMakeRect(leftW, 0, contentW + offset, height)
  661. self.contentView.frame = NSMakeRect(offset*0.5, 0, contentW, height)
  662. } else {
  663. self.contentView.frame = NSMakeRect(offset*0.5, 0, contentW, height)
  664. }
  665. itemX = 0
  666. for sv in self.contentView.subviews {
  667. guard let item = sv as? KMToolbarItemView else {
  668. continue
  669. }
  670. item.isHidden = false
  671. itemY = (height-item.itemHeight)*0.5
  672. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  673. itemX += item.itemWidth + hSpace
  674. }
  675. self.rightView.frame = NSMakeRect(leftW+contentW+offset, 0, rightW, height)
  676. itemX = 0
  677. for sv in self.rightView.subviews {
  678. guard let item = sv as? KMToolbarItemView else {
  679. continue
  680. }
  681. item.isHidden = false
  682. itemY = (height-item.itemHeight)*0.5
  683. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  684. itemX += item.itemWidth + hSpace
  685. }
  686. return
  687. } else { // 显示更多
  688. self.moreButton.isHidden = false
  689. self.moreButton.mas_remakeConstraints { make in
  690. make?.top.bottom().equalTo()(0)
  691. make?.width.mas_equalTo()(moreButtonWidth)
  692. make?.right.mas_equalTo()(0)
  693. }
  694. if width >= leftW + contentW { // 左 + 中 显示(全) 右显示不全
  695. let offset = width - leftW - contentW
  696. self.leftView.frame = NSMakeRect(0, 0, leftW, height)
  697. var itemX: CGFloat = leftMargin
  698. for sv in self.leftView.subviews {
  699. guard let item = sv as? KMToolbarItemView else {
  700. continue
  701. }
  702. item.isHidden = false
  703. itemY = (height-item.itemHeight)*0.5
  704. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  705. itemX += item.itemWidth + hSpace
  706. }
  707. if self.isMain() {
  708. self.centerView.frame = NSMakeRect(leftW, 0, contentW, height)
  709. self.contentView.frame = NSMakeRect(0, 0, contentW + offset, height)
  710. } else {
  711. self.contentView.frame = NSMakeRect(leftW, 0, contentW, height)
  712. }
  713. itemX = 0
  714. for sv in self.contentView.subviews {
  715. guard let item = sv as? KMToolbarItemView else {
  716. continue
  717. }
  718. item.isHidden = false
  719. itemY = (height-item.itemHeight)*0.5
  720. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  721. itemX += item.itemWidth + hSpace
  722. }
  723. self.rightView.frame = NSMakeRect(leftW+contentW, 0, offset, height)
  724. itemX = 0
  725. for sv in self.rightView.subviews {
  726. guard let item = sv as? KMToolbarItemView else {
  727. continue
  728. }
  729. itemY = (height-item.itemHeight)*0.5
  730. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  731. itemX += item.itemWidth + hSpace
  732. let itemMinX = NSMinX(self.rightView.frame) + itemX
  733. if itemMinX <= width {
  734. item.isHidden = false
  735. } else {
  736. item.isHidden = true
  737. self._invisibleItems.append(item)
  738. }
  739. }
  740. return
  741. } else if width >= leftW { // 左 显示(全) 中 显示不全 右不显示
  742. let offset = width - leftW
  743. self.leftView.frame = NSMakeRect(0, 0, leftW, height)
  744. var itemX: CGFloat = leftMargin
  745. for sv in self.leftView.subviews {
  746. guard let item = sv as? KMToolbarItemView else {
  747. continue
  748. }
  749. itemY = (height-item.itemHeight)*0.5
  750. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  751. itemX += item.itemWidth + hSpace
  752. item.isHidden = false
  753. }
  754. if self.isMain() {
  755. self.centerView.frame = NSMakeRect(leftW, 0, offset, height)
  756. self.contentView.frame = NSMakeRect(0, 0, offset, height)
  757. } else {
  758. self.contentView.frame = NSMakeRect(leftW, 0, offset, height)
  759. }
  760. itemX = 0
  761. for sv in self.contentView.subviews {
  762. guard let item = sv as? KMToolbarItemView else {
  763. continue
  764. }
  765. itemY = (height-item.itemHeight)*0.5
  766. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  767. itemX += item.itemWidth + hSpace
  768. let itemMinX = leftW + itemX
  769. if itemMinX <= width {
  770. item.isHidden = false
  771. } else {
  772. item.isHidden = true
  773. self._invisibleItems.append(item)
  774. }
  775. }
  776. self.rightView.frame = NSMakeRect(leftW+contentW, 0, 0, height)
  777. itemX = 0
  778. for sv in self.rightView.subviews {
  779. guard let item = sv as? KMToolbarItemView else {
  780. continue
  781. }
  782. itemY = (height-item.itemHeight)*0.5
  783. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  784. itemX += item.itemWidth + hSpace
  785. item.isHidden = true
  786. self._invisibleItems.append(item)
  787. }
  788. return
  789. } else { // 左显示不全 中 + 右 不显示
  790. let offset = width - leftW
  791. self.leftView.frame = NSMakeRect(0, 0, offset, height)
  792. var itemX: CGFloat = leftMargin
  793. for sv in self.leftView.subviews {
  794. guard let item = sv as? KMToolbarItemView else {
  795. continue
  796. }
  797. itemY = (height-item.itemHeight)*0.5
  798. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  799. itemX += item.itemWidth + hSpace
  800. if itemX <= width {
  801. item.isHidden = false
  802. } else {
  803. item.isHidden = true
  804. self._invisibleItems.append(item)
  805. }
  806. }
  807. if self.isMain() {
  808. self.centerView.frame = NSMakeRect(leftW, 0, 0, height)
  809. self.contentView.frame = NSMakeRect(0, 0, 0, height)
  810. } else {
  811. self.contentView.frame = NSMakeRect(leftW, 0, 0, height)
  812. }
  813. itemX = 0
  814. for sv in self.contentView.subviews {
  815. guard let item = sv as? KMToolbarItemView else {
  816. continue
  817. }
  818. itemY = (height-item.itemHeight)*0.5
  819. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  820. itemX += item.itemWidth + hSpace
  821. item.isHidden = true
  822. self._invisibleItems.append(item)
  823. }
  824. self.rightView.frame = NSMakeRect(leftW+contentW, 0, 0, height)
  825. itemX = 0
  826. for sv in self.rightView.subviews {
  827. guard let item = sv as? KMToolbarItemView else {
  828. continue
  829. }
  830. itemY = (height-item.itemHeight)*0.5
  831. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  832. itemX += item.itemWidth + hSpace
  833. item.isHidden = true
  834. self._invisibleItems.append(item)
  835. }
  836. return
  837. }
  838. }
  839. #if false
  840. if contentLength <= self.frame.size.width { // 能正常显示
  841. if self.leftView.superview != nil {
  842. self.leftView.mas_remakeConstraints { make in
  843. make?.top.bottom().equalTo()(self)
  844. make?.left.equalTo()(self)
  845. make?.width.mas_equalTo()(leftW)
  846. }
  847. }
  848. if self.centerView.superview != nil {
  849. self.centerView.mas_remakeConstraints { make in
  850. make?.center.equalTo()(self)
  851. }
  852. } else {
  853. self.contentView.mas_remakeConstraints { make in
  854. make?.center.equalTo()(self)
  855. }
  856. }
  857. if self.rightView.superview != nil {
  858. self.rightView.mas_remakeConstraints { make in
  859. make?.top.bottom().equalTo()(self)
  860. make?.right.mas_equalTo()(0)
  861. make?.width.mas_equalTo()(rightW)
  862. // if self.centerView.superview != nil {
  863. // make?.left.equalTo()(self.centerView.mas_right)
  864. // } else {
  865. // make?.left.equalTo()(self.contentView.mas_right)
  866. // }
  867. }
  868. } else {
  869. }
  870. self.moreButton.isHidden = true
  871. } else { // 需要显示更多
  872. self.moreButton.isHidden = false
  873. self.moreButton.mas_remakeConstraints { make in
  874. make?.top.bottom().equalTo()(0)
  875. make?.width.mas_equalTo()(moreButtonWidth)
  876. make?.right.mas_equalTo()(0)
  877. }
  878. if self.frame.size.width > leftW + contentW { // 能显示 left + content, right 显示不下
  879. if self.centerView.superview != nil {
  880. self.centerView.mas_remakeConstraints { make in
  881. make?.center.equalTo()(self)
  882. }
  883. } else {
  884. self.contentView.mas_remakeConstraints { make in
  885. make?.center.equalTo()(self)
  886. }
  887. }
  888. if self.rightView.superview != nil {
  889. self.rightView.mas_remakeConstraints { make in
  890. make?.top.bottom().equalTo()(self)
  891. make?.right.mas_equalTo()(0)
  892. make?.width.mas_equalTo()(contentLength-self.frame.size.width)
  893. // if self.centerView.superview != nil {
  894. // make?.left.equalTo()(self.centerView.mas_right)
  895. // } else {
  896. // make?.left.equalTo()(self.contentView.mas_right)
  897. // }
  898. }
  899. }
  900. }
  901. }
  902. #endif
  903. // var frame = self.contentView.frame
  904. #if false
  905. let allItems: [KMToolbarItemView] = self.leftView.subviews as! [KMToolbarItemView] + self.items + self.rightView.subviews as! [KMToolbarItemView]
  906. var count = allItems.count
  907. var idx = count-1
  908. for item in allItems.reversed() {
  909. let tempFrame = item.superview?.convert(item.frame, to: item.superview!.superview) ?? .zero
  910. if (CGRectGetMaxX(tempFrame) <= self.frame.size.width) {
  911. count = idx
  912. break
  913. }
  914. idx -= 1
  915. }
  916. if (count >= allItems.count) {
  917. return
  918. }
  919. // frame.size.width = CGRectGetMaxX(self.items[count].frame)
  920. if (count == allItems.count-1) {
  921. // frame.origin.x = (self.frame.size.width-frame.size.width)/2.0;
  922. // self.moreButton.isHidden = true
  923. self._visibleItems = allItems
  924. self._invisibleItems = []
  925. for item in allItems {
  926. item.isHidden = false
  927. }
  928. // self.contentView.mas_remakeConstraints { make in
  929. // make?.centerX.equalTo()(0)
  930. // make?.centerY.equalTo()(0)
  931. // }
  932. } else {
  933. //// frame.origin.x = leftButtonWidth
  934. // self.moreButton.mas_makeConstraints { make in
  935. // make?.top.bottom().equalTo()(0)
  936. // make?.width.offset()(moreButtonWidth)
  937. // make?.right.equalTo()(self.mas_right)?.offset()(0)
  938. // }
  939. // self.moreButton.isHidden = false
  940. self._visibleItems.removeAll()
  941. for i in 0 ... count+1 {
  942. self._visibleItems.append(allItems[i])
  943. }
  944. // self.contentView.mas_remakeConstraints { make in
  945. // make?.left.equalTo()(self.mas_left)?.offset()(10)
  946. // make?.centerY.equalTo()(0)
  947. // }
  948. }
  949. self._invisibleItems.removeAll()
  950. for item in allItems.reversed() {
  951. let tempFrame = item.superview?.convert(item.frame, to: item.superview!.superview) ?? .zero
  952. KMPrint("\(item.itemIdentifier): \(tempFrame)")
  953. if (CGRectGetMaxX(tempFrame) <= self.frame.size.width) {
  954. item.isHidden = false
  955. } else {
  956. item.isHidden = true
  957. self._invisibleItems.append(item)
  958. }
  959. }
  960. if self._invisibleItems.isEmpty {
  961. KMPrint("无更多菜单")
  962. } else {
  963. for item in self._invisibleItems {
  964. KMPrint(item.itemIdentifier)
  965. }
  966. }
  967. #endif
  968. // KMPrint(self._visibleItems)
  969. }
  970. func hasLeft() -> Bool {
  971. if let items = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self) {
  972. return items.count > 0
  973. }
  974. return false
  975. }
  976. func hasRight() -> Bool {
  977. if let items = self.delegate?.toolbarRightDefaultItemIdentifiers?(self) {
  978. return items.count > 0
  979. }
  980. return false
  981. }
  982. func hasCenter() -> Bool {
  983. if let items = self.delegate?.toolbarDefaultItemIdentifiers?(self) {
  984. return items.count > 0
  985. }
  986. return false
  987. }
  988. func isMain() -> Bool {
  989. if self.contentView.superview == self {
  990. return false
  991. }
  992. return true
  993. }
  994. override func resizeSubviews(withOldSize oldSize: NSSize) {
  995. super.resizeSubviews(withOldSize: oldSize)
  996. self._layoutSubViewsIfNeed()
  997. }
  998. func reloadData() {
  999. let leftitemIdentifiers = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self) ?? []
  1000. if (leftitemIdentifiers.count > 0) {
  1001. self._updateMainView()
  1002. } else {
  1003. self._updateView()
  1004. }
  1005. self.resizeSubviews(withOldSize: self.frame.size)
  1006. }
  1007. func itemForItemIdentifier(_ itemIdentifier: String) -> KMToolbarItemView? {
  1008. for item in self.items {
  1009. if (item.itemIdentifier == itemIdentifier) {
  1010. return item
  1011. }
  1012. }
  1013. return nil
  1014. }
  1015. @objc func toolbarCustomChangeNotification() {
  1016. self.reloadData()
  1017. }
  1018. }