KMCustomButtonPopMenuViewController.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // KMCustomButtonPopMenuViewController.swift
  3. // PDF Reader Pro
  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. @objc optional 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. // override var nibName: NSNib.Name? {
  30. // return "KMCustomButtonPopMenuViewController"
  31. // }
  32. @objc override func viewDidLoad() {
  33. super.viewDidLoad()
  34. // Do view setup here.
  35. var numberOfLine = 0
  36. if let data = self.dataSources?.numberOfLine() {
  37. numberOfLine = data
  38. }
  39. if numberOfLine < 1 {
  40. return
  41. }
  42. var referenceView: NSView? = self.view
  43. for i in 0..<numberOfLine {
  44. var image: NSImage?
  45. var title: String?
  46. if let data = self.dataSources?.imageForLine?(at: i) {
  47. image = data
  48. }
  49. if let data = self.dataSources?.stringForLine(at: i) {
  50. title = data
  51. }
  52. var v: KMPopMenuButton
  53. if let image1 = image {
  54. v = KMPopMenuButton(title: title!, image: image1, target: self, action: #selector(buttonClicked(_:)))
  55. v.imageHugsTitle = true
  56. } else {
  57. v = KMPopMenuButton(title: title!, target: self, action: #selector(buttonClicked(_:)))
  58. }
  59. v.isBordered = false
  60. v.tag = i
  61. v.setTitleColorWithColor(NSColor.labelColor, font: nil)
  62. if let data = self.dataSources?.itemEnable(at: i) {
  63. v.isEnabled = data
  64. }
  65. if let data = self.dataSources?.needInsertSeperateLine(at: i), data {
  66. let box = NSBox(frame: .zero)
  67. box.boxType = .separator
  68. self.view.addSubview(box)
  69. box.mas_makeConstraints { make in
  70. make?.top.equalTo()(referenceView?.mas_bottom)?.offset()(2)
  71. make?.left.equalTo()(self.view)?.offset()(CGFloat(21))
  72. make?.centerX.equalTo()(self.view)
  73. }
  74. referenceView = box
  75. } else {
  76. self.view.addSubview(v)
  77. v.mas_makeConstraints { make in
  78. if let viewBool = referenceView?.isEqual(self.view), viewBool {
  79. make?.top.equalTo()(referenceView)?.offset()(CGFloat(10))
  80. } else {
  81. make?.top.equalTo()(referenceView?.mas_bottom)?.offset()(CGFloat(2))
  82. }
  83. make?.left.right().equalTo()(self.view)
  84. make?.height.offset()(24)
  85. }
  86. referenceView = v
  87. }
  88. if self.responds(to: Selector.init(("needHightLightLineAtIndex:"))) {
  89. if let needBool = self.dataSources?.needHightLightLine(at: i) {
  90. v.state = .on
  91. }
  92. }
  93. if i == numberOfLine - 1 {
  94. referenceView?.mas_updateConstraints({ make in
  95. make?.bottom.equalTo()(self.view)?.offset()(-10)
  96. })
  97. }
  98. }
  99. }
  100. @objc func buttonClicked(_ sender: NSButton) {
  101. self.delegate?.customViewButtonPopDidSelectIndex(sender.tag)
  102. }
  103. }