KMCustomButtonPopMenuViewController.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // KMCustomButtonPopMenuViewController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2023/10/7.
  6. //
  7. import Cocoa
  8. import Masonry
  9. @objc protocol KMCustomButtonPopMenuViewControllerDataSources: AnyObject {
  10. // pop框有多少行
  11. func numberOfLine() -> Int
  12. // pop框某行显示的文字
  13. func stringForLine(at index: Int) -> String?
  14. // 某行是否需要下划线
  15. func needInsertSeperateLine(at index: Int) -> Bool
  16. // 某行是否需要选取
  17. func needHightLightLine(at index: Int) -> Bool
  18. // 某行对应的图片
  19. func imageForLine(at index: Int) -> NSImage?
  20. // 某行是否可用
  21. func itemEnable(at index: Int) -> Bool
  22. }
  23. @objc protocol KMCustomButtonPopMenuViewControllerDelegate: AnyObject {
  24. func customViewButtonPopDidSelectIndex(_ index: Int)
  25. }
  26. @objc class KMCustomButtonPopMenuViewController: NSViewController {
  27. weak var dataSources: KMCustomButtonPopMenuViewControllerDataSources?
  28. weak var delegate: KMCustomButtonPopMenuViewControllerDelegate?
  29. @objc override func viewDidLoad() {
  30. super.viewDidLoad()
  31. // Do view setup here.
  32. var numberOfLine = 0
  33. if self.responds(to: Selector.init(("numberOfLine"))) {
  34. numberOfLine = self.dataSources!.numberOfLine()
  35. }
  36. if numberOfLine < 1 {
  37. return
  38. }
  39. var referenceView: NSView? = self.view
  40. for i in 0..<numberOfLine {
  41. var image: NSImage?
  42. var title: String?
  43. if self.responds(to: Selector.init(("imageForLineAtIndex:"))) {
  44. image = self.dataSources?.imageForLine(at: i)
  45. title = self.dataSources?.stringForLine(at: i)
  46. }
  47. if self.responds(to: Selector.init(("stringForLineAtIndex:"))) {
  48. title = self.dataSources?.stringForLine(at: i)
  49. }
  50. var v: KMPopMenuButton
  51. if let image1 = image {
  52. v = KMPopMenuButton(title: title!, image: image1, target: self, action: #selector(buttonClicked(_:)))
  53. } else {
  54. v = KMPopMenuButton(title: title!, target: self, action: #selector(buttonClicked(_:)))
  55. }
  56. v.isBordered = false
  57. v.tag = i
  58. if self.responds(to: Selector.init(("itemEnableAtIndex:"))) {
  59. v.isEnabled = ((self.dataSources?.itemEnable(at: i)) != nil)
  60. }
  61. self.view.addSubview(v)
  62. v.mas_makeConstraints { make in
  63. if let viewBool = referenceView?.isEqual(self.view) {
  64. make?.top.equalTo()(referenceView)?.offset()(CGFloat(10))
  65. } else {
  66. make?.top.equalTo()(referenceView?.mas_bottom)?.offset()(CGFloat(2))
  67. }
  68. make?.left.right().equalTo()(self.view)
  69. make?.height.offset()(24)
  70. }
  71. referenceView = v
  72. if self.responds(to: Selector.init(("needHightLightLineAtIndex:"))) {
  73. if let needBool = self.dataSources?.needHightLightLine(at: i) {
  74. v.state = .on
  75. }
  76. }
  77. if self.responds(to: Selector.init(("needInsertSeperateLineAtIndex:"))) {
  78. if let needBool = self.dataSources?.needInsertSeperateLine(at: i) {
  79. let box = NSBox(frame: .zero)
  80. box.boxType = .separator
  81. self.view.addSubview(box)
  82. box.mas_makeConstraints { make in
  83. make?.top.equalTo()(referenceView?.mas_bottom)?.offset()(2)
  84. make?.left.equalTo()(self.view)?.offset()(CGFloat(21))
  85. make?.centerX.equalTo()(self.view)
  86. }
  87. referenceView = box
  88. }
  89. }
  90. if i == numberOfLine - 1 {
  91. referenceView?.mas_updateConstraints({ make in
  92. make?.bottom.equalTo()(self.view)?.offset()(-10)
  93. })
  94. }
  95. }
  96. }
  97. @objc func buttonClicked(_ sender: NSButton) {
  98. if self.responds(to: Selector.init(("customViewButtonPopDidSelectIndex:"))) {
  99. self.delegate!.customViewButtonPopDidSelectIndex(sender.tag)
  100. }
  101. }
  102. }