ComponentCheckBox.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // ComponentCheckBox.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by Niehaoyu on 2024/8/29.
  6. //
  7. import Cocoa
  8. import AppKit
  9. public class ComponentCheckBox: ComponentBaseXibView {
  10. @IBOutlet var contendBox: NSBox!
  11. @IBOutlet var checkboxImage: NSImageView!
  12. @IBOutlet var titleLabel: NSTextField!
  13. @IBOutlet var helpTooltips: ComponentToolTipsHelp!
  14. // MARK: Private Property
  15. private var _properties : ComponentCheckBoxProperty = ComponentCheckBoxProperty()
  16. private var action: Selector? // 点击事件
  17. private weak var target: AnyObject? // 对象目标
  18. public override func draw(_ dirtyRect: NSRect) {
  19. super.draw(dirtyRect)
  20. // Drawing code here.
  21. }
  22. // MARK: 初始化
  23. deinit {
  24. NotificationCenter.default.removeObserver(self)
  25. }
  26. public required init?(coder decoder: NSCoder) {
  27. super.init(coder: decoder)
  28. }
  29. override init(frame frameRect: NSRect) {
  30. super.init(frame: frameRect)
  31. }
  32. public override func awakeFromNib() {
  33. super.awakeFromNib()
  34. contendBox.fillColor = .clear
  35. contendBox.borderWidth = 0
  36. }
  37. // MARK: - Set & Get
  38. public var properties : ComponentCheckBoxProperty {
  39. get {
  40. return _properties
  41. }
  42. set {
  43. _properties = newValue
  44. ComponentLibrary.shared.configCheckBoxComponent(properties: _properties)
  45. reloadData()
  46. }
  47. }
  48. public func reloadData() {
  49. setupUI()
  50. refreshUI()
  51. }
  52. public func setTarget(_ target: AnyObject?, action: Selector?) {
  53. self.target = target!
  54. self.action = action!
  55. }
  56. func setupUI() {
  57. if (properties.text) != nil {
  58. titleLabel.stringValue = properties.text ?? ""
  59. titleLabel.textColor = properties.propertyInfo.textColor
  60. if properties.isDisabled == true {
  61. titleLabel.textColor = properties.propertyInfo.textColor_dis
  62. }
  63. titleLabel.font = properties.propertyInfo.textFont
  64. }
  65. if properties.showhelp == true {
  66. helpTooltips.isHidden = false
  67. } else {
  68. helpTooltips.isHidden = true
  69. }
  70. if (properties.text) != nil {
  71. titleLabel.isHidden = false
  72. } else {
  73. titleLabel.isHidden = true
  74. }
  75. }
  76. func refreshUI() {
  77. var imageName = "checkbox_default_nor"
  78. if properties.checkboxType == .normal {
  79. if properties.state == .normal {
  80. imageName = "checkbox_default_nor"
  81. } else if properties.state == .hover {
  82. imageName = "checkbox_default_hov"
  83. }
  84. if properties.isDisabled == true {
  85. imageName = "checkbox_default_dis"
  86. }
  87. } else if properties.checkboxType == .indeterminate {
  88. if properties.state == .normal {
  89. imageName = "checkbox_Indet_nor"
  90. } else if properties.state == .hover {
  91. imageName = "checkbox_Indet_hov"
  92. }
  93. if properties.isDisabled == true {
  94. imageName = "checkbox_Indet_dis"
  95. }
  96. } else if properties.checkboxType == .selected {
  97. if properties.state == .normal {
  98. imageName = "checkbox_sel_nor"
  99. } else if properties.state == .hover {
  100. imageName = "checkbox_sel_hov"
  101. }
  102. if properties.isDisabled == true {
  103. imageName = "checkbox_sel_dis"
  104. }
  105. }
  106. checkboxImage.image = ComponentLibrary.shared.image(forResource: imageName)
  107. }
  108. //MARK: - MouseEvent
  109. public override func mouseEntered(with event: NSEvent) {
  110. super.mouseEntered(with: event)
  111. if properties.isDisabled == false {
  112. properties.state = .hover
  113. }
  114. refreshUI()
  115. }
  116. public override func mouseMoved(with event: NSEvent) {
  117. super.mouseMoved(with: event)
  118. }
  119. public override func mouseExited(with event: NSEvent) {
  120. super.mouseExited(with: event)
  121. if properties.isDisabled == false {
  122. properties.state = .normal
  123. }
  124. refreshUI()
  125. }
  126. public override func mouseDown(with event: NSEvent) {
  127. // super.mouseDown(with: event)
  128. }
  129. public override func mouseUp(with event: NSEvent) {
  130. super.mouseUp(with: event)
  131. var eventContinue = true
  132. let point = convert(event.locationInWindow, from: nil)
  133. if helpTooltips.isHidden == false {
  134. if CGRectContainsPoint(helpTooltips.frame, point) {
  135. eventContinue = false
  136. }
  137. }
  138. if properties.isDisabled == false && eventContinue == true {
  139. if properties.isDisabled == false {
  140. properties.state = .normal
  141. }
  142. if properties.checkboxType != .selected {
  143. properties.checkboxType = .selected
  144. } else {
  145. properties.checkboxType = .normal
  146. }
  147. refreshUI()
  148. if let target = target, let action = action {
  149. _ = target.perform(action, with: self)
  150. }
  151. }
  152. }
  153. }