123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122 |
- //
- // 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()
- }
- }
|