KMCustomButtonPopMenuViewController.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. @objc override func viewDidLoad() {
  30. super.viewDidLoad()
  31. // Do view setup here.
  32. var numberOfLine = 0
  33. if let data = self.dataSources?.numberOfLine() {
  34. numberOfLine = data
  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 let data = self.dataSources?.imageForLine?(at: i) {
  44. image = data
  45. }
  46. if let data = self.dataSources?.stringForLine(at: i) {
  47. title = data
  48. }
  49. var v: KMPopMenuButton
  50. if let image1 = image {
  51. v = KMPopMenuButton(title: title!, image: image1, target: self, action: #selector(buttonClicked(_:)))
  52. v.imageHugsTitle = true
  53. } else {
  54. v = KMPopMenuButton(title: title!, target: self, action: #selector(buttonClicked(_:)))
  55. }
  56. v.isBordered = false
  57. v.tag = i
  58. v.setTitleColorWithColor(NSColor.labelColor, font: nil)
  59. if let data = self.dataSources?.itemEnable(at: i) {
  60. v.isEnabled = data
  61. }
  62. if let data = self.dataSources?.needInsertSeperateLine(at: i), data {
  63. let box = NSBox(frame: .zero)
  64. box.boxType = .separator
  65. self.view.addSubview(box)
  66. box.mas_makeConstraints { make in
  67. make?.top.equalTo()(referenceView?.mas_bottom)?.offset()(2)
  68. make?.left.equalTo()(self.view)?.offset()(CGFloat(21))
  69. make?.centerX.equalTo()(self.view)
  70. }
  71. referenceView = box
  72. } else {
  73. self.view.addSubview(v)
  74. v.mas_makeConstraints { make in
  75. if let viewBool = referenceView?.isEqual(self.view), viewBool {
  76. make?.top.equalTo()(referenceView)?.offset()(CGFloat(10))
  77. } else {
  78. make?.top.equalTo()(referenceView?.mas_bottom)?.offset()(CGFloat(2))
  79. }
  80. make?.left.right().equalTo()(self.view)
  81. make?.height.offset()(24)
  82. }
  83. referenceView = v
  84. }
  85. if self.responds(to: Selector.init(("needHightLightLineAtIndex:"))) {
  86. if let needBool = self.dataSources?.needHightLightLine(at: i) {
  87. v.state = .on
  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. self.delegate?.customViewButtonPopDidSelectIndex(sender.tag)
  99. }
  100. }