KMToolbarView.swift 27 KB

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