ComponentRadio.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // ComponentRadio.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by Niehaoyu on 2024/8/29.
  6. //
  7. import Cocoa
  8. import AppKit
  9. public class ComponentRadio: 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.configRadioComponent(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 = "radio_default_nor"
  78. if properties.checkboxType == .normal {
  79. if properties.state == .normal {
  80. imageName = "radio_default_nor"
  81. } else if properties.state == .hover {
  82. imageName = "radio_default_hov"
  83. }
  84. if properties.isDisabled == true {
  85. imageName = "radio_default_dis"
  86. }
  87. } else if properties.checkboxType == .selected {
  88. if properties.state == .normal {
  89. imageName = "radio_sel_nor"
  90. } else if properties.state == .hover {
  91. imageName = "radio_sel_hov"
  92. }
  93. if properties.isDisabled == true {
  94. imageName = "radio_sel_dis"
  95. }
  96. }
  97. checkboxImage.image = ComponentLibrary.shared.image(forResource: imageName)
  98. }
  99. //MARK: - MouseEvent
  100. public override func mouseEntered(with event: NSEvent) {
  101. super.mouseEntered(with: event)
  102. if properties.isDisabled == false {
  103. properties.state = .hover
  104. }
  105. refreshUI()
  106. }
  107. public override func mouseMoved(with event: NSEvent) {
  108. super.mouseMoved(with: event)
  109. }
  110. public override func mouseExited(with event: NSEvent) {
  111. super.mouseExited(with: event)
  112. if properties.isDisabled == false {
  113. properties.state = .normal
  114. }
  115. refreshUI()
  116. }
  117. public override func mouseDown(with event: NSEvent) {
  118. super.mouseDown(with: event)
  119. }
  120. public override func mouseUp(with event: NSEvent) {
  121. super.mouseUp(with: event)
  122. var eventContinue = true
  123. let point = convert(event.locationInWindow, from: nil)
  124. if helpTooltips.isHidden == false {
  125. if CGRectContainsPoint(helpTooltips.frame, point) {
  126. eventContinue = false
  127. }
  128. }
  129. if properties.isDisabled == false && eventContinue == true {
  130. if properties.isDisabled == false {
  131. properties.state = .normal
  132. }
  133. if properties.checkboxType != .selected {
  134. properties.checkboxType = .selected
  135. } else {
  136. properties.checkboxType = .normal
  137. }
  138. refreshUI()
  139. if let target = target, let action = action {
  140. _ = target.perform(action, with: self)
  141. }
  142. }
  143. }
  144. }