123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- //
- // KMCustomButtonPopMenuViewController.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2023/10/7.
- //
- import Cocoa
- import Masonry
- @objc protocol KMCustomButtonPopMenuViewControllerDataSources: AnyObject {
- // pop框有多少行
- func numberOfLine() -> Int
- // pop框某行显示的文字
- func stringForLine(at index: Int) -> String?
- // 某行是否需要下划线
- func needInsertSeperateLine(at index: Int) -> Bool
- // 某行是否需要选取
- func needHightLightLine(at index: Int) -> Bool
- // 某行对应的图片
- @objc optional func imageForLine(at index: Int) -> NSImage?
- // 某行是否可用
- func itemEnable(at index: Int) -> Bool
- }
- @objc protocol KMCustomButtonPopMenuViewControllerDelegate: AnyObject {
- func customViewButtonPopDidSelectIndex(_ index: Int)
- }
- @objc class KMCustomButtonPopMenuViewController: NSViewController {
- weak var dataSources: KMCustomButtonPopMenuViewControllerDataSources?
- weak var delegate: KMCustomButtonPopMenuViewControllerDelegate?
-
- // override var nibName: NSNib.Name? {
- // return "KMCustomButtonPopMenuViewController"
- // }
- @objc override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- var numberOfLine = 0
-
- if let data = self.dataSources?.numberOfLine() {
- numberOfLine = data
- }
- if numberOfLine < 1 {
- return
- }
-
- var referenceView: NSView? = self.view
-
- for i in 0..<numberOfLine {
- var image: NSImage?
- var title: String?
- if let data = self.dataSources?.imageForLine?(at: i) {
- image = data
- }
- if let data = self.dataSources?.stringForLine(at: i) {
- title = data
- }
-
- var v: KMPopMenuButton
- if let image1 = image {
- v = KMPopMenuButton(title: title!, image: image1, target: self, action: #selector(buttonClicked(_:)))
- v.imageHugsTitle = true
- } else {
- v = KMPopMenuButton(title: title!, target: self, action: #selector(buttonClicked(_:)))
- }
-
- v.isBordered = false
- v.tag = i
- v.setTitleColorWithColor(NSColor.labelColor, font: nil)
-
- if let data = self.dataSources?.itemEnable(at: i) {
- v.isEnabled = data
- }
-
- if let data = self.dataSources?.needInsertSeperateLine(at: i), data {
- let box = NSBox(frame: .zero)
- box.boxType = .separator
- self.view.addSubview(box)
-
- box.mas_makeConstraints { make in
- make?.top.equalTo()(referenceView?.mas_bottom)?.offset()(2)
- make?.left.equalTo()(self.view)?.offset()(CGFloat(21))
- make?.centerX.equalTo()(self.view)
- }
- referenceView = box
- } else {
- self.view.addSubview(v)
- v.mas_makeConstraints { make in
- if let viewBool = referenceView?.isEqual(self.view), viewBool {
- make?.top.equalTo()(referenceView)?.offset()(CGFloat(10))
- } else {
- make?.top.equalTo()(referenceView?.mas_bottom)?.offset()(CGFloat(2))
- }
- make?.left.right().equalTo()(self.view)
- make?.height.offset()(24)
- }
- referenceView = v
- }
-
- if self.responds(to: Selector.init(("needHightLightLineAtIndex:"))) {
- if let needBool = self.dataSources?.needHightLightLine(at: i) {
- v.state = .on
- }
- }
-
-
- if i == numberOfLine - 1 {
- referenceView?.mas_updateConstraints({ make in
- make?.bottom.equalTo()(self.view)?.offset()(-10)
- })
- }
- }
- }
-
- @objc func buttonClicked(_ sender: NSButton) {
- self.delegate?.customViewButtonPopDidSelectIndex(sender.tag)
- }
- }
|