123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- //
- // KMCustomButtonPopMenuViewController.swift
- // PDF Master
- //
- // 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
- // 某行对应的图片
- 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?
- @objc override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- var numberOfLine = 0
- if self.responds(to: Selector.init(("numberOfLine"))) {
- numberOfLine = self.dataSources!.numberOfLine()
- }
- if numberOfLine < 1 {
- return
- }
-
- var referenceView: NSView? = self.view
-
- for i in 0..<numberOfLine {
- var image: NSImage?
- var title: String?
-
- if self.responds(to: Selector.init(("imageForLineAtIndex:"))) {
- image = self.dataSources?.imageForLine(at: i)
- title = self.dataSources?.stringForLine(at: i)
- }
- if self.responds(to: Selector.init(("stringForLineAtIndex:"))) {
- title = self.dataSources?.stringForLine(at: i)
- }
- var v: KMPopMenuButton
- if let image1 = image {
- v = KMPopMenuButton(title: title!, image: image1, target: self, action: #selector(buttonClicked(_:)))
- } else {
- v = KMPopMenuButton(title: title!, target: self, action: #selector(buttonClicked(_:)))
- }
-
- v.isBordered = false
- v.tag = i
-
- if self.responds(to: Selector.init(("itemEnableAtIndex:"))) {
- v.isEnabled = ((self.dataSources?.itemEnable(at: i)) != nil)
- }
-
- self.view.addSubview(v)
-
- v.mas_makeConstraints { make in
- if let viewBool = referenceView?.isEqual(self.view) {
- 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 self.responds(to: Selector.init(("needInsertSeperateLineAtIndex:"))) {
- if let needBool = self.dataSources?.needInsertSeperateLine(at: i) {
- 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
- }
- }
-
- if i == numberOfLine - 1 {
- referenceView?.mas_updateConstraints({ make in
- make?.bottom.equalTo()(self.view)?.offset()(-10)
- })
- }
- }
- }
-
- @objc func buttonClicked(_ sender: NSButton) {
- if self.responds(to: Selector.init(("customViewButtonPopDidSelectIndex:"))) {
- self.delegate!.customViewButtonPopDidSelectIndex(sender.tag)
- }
- }
- }
|