// // KMToolbarView.swift // PDF Reader Pro // // Created by tangchao on 2023/10/24. // import Cocoa private let KMToolbarSpaceItemWidth = 30.0 private let KMToolbarLineItemWidth = 1.0 private let KMToolbarItemSpace = 8.0 @objc protocol KMToolbarViewDelegate: NSObjectProtocol { @objc optional func toolbar(_ toolbar: KMToolbarView, itemFor identifier: String) -> KMToolbarItemView @objc optional func toolbarAllowedItemIdentifiers(_ toolbar: KMToolbarView) -> [String] @objc optional func toolbarDefaultItemIdentifiers(_ toolbar: KMToolbarView) -> [String] @objc optional func toolbarLeftDefaultItemIdentifiers(_ toolbar: KMToolbarView) -> [String] @objc optional func toolbarRightDefaultItemIdentifiers(_ toolbar: KMToolbarView) -> [String] } @objcMembers class KMToolbarView: NSView { private var _toolbarIdentifier: String? var toolbarIdentifier: String? { get { return self._toolbarIdentifier } } private var _items: [KMToolbarItemView] = [] var items: [KMToolbarItemView] { get { return self._items } } weak var delegate: KMToolbarViewDelegate? private var _visibleItems: [KMToolbarItemView] = [] private var _invisibleItems: [KMToolbarItemView] = [] private var _contentView: NSView? var contentView: NSView { get { if (self._contentView == nil) { self._contentView = NSView() } return self._contentView! } } lazy var moreButton: NSButton = { let _moreButton = NSButton() _moreButton.bezelStyle = .regularSquare _moreButton.isBordered = false _moreButton.font = .systemFont(ofSize: 20) _moreButton.title = " »" _moreButton.isHidden = true _moreButton.target = self _moreButton.action = #selector(_moreButtonAction) _moreButton.wantsLayer = true _moreButton.layer?.backgroundColor = NSColor.clear.cgColor return _moreButton }() private lazy var centerView: NSView = { let view = NSView() return view }() private lazy var leftView: NSView = { let view = NSView() // view.wantsLayer = true // view.layer?.backgroundColor = NSColor(red: 247/255.0, green: 248/255.0, blue: 250/255.0 , alpha: 1).cgColor return view }() private class KMToolbarView_rightView: NSView {} private lazy var rightView: NSView = { let view = KMToolbarView_rightView() // view.wantsLayer = true // view.layer?.backgroundColor = NSColor(red: 247/255.0, green: 248/255.0, blue: 250/255.0, alpha: 1).cgColor return view }() static let kLeftMargin: CGFloat = 8 static let kHSpace: CGFloat = 8 static let kRightMargin: CGFloat = 8 static let kMoreWidth: CGFloat = 30 private var _leftWidth: CGFloat = 0 private var _centerWidth: CGFloat = 0 private var _rightWidth: CGFloat = 0 convenience init(identifier: String) { self.init() self._toolbarIdentifier = identifier self._addTrackingArea() NotificationCenter.default.addObserver(self, selector: #selector(toolbarCustomChangeNotification), name: KMToolbarCustomChangeNotification, object: nil) // self.leftView.wantsLayer = true // self.contentView.wantsLayer = true // self.rightView.wantsLayer = true // self.leftView.layer?.backgroundColor = NSColor.red.cgColor // self.contentView.layer?.backgroundColor = NSColor.blue.cgColor // self.rightView.layer?.backgroundColor = NSColor.green.cgColor } deinit { Swift.debugPrint("KMToolbarView deinit") NotificationCenter.default.removeObserver(self) } // MARK: - Private Methods private func _addTrackingArea() { let trackingArea = NSTrackingArea(rect: self.bounds, options: [.inVisibleRect, .activeInKeyWindow, .mouseMoved], owner: self) self.addTrackingArea(trackingArea) } override func mouseMoved(with event: NSEvent) { // self.window?.mouseMoved(with: event) super.mouseMoved(with: event) } @objc private func _moreButtonAction() { let menu = NSMenu() for item in self._invisibleItems { if (item.itemIdentifier == KMToolbarDividerItemIdentifier) { menu.addItem(NSMenuItem.separator()) } else if (item.menuFormRepresentation != nil) { menu.addItem(item.menuFormRepresentation!) } } menu.popUp(positioning: menu.items.first, at: NSMakePoint(CGRectGetMaxX(self.moreButton.frame), CGRectGetMaxY(self.moreButton.frame)-5), in: self) } private func _updateMainView(_ left: Bool = true, _ right: Bool = true) { if (self.centerView.superview == nil) { self.addSubview(self.centerView) } if left { self._updateLeftView() } if right { self._updateRightView() } // self.centerView.mas_remakeConstraints { make in // make?.right.equalTo()(0) // make?.top.equalTo()(self.mas_top)?.offset()(0) // make?.bottom.equalTo()(self.mas_bottom)?.offset()(0) // make?.left.mas_equalTo()(0) // } if(self.contentView.superview == nil) { self.centerView.addSubview(self.contentView) } // self.contentView.mas_remakeConstraints { make in // make?.centerX.equalTo()(0) // make?.centerY.equalTo()(0) // } for item in self.items { item.removeFromSuperview() } self._items = [] var posX = 0.0 var items: [KMToolbarItemView] = [] var itemIdentifiers = self.delegate?.toolbarDefaultItemIdentifiers?(self) ?? [] var lastItem: KMToolbarItemView? var iWidth: CGFloat = 0 for i in 0 ..< (itemIdentifiers.count-self._invisibleItems.count) { let itemIdentifier = itemIdentifiers[i] var item: KMToolbarItemView? if itemIdentifier == KMToolbarDividerItemIdentifier { item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil) } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier { item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil) item?.layer?.backgroundColor = .clear item?.imageViewBox.borderColor = .clear item?.layer?.cornerRadius = 0.0 } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) { item = data } guard let data = item else { continue } self.contentView.addSubview(data) // if (lastItem != nil) { // if (i == itemIdentifiers.count - 1) { // item?.mas_makeConstraints({ make in // make?.top.bottom().equalTo()(0) // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace) // make?.right.equalTo()(self.contentView.mas_right)?.offset()(0) // }) // } else { // item?.mas_makeConstraints({ make in // make?.top.bottom().equalTo()(0) // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace) // }) // } // // } else { // if (item?.image != nil) { // item?.mas_makeConstraints({ make in // make?.top.bottom().equalTo()(0) // make?.left.equalTo()(0) // }) // } else { // item?.mas_makeConstraints({ make in // make?.top.bottom().equalTo()(0) // make?.left.equalTo()(0) // make?.height.offset()(40) // }) // } // } items.append(data) iWidth += data.itemWidth if lastItem != nil { // + 前一个的间距 iWidth += Self.kHSpace } lastItem = item // posX += item!.frame.size.width } self._items = items self._centerWidth = iWidth self.addSubview(self.moreButton) } // UI 20 16 // item w 28 h 24 private func _updateView(_ left: Bool = true, _ right: Bool = true) { if left { self._updateLeftView() } if right { self._updateRightView() } if(self.contentView.superview == nil) { self.addSubview(self.contentView) } // self.contentView.mas_remakeConstraints { make in // make?.centerX.equalTo()(0) // make?.centerY.equalTo()(0) // } for item in self.items { item.removeFromSuperview() } self._items = [] var posX = 0.0 var items: [KMToolbarItemView] = [] var itemIdentifiers = self.delegate?.toolbarDefaultItemIdentifiers?(self) ?? [] var lastItem: KMToolbarItemView? // -self._invisibleItems.count var iWidth: CGFloat = 0 for i in 0 ..< itemIdentifiers.count { let itemIdentifier = itemIdentifiers[i] var item: KMToolbarItemView? if itemIdentifier == KMToolbarDividerItemIdentifier { item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil) } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier { item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil) item?.layer?.backgroundColor = .clear item?.imageViewBox.borderColor = .clear item?.layer?.cornerRadius = 0.0 } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) { item = data } guard let data = item else { continue } self.contentView.addSubview(data) // if (lastItem != nil) { // if (i == itemIdentifiers.count - 1) { // item?.mas_makeConstraints({ make in // make?.top.bottom().equalTo()(0) // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace) // make?.right.equalTo()(self.contentView.mas_right)?.offset()(0) // }) // } else { // if itemIdentifier == KMNewToolbarSpaceItemIdentifier { // item?.mas_makeConstraints({ make in // make?.top.bottom().equalTo()(0) // make?.left.equalTo()(lastItem!.mas_right)?.offset()(2) // }) // } else { // if lastItem?.itemIdentifier == KMNewToolbarSpaceItemIdentifier { // item?.mas_makeConstraints({ make in // make?.top.bottom().equalTo()(0) // make?.left.equalTo()(lastItem!.mas_right)?.offset()(2) // }) // } else { // item?.mas_makeConstraints({ make in // make?.top.bottom().equalTo()(0) // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace) // }) // } // } // } // } else { // if (item?.image != nil) { // item?.mas_makeConstraints({ make in // make?.top.bottom().equalTo()(0) // make?.left.equalTo()(0) // }) // } else { // item?.mas_makeConstraints({ make in // make?.top.bottom().equalTo()(0) // make?.left.equalTo()(0) // make?.height.offset()(40) // }) // } // } items.append(data) iWidth += data.itemWidth if lastItem != nil { // + 前一个的间距 iWidth += Self.kHSpace } lastItem = item // posX += item!.frame.size.width } self._items = items self._centerWidth = iWidth self.addSubview(self.moreButton) } private func _updateLeftView() { // 刷新前移除视图 for item in self.leftView.subviews { item.removeFromSuperview() } if (self.leftView.superview != nil) { self.leftView.removeFromSuperview() } var iWidth: CGFloat = 0 if let itemIdentifiers = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self), itemIdentifiers.count > 0 { if (self.leftView.superview == nil) { self.addSubview(self.leftView) } self.leftView.mas_remakeConstraints { make in make?.left.equalTo()(self.mas_left)?.offset()(0) make?.top.equalTo()(self.mas_top)?.offset()(0) make?.bottom.equalTo()(self.mas_bottom)?.offset()(0) } var lastItem: KMToolbarItemView? for i in 0 ..< itemIdentifiers.count { let itemIdentifier = itemIdentifiers[i] var item: KMToolbarItemView? if itemIdentifier == KMToolbarDividerItemIdentifier { item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil) } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier { item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil) item?.layer?.backgroundColor = .clear item?.imageViewBox.borderColor = .clear item?.layer?.cornerRadius = 0.0 } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) { item = data } guard let data = item else { continue } self.leftView.addSubview(data) // if (lastItem != nil) { // if (i == itemIdentifiers.count - 1) { // item?.mas_makeConstraints({ make in // make?.height.mas_equalTo()(40) // make?.centerY.mas_equalTo()(0) // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace) // make?.right.equalTo()(self.leftView.mas_right)?.offset()(0) // }) // } else { // item?.mas_makeConstraints({ make in // make?.height.mas_equalTo()(40) // make?.centerY.mas_equalTo()(0) // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace) // }) // } // // } else { // if (i == itemIdentifiers.count - 1) { // item?.mas_makeConstraints({ make in // make?.height.mas_equalTo()(40) // make?.centerY.mas_equalTo()(0) // make?.left.equalTo()(self.leftView.mas_left)?.offset()(KMToolbarItemSpace) // make?.right.equalTo()(self.leftView.mas_right)?.offset()(-20) // }) // } else { // item?.mas_makeConstraints({ make in // make?.height.mas_equalTo()(40) // make?.centerY.mas_equalTo()(0) // make?.left.equalTo()(KMToolbarItemSpace) // }) // } // } iWidth += data.itemWidth if lastItem != nil { // + 前一个的间距 iWidth += Self.kHSpace } lastItem = item } iWidth += (Self.kLeftMargin + Self.kHSpace) } self._leftWidth = iWidth } private func _updateRightView() { // 刷新前移除视图 for item in self.rightView.subviews { item.removeFromSuperview() } if (self.rightView.superview != nil) { self.rightView.removeFromSuperview() } var iWidth: CGFloat = 0 if let itemIdentifiers = self.delegate?.toolbarRightDefaultItemIdentifiers?(self), itemIdentifiers.isEmpty == false { if (self.rightView.superview == nil) { self.addSubview(self.rightView) } // self.rightView.mas_remakeConstraints { make in // make?.right.equalTo()(self.mas_right)?.offset()(0) // make?.top.equalTo()(self.mas_top)?.offset()(0) // make?.bottom.equalTo()(self.mas_bottom)?.offset()(0) // } var lastItem: KMToolbarItemView? for i in 0 ..< itemIdentifiers.count { let itemIdentifier = itemIdentifiers[i] var item: KMToolbarItemView? if itemIdentifier == KMToolbarDividerItemIdentifier { item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil) } else if itemIdentifier == KMNewToolbarSpaceItemIdentifier { item = KMToolbarItemView(itemIdentifier: itemIdentifier, postition: .imageOnly, withPopMenu: nil) item?.layer?.backgroundColor = .clear item?.imageViewBox.borderColor = .clear item?.layer?.cornerRadius = 0.0; } else if let data = self.delegate?.toolbar?(self, itemFor: itemIdentifier) { item = data } guard let data = item else { continue } self.rightView.addSubview(data) // if (lastItem != nil) { // if (i == itemIdentifiers.count - 1) { // item?.mas_makeConstraints({ make in // make?.height.offset()(40) // make?.centerY.mas_equalTo()(0) // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace) // make?.right.equalTo()(self.rightView.mas_right)?.offset()(-KMToolbarItemSpace) // }) // } else { // item?.mas_makeConstraints({ make in // make?.height.offset()(40) // make?.centerY.mas_equalTo()(0) // make?.left.equalTo()(lastItem!.mas_right)?.offset()(KMToolbarItemSpace) // }) // } // } else { // if (i == itemIdentifiers.count - 1) { // item?.mas_makeConstraints({ make in // make?.centerY.mas_equalTo()(0) // make?.height.offset()(40) // make?.left.equalTo()(self.rightView.mas_left)?.offset()(KMToolbarItemSpace) // make?.right.equalTo()(self.rightView.mas_right)?.offset()(-KMToolbarItemSpace) // }) // } else { // item?.mas_makeConstraints({ make in // make?.height.offset()(40) // make?.centerY.mas_equalTo()(0) // make?.left.equalTo()(0) // }) // } // } iWidth += data.itemWidth if lastItem != nil { // + 前一个的间距 iWidth += Self.kHSpace } lastItem = item } iWidth += Self.kRightMargin } self._rightWidth = iWidth } override func updateLayer() { super.updateLayer() if KMAppearance.isDarkMode() { self.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor } else { self.layer?.backgroundColor = .clear } for item in self._items { item.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua) item.customizeView?.interfaceThemeDidChanged(self.appearance?.name ?? .aqua) item.nameBtn.setTitleColor(KMAppearance.titleColor()) } } // MARK: - View Methods override func viewWillMove(toWindow newWindow: NSWindow?) { super.viewWillMove(toWindow: newWindow) if (newWindow != nil && self._contentView == nil) { let leftitemIdentifiers = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self) ?? [] if (leftitemIdentifiers.count > 0) { self._updateMainView() }else { self._updateView() } self.resizeSubviews(withOldSize: self.frame.size) // NotificationCenter.default.addObserver(self, selector: #selector(toolbarCustomChangeNotification), name: KMToolbarCustomChangeNotification, object: nil) let topLine = NSBox(frame: CGRectMake(0, 0, self.frame.size.width, 0.5)) topLine.boxType = .separator topLine.fillColor = .black self.addSubview(topLine) topLine.mas_makeConstraints { make in make?.left.right().equalTo()(self) make?.bottom.equalTo()(self.mas_bottom)?.offset()(-0.5) make?.height.offset()(0.5) } let bottomLine = NSBox(frame: CGRectMake(0, 0, self.frame.size.width, 0.5)) bottomLine.boxType = .separator bottomLine.fillColor = .black self.addSubview(bottomLine) bottomLine.mas_makeConstraints { make in make?.left.right().equalTo()(self) make?.top.equalTo()(self.mas_top)?.offset()(0.1) make?.height.offset()(0.5) } // DispatchQueue.main.async { // self._layoutSubViewsIfNeed() // } } if (newWindow != nil) { self._addTrackingArea() } } private func _right_layoutSubViewIfNeed() { if self._rightWidth < 0 { // 没有item return } if self.rightView.superview == nil { // 没有item return } if self.rightView.subviews.isEmpty { // 没有item return } let width = NSWidth(self.bounds) let contentW = self._leftWidth + self._centerWidth + self._rightWidth if width >= contentW { // 左 1 中 1 右 1 self._updateRightView() return } if width >= self._leftWidth + self._centerWidth { // 左 1 中 1 右 0 let leftMargin = self._leftWidth-self._centerWidth self.rightView.mas_remakeConstraints { make in make?.right.mas_equalTo()(-Self.kMoreWidth) make?.top.bottom().mas_equalTo()(0) make?.width.mas_equalTo()(width-leftMargin) } var itemX: CGFloat = leftMargin for sv in self.rightView.subviews { guard let item = sv as? KMToolbarItemView else { continue } let itemMaxX = itemX + item.itemWidth + Self.kHSpace if itemMaxX > width { // 隐藏 item.isHidden = true self._invisibleItems.append(item) } else { // 显示 item.isHidden = false } } return } // 右边完全显示不下 self.rightView.mas_remakeConstraints { make in make?.right.mas_equalTo()(0) make?.top.bottom().mas_equalTo()(0) make?.width.mas_equalTo()(0) } } private func _center_layoutSubViewIfNeed() { if self._centerWidth < 0 { // 没有item return } if self.contentView.superview == nil { // 没有item return } if self.contentView.subviews.isEmpty { // 没有item return } let width = NSWidth(self.bounds) let contentW = self._leftWidth + self._centerWidth + self._rightWidth if width >= contentW { // 左 1 中 1 右 1 if self.contentView.superview == self { self._updateView(false, false) } else { self._updateMainView(false, false) } return } if width >= self._leftWidth + self._centerWidth { // 左 1 中 1 右 0 (中 完全显示) if self.contentView.superview == self { self._updateView(false, false) } else { self._updateMainView(false, false) } return } if width >= self._leftWidth { // 左 1 中 1 右 0 (中 不完全显示) let leftMargin = self._leftWidth self.contentView.mas_remakeConstraints { make in make?.right.mas_equalTo()(-Self.kMoreWidth) make?.top.bottom().mas_equalTo()(0) make?.width.mas_equalTo()(width-leftMargin) } var itemX: CGFloat = leftMargin for sv in self.contentView.subviews { guard let item = sv as? KMToolbarItemView else { continue } let itemMaxX = itemX + item.itemWidth + Self.kHSpace if itemMaxX > width { // 隐藏 item.isHidden = true self._invisibleItems.append(item) } else { // 显示 item.isHidden = false } } return } // 中间完全显示不下 if self.centerView.superview != nil { self.centerView.mas_remakeConstraints { make in make?.center.mas_equalTo()(0) make?.width.mas_equalTo()(0) } } self.contentView.mas_remakeConstraints { make in make?.center.mas_equalTo()(0) make?.width.mas_equalTo()(0) } } private func _layoutSubViewsIfNeed() { if (self._contentView == nil) { return } let moreButtonWidth = 18.0 // let leftButtonWidth = 10.0 // let leftW = NSWidth(self.leftView.frame) // let contentW = NSWidth(self.contentView.frame) // let rightW = NSWidth(self.rightView.frame) let leftW = self._leftWidth let contentW = self._centerWidth let rightW = self._rightWidth // KMPrint("width: \(leftW), \(contentW), \(rightW)") // KMPrint("\(self.frame.size.width)") // self._right_layoutSubViewIfNeed() // self._center_layoutSubViewIfNeed() let width = NSWidth(self.bounds) let height = NSHeight(self.bounds) let leftMargin = Self.kLeftMargin let hSpace = Self.kHSpace let itemH: CGFloat = 40 var itemY: CGFloat = (height-itemH)*0.5 let contentLength = leftW + contentW + rightW if (contentLength <= 0) { return } self._invisibleItems.removeAll() if width >= contentLength { // 左 + 中 + 右 显示(全) self.moreButton.isHidden = true let offset = width - contentLength self.leftView.frame = NSMakeRect(0, 0, leftW, height) var itemX: CGFloat = leftMargin for sv in self.leftView.subviews { guard let item = sv as? KMToolbarItemView else { continue } item.isHidden = false itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace } if self.isMain() { self.centerView.frame = NSMakeRect(leftW, 0, contentW + offset, height) self.contentView.frame = NSMakeRect(offset*0.5, 0, contentW, height) } else { self.contentView.frame = NSMakeRect(offset*0.5, 0, contentW, height) } itemX = 0 for sv in self.contentView.subviews { guard let item = sv as? KMToolbarItemView else { continue } item.isHidden = false itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace } self.rightView.frame = NSMakeRect(leftW+contentW+offset, 0, rightW, height) itemX = 0 for sv in self.rightView.subviews { guard let item = sv as? KMToolbarItemView else { continue } item.isHidden = false itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace } return } else { // 显示更多 self.moreButton.isHidden = false self.moreButton.mas_remakeConstraints { make in make?.top.bottom().equalTo()(0) make?.width.mas_equalTo()(moreButtonWidth) make?.right.mas_equalTo()(0) } if width >= leftW + contentW { // 左 + 中 显示(全) 右显示不全 let offset = width - leftW - contentW self.leftView.frame = NSMakeRect(0, 0, leftW, height) var itemX: CGFloat = leftMargin for sv in self.leftView.subviews { guard let item = sv as? KMToolbarItemView else { continue } item.isHidden = false itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace } if self.isMain() { self.centerView.frame = NSMakeRect(leftW, 0, contentW, height) self.contentView.frame = NSMakeRect(0, 0, contentW + offset, height) } else { self.contentView.frame = NSMakeRect(leftW, 0, contentW, height) } itemX = 0 for sv in self.contentView.subviews { guard let item = sv as? KMToolbarItemView else { continue } item.isHidden = false itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace } self.rightView.frame = NSMakeRect(leftW+contentW, 0, offset, height) itemX = 0 for sv in self.rightView.subviews { guard let item = sv as? KMToolbarItemView else { continue } itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace let itemMinX = NSMinX(self.rightView.frame) + itemX if itemMinX <= width { item.isHidden = false } else { item.isHidden = true self._invisibleItems.append(item) } } return } else if width >= leftW { // 左 显示(全) 中 显示不全 右不显示 let offset = width - leftW self.leftView.frame = NSMakeRect(0, 0, leftW, height) var itemX: CGFloat = leftMargin for sv in self.leftView.subviews { guard let item = sv as? KMToolbarItemView else { continue } itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace item.isHidden = false } if self.isMain() { self.centerView.frame = NSMakeRect(leftW, 0, offset, height) self.contentView.frame = NSMakeRect(0, 0, offset, height) } else { self.contentView.frame = NSMakeRect(leftW, 0, offset, height) } itemX = 0 for sv in self.contentView.subviews { guard let item = sv as? KMToolbarItemView else { continue } itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace let itemMinX = leftW + itemX if itemMinX <= width { item.isHidden = false } else { item.isHidden = true self._invisibleItems.append(item) } } self.rightView.frame = NSMakeRect(leftW+contentW, 0, 0, height) itemX = 0 for sv in self.rightView.subviews { guard let item = sv as? KMToolbarItemView else { continue } itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace item.isHidden = true self._invisibleItems.append(item) } return } else { // 左显示不全 中 + 右 不显示 let offset = width - leftW self.leftView.frame = NSMakeRect(0, 0, offset, height) var itemX: CGFloat = leftMargin for sv in self.leftView.subviews { guard let item = sv as? KMToolbarItemView else { continue } itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace if itemX <= width { item.isHidden = false } else { item.isHidden = true self._invisibleItems.append(item) } } if self.isMain() { self.centerView.frame = NSMakeRect(leftW, 0, 0, height) self.contentView.frame = NSMakeRect(0, 0, 0, height) } else { self.contentView.frame = NSMakeRect(leftW, 0, 0, height) } itemX = 0 for sv in self.contentView.subviews { guard let item = sv as? KMToolbarItemView else { continue } itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace item.isHidden = true self._invisibleItems.append(item) } self.rightView.frame = NSMakeRect(leftW+contentW, 0, 0, height) itemX = 0 for sv in self.rightView.subviews { guard let item = sv as? KMToolbarItemView else { continue } itemY = (height-item.itemHeight)*0.5 item.frame = NSMakeRect(itemX, itemY, item.itemWidth, item.itemHeight) itemX += item.itemWidth + hSpace item.isHidden = true self._invisibleItems.append(item) } return } } #if false if contentLength <= self.frame.size.width { // 能正常显示 if self.leftView.superview != nil { self.leftView.mas_remakeConstraints { make in make?.top.bottom().equalTo()(self) make?.left.equalTo()(self) make?.width.mas_equalTo()(leftW) } } if self.centerView.superview != nil { self.centerView.mas_remakeConstraints { make in make?.center.equalTo()(self) } } else { self.contentView.mas_remakeConstraints { make in make?.center.equalTo()(self) } } if self.rightView.superview != nil { self.rightView.mas_remakeConstraints { make in make?.top.bottom().equalTo()(self) make?.right.mas_equalTo()(0) make?.width.mas_equalTo()(rightW) // if self.centerView.superview != nil { // make?.left.equalTo()(self.centerView.mas_right) // } else { // make?.left.equalTo()(self.contentView.mas_right) // } } } else { } self.moreButton.isHidden = true } else { // 需要显示更多 self.moreButton.isHidden = false self.moreButton.mas_remakeConstraints { make in make?.top.bottom().equalTo()(0) make?.width.mas_equalTo()(moreButtonWidth) make?.right.mas_equalTo()(0) } if self.frame.size.width > leftW + contentW { // 能显示 left + content, right 显示不下 if self.centerView.superview != nil { self.centerView.mas_remakeConstraints { make in make?.center.equalTo()(self) } } else { self.contentView.mas_remakeConstraints { make in make?.center.equalTo()(self) } } if self.rightView.superview != nil { self.rightView.mas_remakeConstraints { make in make?.top.bottom().equalTo()(self) make?.right.mas_equalTo()(0) make?.width.mas_equalTo()(contentLength-self.frame.size.width) // if self.centerView.superview != nil { // make?.left.equalTo()(self.centerView.mas_right) // } else { // make?.left.equalTo()(self.contentView.mas_right) // } } } } } #endif // var frame = self.contentView.frame #if false let allItems: [KMToolbarItemView] = self.leftView.subviews as! [KMToolbarItemView] + self.items + self.rightView.subviews as! [KMToolbarItemView] var count = allItems.count var idx = count-1 for item in allItems.reversed() { let tempFrame = item.superview?.convert(item.frame, to: item.superview!.superview) ?? .zero if (CGRectGetMaxX(tempFrame) <= self.frame.size.width) { count = idx break } idx -= 1 } if (count >= allItems.count) { return } // frame.size.width = CGRectGetMaxX(self.items[count].frame) if (count == allItems.count-1) { // frame.origin.x = (self.frame.size.width-frame.size.width)/2.0; // self.moreButton.isHidden = true self._visibleItems = allItems self._invisibleItems = [] for item in allItems { item.isHidden = false } // self.contentView.mas_remakeConstraints { make in // make?.centerX.equalTo()(0) // make?.centerY.equalTo()(0) // } } else { //// frame.origin.x = leftButtonWidth // self.moreButton.mas_makeConstraints { make in // make?.top.bottom().equalTo()(0) // make?.width.offset()(moreButtonWidth) // make?.right.equalTo()(self.mas_right)?.offset()(0) // } // self.moreButton.isHidden = false self._visibleItems.removeAll() for i in 0 ... count+1 { self._visibleItems.append(allItems[i]) } // self.contentView.mas_remakeConstraints { make in // make?.left.equalTo()(self.mas_left)?.offset()(10) // make?.centerY.equalTo()(0) // } } self._invisibleItems.removeAll() for item in allItems.reversed() { let tempFrame = item.superview?.convert(item.frame, to: item.superview!.superview) ?? .zero KMPrint("\(item.itemIdentifier): \(tempFrame)") if (CGRectGetMaxX(tempFrame) <= self.frame.size.width) { item.isHidden = false } else { item.isHidden = true self._invisibleItems.append(item) } } if self._invisibleItems.isEmpty { KMPrint("无更多菜单") } else { for item in self._invisibleItems { KMPrint(item.itemIdentifier) } } #endif // KMPrint(self._visibleItems) } func hasLeft() -> Bool { if let items = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self) { return items.count > 0 } return false } func hasRight() -> Bool { if let items = self.delegate?.toolbarRightDefaultItemIdentifiers?(self) { return items.count > 0 } return false } func hasCenter() -> Bool { if let items = self.delegate?.toolbarDefaultItemIdentifiers?(self) { return items.count > 0 } return false } func isMain() -> Bool { if self.contentView.superview == self { return false } return true } override func resizeSubviews(withOldSize oldSize: NSSize) { super.resizeSubviews(withOldSize: oldSize) self._layoutSubViewsIfNeed() } func reloadData() { let leftitemIdentifiers = self.delegate?.toolbarLeftDefaultItemIdentifiers?(self) ?? [] if (leftitemIdentifiers.count > 0) { self._updateMainView() } else { self._updateView() } self.resizeSubviews(withOldSize: self.frame.size) } func itemForItemIdentifier(_ itemIdentifier: String) -> KMToolbarItemView? { for item in self.items { if (item.itemIdentifier == itemIdentifier) { return item } } return nil } @objc func toolbarCustomChangeNotification() { self.reloadData() } }