KMToolbarView.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. var contentInset: NSEdgeInsets = NSEdgeInsetsZero
  57. private lazy var centerView: NSView = {
  58. let view = NSView()
  59. return view
  60. }()
  61. private lazy var leftView: NSView = {
  62. let view = NSView()
  63. return view
  64. }()
  65. private lazy var rightView: NSView = {
  66. let view = NSView()
  67. return view
  68. }()
  69. static let kLeftMargin: CGFloat = 8
  70. static let kHSpace: CGFloat = 8
  71. static let kRightMargin: CGFloat = 8
  72. static let kMoreWidth: CGFloat = 30
  73. private var _leftWidth: CGFloat = 0
  74. private var _centerWidth: CGFloat = 0
  75. private var _rightWidth: CGFloat = 0
  76. var allowsUserCustomization = false
  77. private var configWindowC_: KMToolbarConfigWindowController?
  78. deinit {
  79. KMPrint("KMToolbarView deinit")
  80. }
  81. convenience init(identifier: String) {
  82. self.init()
  83. self._toolbarIdentifier = identifier
  84. self._addTrackingArea()
  85. }
  86. override func menu(for event: NSEvent) -> NSMenu? {
  87. if self.allowsUserCustomization == false {
  88. return super.menu(for: event)
  89. }
  90. let menu = NSMenu()
  91. let item = NSMenuItem(title: NSLocalizedString("Customize Toolbar", comment: ""), action: #selector(_customToolbarItemAction), keyEquivalent: "")
  92. menu.addItem(item)
  93. return menu
  94. }
  95. // MARK: - Private Methods
  96. @objc private func _customToolbarItemAction(_ sender: NSMenuItem?) {
  97. if self.configWindowC_ == nil {
  98. self.configWindowC_ = KMToolbarConfigWindowController()
  99. }
  100. var leftCellIdentifiers = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self) ?? []
  101. if leftCellIdentifiers.contains(KMDocumentHomeToolbarItemIdentifier) {
  102. leftCellIdentifiers.removeObject(KMDocumentHomeToolbarItemIdentifier)
  103. }
  104. self.configWindowC_?.leftCellIdentifiers = leftCellIdentifiers
  105. self.configWindowC_?.centerCellIdentifiers = (self.delegate?.toolbarDefaultItemIdentifiers?(self) ?? [])
  106. self.configWindowC_?.rightCellIdentifiers = (self.delegate?.toolbarRightDefaultItemIdentifiers?(self) ?? [])
  107. self.configWindowC_?.defaultCellIdentifiers = self.delegate?.toolbarAllowedItemIdentifiers?(self) ?? []
  108. self.window?.beginSheet((self.configWindowC_?.window)!, completionHandler: { [weak self] resp in
  109. self?.configWindowC_ = nil
  110. if resp == .OK {
  111. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  112. KMDataManager.default.toolbarConfigDataUpdated = true
  113. self?.reloadData()
  114. KMDataManager.default.toolbarConfigDataUpdated = false
  115. }
  116. }
  117. })
  118. }
  119. private func _addTrackingArea() {
  120. let trackingArea = NSTrackingArea(rect: self.bounds, options: [.inVisibleRect, .activeInKeyWindow, .mouseMoved], owner: self)
  121. self.addTrackingArea(trackingArea)
  122. }
  123. @objc private func _moreButtonAction() {
  124. let menu = NSMenu()
  125. for item in self._invisibleItems {
  126. if (item.itemIdentifier == KMToolbarDividerItemIdentifier) {
  127. menu.addItem(NSMenuItem.separator())
  128. } else if (item.menuFormRepresentation != nil) {
  129. menu.addItem(item.menuFormRepresentation!)
  130. }
  131. }
  132. menu.popUp(positioning: menu.items.first, at: NSMakePoint(NSMaxX(self.moreButton.frame), NSMaxY(self.moreButton.frame)-5), in: self)
  133. }
  134. private func _updateMainView(_ left: Bool = true, _ right: Bool = true) {
  135. if (self.centerView.superview == nil) {
  136. self.addSubview(self.centerView)
  137. }
  138. if left {
  139. self._updateLeftView()
  140. }
  141. if right {
  142. self._updateRightView()
  143. }
  144. if(self.contentView.superview == nil) {
  145. self.centerView.addSubview(self.contentView)
  146. }
  147. for item in self.items {
  148. item.removeFromSuperview()
  149. }
  150. self._items = []
  151. var items: [KMToolbarItemView] = []
  152. let itemIdentifiers = self.delegate?.toolbarDefaultItemIdentifiers?(self) ?? []
  153. var lastItem: KMToolbarItemView?
  154. var iWidth: CGFloat = 0
  155. // -self._invisibleItems.count
  156. for i in 0 ..< (itemIdentifiers.count) {
  157. let itemIdentifier = itemIdentifiers[i]
  158. var item: KMToolbarItemView?
  159. if itemIdentifier == KMToolbarDividerItemIdentifier {
  160. item = KMToolbarCustomItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  161. } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  162. item = KMToolbarCustomItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  163. item?.layer?.backgroundColor = .clear
  164. item?.imageViewBox.borderColor = .clear
  165. item?.layer?.cornerRadius = 0.0
  166. } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) {
  167. item = data
  168. }
  169. guard let data = item else {
  170. continue
  171. }
  172. self.contentView.addSubview(data)
  173. items.append(data)
  174. iWidth += data.itemWidth
  175. if lastItem != nil {
  176. // + 前一个的间距
  177. iWidth += Self.kHSpace
  178. }
  179. lastItem = item
  180. }
  181. self._items = items
  182. self._centerWidth = iWidth
  183. self.addSubview(self.moreButton)
  184. }
  185. // UI 20 16
  186. // item w 28 h 24
  187. private func _updateView(_ left: Bool = true, _ right: Bool = true) {
  188. if left {
  189. self._updateLeftView()
  190. }
  191. if right {
  192. self._updateRightView()
  193. }
  194. if(self.contentView.superview == nil) {
  195. self.addSubview(self.contentView)
  196. }
  197. for item in self.items {
  198. item.removeFromSuperview()
  199. }
  200. self._items = []
  201. var items: [KMToolbarItemView] = []
  202. let itemIdentifiers = self.delegate?.toolbarDefaultItemIdentifiers?(self) ?? []
  203. var lastItem: KMToolbarItemView?
  204. // -self._invisibleItems.count
  205. var iWidth: CGFloat = 0
  206. for i in 0 ..< itemIdentifiers.count {
  207. let itemIdentifier = itemIdentifiers[i]
  208. var item: KMToolbarItemView?
  209. if itemIdentifier == KMToolbarDividerItemIdentifier {
  210. item = KMToolbarCustomItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  211. } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  212. item = KMToolbarCustomItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  213. item?.layer?.backgroundColor = .clear
  214. item?.imageViewBox.borderColor = .clear
  215. item?.layer?.cornerRadius = 0.0
  216. } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) {
  217. item = data
  218. }
  219. guard let data = item else {
  220. continue
  221. }
  222. self.contentView.addSubview(data)
  223. items.append(data)
  224. iWidth += data.itemWidth
  225. if lastItem != nil {
  226. // + 前一个的间距
  227. iWidth += Self.kHSpace
  228. }
  229. lastItem = item
  230. }
  231. self._items = items
  232. self._centerWidth = iWidth
  233. self.addSubview(self.moreButton)
  234. }
  235. private func _updateLeftView() {
  236. // 刷新前移除视图
  237. for item in self.leftView.subviews {
  238. item.removeFromSuperview()
  239. }
  240. if (self.leftView.superview != nil) {
  241. self.leftView.removeFromSuperview()
  242. }
  243. var iWidth: CGFloat = 0
  244. if let itemIdentifiers = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self), itemIdentifiers.count > 0 {
  245. if (self.leftView.superview == nil) {
  246. self.addSubview(self.leftView)
  247. }
  248. self.leftView.mas_remakeConstraints { make in
  249. make?.left.equalTo()(self.mas_left)?.offset()(0)
  250. make?.top.equalTo()(self.mas_top)?.offset()(0)
  251. make?.bottom.equalTo()(self.mas_bottom)?.offset()(0)
  252. }
  253. var lastItem: KMToolbarItemView?
  254. for i in 0 ..< itemIdentifiers.count {
  255. let itemIdentifier = itemIdentifiers[i]
  256. var item: KMToolbarItemView?
  257. if itemIdentifier == KMToolbarDividerItemIdentifier {
  258. item = KMToolbarCustomItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  259. } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  260. item = KMToolbarCustomItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  261. item?.layer?.backgroundColor = .clear
  262. item?.imageViewBox.borderColor = .clear
  263. item?.layer?.cornerRadius = 0.0
  264. } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) {
  265. item = data
  266. }
  267. guard let data = item else {
  268. continue
  269. }
  270. self.leftView.addSubview(data)
  271. iWidth += data.itemWidth
  272. if lastItem != nil {
  273. // + 前一个的间距
  274. iWidth += Self.kHSpace
  275. }
  276. lastItem = item
  277. }
  278. iWidth += (Self.kLeftMargin + Self.kHSpace)
  279. }
  280. self._leftWidth = iWidth
  281. }
  282. private func _updateRightView() {
  283. // 刷新前移除视图
  284. for item in self.rightView.subviews {
  285. item.removeFromSuperview()
  286. }
  287. if (self.rightView.superview != nil) {
  288. self.rightView.removeFromSuperview()
  289. }
  290. var iWidth: CGFloat = 0
  291. if let itemIdentifiers = self.delegate?.toolbarRightDefaultItemIdentifiers?(self), itemIdentifiers.isEmpty == false {
  292. if (self.rightView.superview == nil) {
  293. self.addSubview(self.rightView)
  294. }
  295. var lastItem: KMToolbarItemView?
  296. for i in 0 ..< itemIdentifiers.count {
  297. let itemIdentifier = itemIdentifiers[i]
  298. var item: KMToolbarItemView?
  299. if itemIdentifier == KMToolbarDividerItemIdentifier {
  300. item = KMToolbarCustomItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  301. } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  302. item = KMToolbarCustomItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil)
  303. item?.layer?.backgroundColor = .clear
  304. item?.imageViewBox.borderColor = .clear
  305. item?.layer?.cornerRadius = 0.0;
  306. } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) {
  307. item = data
  308. }
  309. guard let data = item else {
  310. continue
  311. }
  312. self.rightView.addSubview(data)
  313. iWidth += data.itemWidth
  314. if lastItem != nil {
  315. // + 前一个的间距
  316. iWidth += Self.kHSpace
  317. }
  318. lastItem = item
  319. }
  320. iWidth += Self.kRightMargin
  321. }
  322. self._rightWidth = iWidth
  323. }
  324. override func updateLayer() {
  325. super.updateLayer()
  326. if KMAppearance.isDarkMode() {
  327. self.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  328. } else {
  329. self.layer?.backgroundColor = .clear
  330. }
  331. for item in self._items {
  332. item.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  333. item.customizeView?.interfaceThemeDidChanged(self.appearance?.name ?? .aqua)
  334. }
  335. }
  336. // MARK: - View Methods
  337. override func viewWillMove(toWindow newWindow: NSWindow?) {
  338. super.viewWillMove(toWindow: newWindow)
  339. if (newWindow != nil && self._contentView == nil) {
  340. let leftitemIdentifiers = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self) ?? []
  341. if (leftitemIdentifiers.count > 0) {
  342. self._updateMainView()
  343. }else {
  344. self._updateView()
  345. }
  346. self.resizeSubviews(withOldSize: self.frame.size)
  347. let topLine = NSBox(frame: NSMakeRect(0, 0, self.frame.size.width, 0.5))
  348. topLine.boxType = .separator
  349. topLine.fillColor = .black
  350. self.addSubview(topLine)
  351. topLine.mas_makeConstraints { make in
  352. make?.left.right().equalTo()(self)
  353. make?.bottom.equalTo()(self.mas_bottom)?.offset()(-0.5)
  354. make?.height.offset()(0.5)
  355. }
  356. let bottomLine = NSBox(frame: NSMakeRect(0, 0, self.frame.size.width, 0.5))
  357. bottomLine.boxType = .separator
  358. bottomLine.fillColor = .black
  359. self.addSubview(bottomLine)
  360. bottomLine.mas_makeConstraints { make in
  361. make?.left.right().equalTo()(self)
  362. make?.top.equalTo()(self.mas_top)?.offset()(0.1)
  363. make?.height.offset()(0.5)
  364. }
  365. }
  366. if (newWindow != nil) {
  367. self._addTrackingArea()
  368. }
  369. }
  370. private func _layoutSubViewsIfNeed() {
  371. if (self._contentView == nil) {
  372. return
  373. }
  374. let moreButtonWidth = 18.0
  375. let leftW = self._leftWidth
  376. let contentW = self._centerWidth
  377. let rightW = self._rightWidth
  378. // KMPrint("width: \(leftW), \(contentW), \(rightW)")
  379. // KMPrint("\(self.frame.size.width)")
  380. let width = NSWidth(self.bounds) - self.contentInset.left - self.contentInset.right
  381. let height = NSHeight(self.bounds)
  382. let leftMargin = Self.kLeftMargin
  383. let hSpace = Self.kHSpace
  384. let itemH: CGFloat = 40
  385. var itemY: CGFloat = (height-itemH)*0.5
  386. let contentLength = leftW + contentW + rightW
  387. if (contentLength <= 0) {
  388. return
  389. }
  390. self._invisibleItems.removeAll()
  391. if width >= contentLength { // 左 + 中 + 右 显示(全)
  392. self.moreButton.isHidden = true
  393. let offset = width - contentLength
  394. self.leftView.frame = NSMakeRect(0, 0, leftW, height)
  395. var itemX: CGFloat = leftMargin
  396. for sv in self.leftView.subviews {
  397. guard let item = sv as? KMToolbarItemView else {
  398. continue
  399. }
  400. item.isHidden = false
  401. itemY = (height-item.itemHeight)*0.5
  402. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  403. itemX += item.itemWidth + hSpace
  404. }
  405. if self.isMain() {
  406. self.centerView.frame = NSMakeRect(leftW, 0, contentW + offset, height)
  407. self.contentView.frame = NSMakeRect(offset*0.5, 0, contentW, height)
  408. } else {
  409. self.contentView.frame = NSMakeRect(offset*0.5, 0, contentW, height)
  410. }
  411. itemX = 0
  412. for sv in self.contentView.subviews {
  413. guard let item = sv as? KMToolbarItemView else {
  414. continue
  415. }
  416. item.isHidden = false
  417. itemY = (height-item.itemHeight)*0.5
  418. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  419. itemX += item.itemWidth + hSpace
  420. }
  421. self.rightView.frame = NSMakeRect(leftW+contentW+offset, 0, rightW, height)
  422. itemX = 0
  423. for sv in self.rightView.subviews {
  424. guard let item = sv as? KMToolbarItemView else {
  425. continue
  426. }
  427. item.isHidden = false
  428. itemY = (height-item.itemHeight)*0.5
  429. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  430. itemX += item.itemWidth + hSpace
  431. }
  432. return
  433. } else { // 显示更多
  434. self.moreButton.isHidden = false
  435. self.moreButton.mas_remakeConstraints { make in
  436. make?.top.bottom().equalTo()(0)
  437. make?.width.mas_equalTo()(moreButtonWidth)
  438. make?.right.mas_equalTo()(-self.contentInset.right)
  439. }
  440. if width >= leftW + contentW { // 左 + 中 显示(全) 右显示不全
  441. let offset = width - leftW - contentW
  442. self.leftView.frame = NSMakeRect(0, 0, leftW, height)
  443. var itemX: CGFloat = leftMargin
  444. for sv in self.leftView.subviews {
  445. guard let item = sv as? KMToolbarItemView else {
  446. continue
  447. }
  448. item.isHidden = false
  449. itemY = (height-item.itemHeight)*0.5
  450. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  451. itemX += item.itemWidth + hSpace
  452. }
  453. if self.isMain() {
  454. self.centerView.frame = NSMakeRect(leftW, 0, contentW, height)
  455. self.contentView.frame = NSMakeRect(0, 0, contentW + offset, height)
  456. } else {
  457. self.contentView.frame = NSMakeRect(leftW, 0, contentW, height)
  458. }
  459. itemX = 0
  460. for sv in self.contentView.subviews {
  461. guard let item = sv as? KMToolbarItemView else {
  462. continue
  463. }
  464. item.isHidden = false
  465. itemY = (height-item.itemHeight)*0.5
  466. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  467. itemX += item.itemWidth + hSpace
  468. }
  469. self.rightView.frame = NSMakeRect(leftW+contentW, 0, offset, height)
  470. itemX = 0
  471. for sv in self.rightView.subviews {
  472. guard let item = sv as? KMToolbarItemView else {
  473. continue
  474. }
  475. itemY = (height-item.itemHeight)*0.5
  476. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  477. itemX += item.itemWidth + hSpace
  478. let itemMinX = NSMinX(self.rightView.frame) + itemX
  479. if itemMinX <= width {
  480. item.isHidden = false
  481. } else {
  482. item.isHidden = true
  483. self._invisibleItems.append(item)
  484. }
  485. }
  486. return
  487. } else if width >= leftW { // 左 显示(全) 中 显示不全 右不显示
  488. let offset = width - leftW
  489. self.leftView.frame = NSMakeRect(0, 0, leftW, height)
  490. var itemX: CGFloat = leftMargin
  491. for sv in self.leftView.subviews {
  492. guard let item = sv as? KMToolbarItemView else {
  493. continue
  494. }
  495. itemY = (height-item.itemHeight)*0.5
  496. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  497. itemX += item.itemWidth + hSpace
  498. item.isHidden = false
  499. }
  500. if self.isMain() {
  501. self.centerView.frame = NSMakeRect(leftW, 0, offset, height)
  502. self.contentView.frame = NSMakeRect(0, 0, offset, height)
  503. } else {
  504. self.contentView.frame = NSMakeRect(leftW, 0, offset, height)
  505. }
  506. itemX = 0
  507. for sv in self.contentView.subviews {
  508. guard let item = sv as? KMToolbarItemView else {
  509. continue
  510. }
  511. itemY = (height-item.itemHeight)*0.5
  512. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  513. itemX += item.itemWidth + hSpace
  514. let itemMinX = leftW + itemX
  515. if itemMinX <= width {
  516. item.isHidden = false
  517. } else {
  518. item.isHidden = true
  519. self._invisibleItems.append(item)
  520. }
  521. }
  522. self.rightView.frame = NSMakeRect(leftW+contentW, 0, 0, height)
  523. itemX = 0
  524. for sv in self.rightView.subviews {
  525. guard let item = sv as? KMToolbarItemView else {
  526. continue
  527. }
  528. itemY = (height-item.itemHeight)*0.5
  529. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  530. itemX += item.itemWidth + hSpace
  531. item.isHidden = true
  532. self._invisibleItems.append(item)
  533. }
  534. return
  535. } else { // 左显示不全 中 + 右 不显示
  536. let offset = width - leftW
  537. self.leftView.frame = NSMakeRect(0, 0, offset, height)
  538. var itemX: CGFloat = leftMargin
  539. for sv in self.leftView.subviews {
  540. guard let item = sv as? KMToolbarItemView else {
  541. continue
  542. }
  543. itemY = (height-item.itemHeight)*0.5
  544. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  545. itemX += item.itemWidth + hSpace
  546. if itemX <= width {
  547. item.isHidden = false
  548. } else {
  549. item.isHidden = true
  550. self._invisibleItems.append(item)
  551. }
  552. }
  553. if self.isMain() {
  554. self.centerView.frame = NSMakeRect(leftW, 0, 0, height)
  555. self.contentView.frame = NSMakeRect(0, 0, 0, height)
  556. } else {
  557. self.contentView.frame = NSMakeRect(leftW, 0, 0, height)
  558. }
  559. itemX = 0
  560. for sv in self.contentView.subviews {
  561. guard let item = sv as? KMToolbarItemView else {
  562. continue
  563. }
  564. itemY = (height-item.itemHeight)*0.5
  565. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  566. itemX += item.itemWidth + hSpace
  567. item.isHidden = true
  568. self._invisibleItems.append(item)
  569. }
  570. self.rightView.frame = NSMakeRect(leftW+contentW, 0, 0, height)
  571. itemX = 0
  572. for sv in self.rightView.subviews {
  573. guard let item = sv as? KMToolbarItemView else {
  574. continue
  575. }
  576. itemY = (height-item.itemHeight)*0.5
  577. item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight)
  578. itemX += item.itemWidth + hSpace
  579. item.isHidden = true
  580. self._invisibleItems.append(item)
  581. }
  582. return
  583. }
  584. }
  585. }
  586. func hasLeft() -> Bool {
  587. if let items = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self) {
  588. return items.count > 0
  589. }
  590. return false
  591. }
  592. func hasRight() -> Bool {
  593. if let items = self.delegate?.toolbarRightDefaultItemIdentifiers?(self) {
  594. return items.count > 0
  595. }
  596. return false
  597. }
  598. func hasCenter() -> Bool {
  599. if let items = self.delegate?.toolbarDefaultItemIdentifiers?(self) {
  600. return items.count > 0
  601. }
  602. return false
  603. }
  604. func isMain() -> Bool {
  605. if self.contentView.superview == self {
  606. return false
  607. }
  608. return true
  609. }
  610. override func resizeSubviews(withOldSize oldSize: NSSize) {
  611. super.resizeSubviews(withOldSize: oldSize)
  612. self._layoutSubViewsIfNeed()
  613. }
  614. func reloadData() {
  615. let leftitemIdentifiers = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self) ?? []
  616. if (leftitemIdentifiers.count > 0) {
  617. self._updateMainView()
  618. } else {
  619. self._updateView()
  620. }
  621. self.resizeSubviews(withOldSize: self.frame.size)
  622. }
  623. func itemForItemIdentifier(_ itemIdentifier: String) -> KMToolbarItemView? {
  624. for item in self.items {
  625. if (item.itemIdentifier == itemIdentifier) {
  626. return item
  627. }
  628. }
  629. return nil
  630. }
  631. }