ComponentMenuItem.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // ComponentMenuItem.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by Niehaoyu on 2024/8/30.
  6. //
  7. import Cocoa
  8. import AppKit
  9. public let ComponentMenuItemUpdatedKey = "ComponentMenuItemUpdatedKey"
  10. public class ComponentMenuItem: ComponentBaseXibView {
  11. @IBOutlet var contendBox: NSBox!
  12. @IBOutlet var checkBox: ComponentCheckBox!
  13. @IBOutlet var leftIconImage: NSImageView!
  14. @IBOutlet var titleLabel: NSTextField!
  15. @IBOutlet var keyEquivalentLabel: NSTextField!
  16. @IBOutlet var righticonImage: NSImageView!
  17. @IBOutlet var separatorBox: NSBox!
  18. @IBOutlet var separatorDivider: ComponentDivider!
  19. @IBOutlet var headerBox: NSBox!
  20. @IBOutlet var headerLabel: NSTextField!
  21. @IBOutlet var titleLabelLeftConst: NSLayoutConstraint!
  22. // MARK: Private Property
  23. private var _properties : ComponentMenuitemProperty = ComponentMenuitemProperty()
  24. private var action: Selector? // 点击事件
  25. private weak var target: AnyObject? // 对象目标
  26. var mouseHandle: ((_ view: ComponentMenuItem, _ mouseState: ComponentMouseState, _ event: NSEvent) -> Void)?
  27. // MARK: 初始化
  28. deinit {
  29. NotificationCenter.default.removeObserver(self)
  30. }
  31. public required init?(coder decoder: NSCoder) {
  32. super.init(coder: decoder)
  33. }
  34. override init(frame frameRect: NSRect) {
  35. super.init(frame: frameRect)
  36. }
  37. public override func awakeFromNib() {
  38. super.awakeFromNib()
  39. NotificationCenter.default.addObserver(self, selector: #selector(propertyUpdatedNoti(_:)), name: NSNotification.Name(rawValue: ComponentMenuItemUpdatedKey), object: nil)
  40. }
  41. @objc func propertyUpdatedNoti(_ notification: Notification) {
  42. if let objectID = notification.object as? String, objectID == properties.identifier {
  43. setupUI()
  44. refreshUI()
  45. }
  46. }
  47. public var properties : ComponentMenuitemProperty {
  48. get {
  49. return _properties
  50. }
  51. set {
  52. _properties = newValue
  53. ComponentLibrary.shared.configMenuItemComponent(properties: _properties)
  54. setupUI()
  55. refreshUI()
  56. }
  57. }
  58. //MARK: - SetupUI
  59. func setupUI() {
  60. contendBox.isHidden = true
  61. separatorBox.isHidden = true
  62. headerBox.isHidden = true
  63. if properties.type == .normal {
  64. contendBox.isHidden = false
  65. } else if properties.type == .divider {
  66. separatorBox.isHidden = false
  67. separatorBox.borderWidth = 0
  68. separatorBox.borderColor = NSColor.clear
  69. separatorBox.fillColor = NSColor.clear
  70. let property: ComponentDividerProperty = ComponentDividerProperty(type: .horizontal, dash: false)
  71. separatorDivider.properties = property
  72. } else if properties.type == .header {
  73. headerBox.isHidden = false
  74. headerBox.fillColor = NSColor.clear
  75. headerLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/3")
  76. headerLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  77. }
  78. let checkboxProperties: ComponentCheckBoxProperty = ComponentCheckBoxProperty.init(size: .m,
  79. state: .normal,
  80. isDisabled: properties.isDisabled,
  81. showhelp: false,
  82. text: nil,
  83. checkboxType: .normal)
  84. checkBox.properties = checkboxProperties
  85. checkBox.properties.isDisabled = properties.isDisabled
  86. checkBox.setTarget(self, action: #selector(checkBoxAction(_:)))
  87. checkBox.reloadData()
  88. if properties.lefticon != nil {
  89. leftIconImage.isHidden = false
  90. leftIconImage.image = properties.lefticon
  91. titleLabelLeftConst.constant = 40
  92. } else {
  93. leftIconImage.isHidden = true
  94. titleLabelLeftConst.constant = 16
  95. }
  96. if properties.subPropertys.isEmpty == false {
  97. properties.righticon = ComponentLibrary.shared.image(forResource: "arrowRight")
  98. }
  99. if properties.righticon != nil {
  100. righticonImage.image = properties.righticon
  101. righticonImage.isHidden = false
  102. } else {
  103. righticonImage.isHidden = true
  104. }
  105. if properties.keyEquivalent != nil &&
  106. properties.keyEquivalent?.isEmpty == false {
  107. keyEquivalentLabel.isHidden = false
  108. } else {
  109. keyEquivalentLabel.isHidden = true
  110. }
  111. if properties.multipleSelect == true {
  112. checkBox.isHidden = false
  113. leftIconImage.isHidden = true
  114. righticonImage.isHidden = true
  115. titleLabelLeftConst.constant = 40
  116. } else {
  117. checkBox.isHidden = true
  118. }
  119. properties.propertyInfo.viewWidth = 200
  120. titleLabel.stringValue = properties.text
  121. headerLabel.stringValue = properties.text
  122. }
  123. func refreshUI() {
  124. if properties.itemSelected == true {
  125. checkBox.properties.checkboxType = .selected
  126. checkBox.refreshUI()
  127. } else {
  128. checkBox.properties.checkboxType = .normal
  129. checkBox.refreshUI()
  130. }
  131. contendBox.borderWidth = 0
  132. contendBox.borderColor = NSColor.clear
  133. var fillColor = properties.propertyInfo.color_nor
  134. var textColor = properties.propertyInfo.textColor
  135. if properties.state == .hover {
  136. fillColor = properties.propertyInfo.color_hov
  137. }
  138. if properties.itemSelected == true {
  139. fillColor = properties.propertyInfo.color_active
  140. }
  141. if properties.isDisabled == true {
  142. fillColor = properties.propertyInfo.color_dis
  143. textColor = properties.propertyInfo.textColor_dis
  144. }
  145. contendBox.fillColor = fillColor
  146. titleLabel.textColor = textColor
  147. titleLabel.font = properties.propertyInfo.textFont
  148. keyEquivalentLabel.font = properties.propertyInfo.textFont
  149. keyEquivalentLabel.textColor = properties.propertyInfo.keyEquivalent_textColor
  150. keyEquivalentLabel.stringValue = properties.keyEquivalent ?? ""
  151. }
  152. public func setTarget(_ target: AnyObject?, action: Selector?) {
  153. self.target = target!
  154. self.action = action!
  155. }
  156. //MARK: - Action
  157. @objc func checkBoxAction(_ sender: ComponentCheckBox) {
  158. if checkBox.properties.checkboxType == .selected {
  159. properties.itemSelected = true
  160. } else {
  161. properties.itemSelected = false
  162. }
  163. refreshUI()
  164. if let target = target, let action = action {
  165. _ = target.perform(action, with: self)
  166. }
  167. }
  168. //MARK: - MouseEvent
  169. public override func mouseEntered(with event: NSEvent) {
  170. super.mouseEntered(with: event)
  171. if properties.isDisabled == false &&
  172. properties.itemSelected == false {
  173. properties.state = .hover
  174. refreshUI()
  175. }
  176. guard let callBack = mouseHandle else {
  177. return
  178. }
  179. callBack(self, .enter, event)
  180. }
  181. public override func mouseMoved(with event: NSEvent) {
  182. super.mouseMoved(with: event)
  183. guard let callBack = mouseHandle else {
  184. return
  185. }
  186. callBack(self, .move, event)
  187. }
  188. public override func mouseExited(with event: NSEvent) {
  189. super.mouseExited(with: event)
  190. if properties.isDisabled == false &&
  191. properties.itemSelected == false {
  192. properties.state = .normal
  193. refreshUI()
  194. }
  195. guard let callBack = mouseHandle else {
  196. return
  197. }
  198. callBack(self, .exit, event)
  199. }
  200. public override func mouseDown(with event: NSEvent) {
  201. super.mouseDown(with: event)
  202. var clickEnable = true
  203. if properties.type == .normal {
  204. let point = convert(event.locationInWindow, from: nil)
  205. if CGRectContainsPoint(checkBox.frame, point) {
  206. clickEnable = false
  207. }
  208. }
  209. if properties.isDisabled == false &&
  210. clickEnable == true {
  211. if properties.itemSelected == true {
  212. properties.itemSelected = false
  213. } else {
  214. properties.itemSelected = true
  215. }
  216. refreshUI()
  217. }
  218. }
  219. public override func mouseUp(with event: NSEvent) {
  220. super.mouseUp(with: event)
  221. print(self.className, "clicked", NSDate())
  222. if properties.isDisabled == false &&
  223. properties.type == .normal {
  224. if let target = target, let action = action {
  225. _ = target.perform(action, with: self)
  226. }
  227. }
  228. }
  229. }