ComponentRadio.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 override var toolTip: String? {
  39. didSet {
  40. if properties.showhelp {
  41. helpTooltips.toolTip = toolTipString
  42. toolTipString = nil
  43. }
  44. }
  45. }
  46. public var properties : ComponentCheckBoxProperty {
  47. get {
  48. return _properties
  49. }
  50. set {
  51. _properties = newValue
  52. ComponentLibrary.shared.configRadioComponent(properties: _properties)
  53. reloadData()
  54. }
  55. }
  56. public func reloadData() {
  57. setupUI()
  58. refreshUI()
  59. }
  60. public func setTarget(_ target: AnyObject?, action: Selector?) {
  61. self.target = target!
  62. self.action = action!
  63. }
  64. func setupUI() {
  65. if (properties.text) != nil {
  66. titleLabel.stringValue = properties.text ?? ""
  67. titleLabel.textColor = properties.propertyInfo.textColor
  68. if properties.isDisabled == true {
  69. titleLabel.textColor = properties.propertyInfo.textColor_dis
  70. }
  71. titleLabel.font = properties.propertyInfo.textFont
  72. }
  73. if properties.showhelp == true {
  74. helpTooltips.isHidden = false
  75. } else {
  76. helpTooltips.isHidden = true
  77. }
  78. if (properties.text) != nil {
  79. titleLabel.isHidden = false
  80. } else {
  81. titleLabel.isHidden = true
  82. }
  83. }
  84. func refreshUI() {
  85. var imageName = "radio_default_nor"
  86. if properties.checkboxType == .normal {
  87. if properties.state == .normal {
  88. imageName = "radio_default_nor"
  89. } else if properties.state == .hover {
  90. imageName = "radio_default_hov"
  91. }
  92. if properties.isDisabled == true {
  93. imageName = "radio_default_dis"
  94. }
  95. } else if properties.checkboxType == .selected {
  96. if properties.state == .normal {
  97. imageName = "radio_sel_nor"
  98. } else if properties.state == .hover {
  99. imageName = "radio_sel_hov"
  100. }
  101. if properties.isDisabled == true {
  102. imageName = "radio_sel_dis"
  103. }
  104. }
  105. checkboxImage.image = ComponentLibrary.shared.image(forResource: imageName)
  106. }
  107. //MARK: - MouseEvent
  108. public override func mouseEntered(with event: NSEvent) {
  109. super.mouseEntered(with: event)
  110. if properties.isDisabled == false {
  111. properties.state = .hover
  112. }
  113. refreshUI()
  114. }
  115. public override func mouseMoved(with event: NSEvent) {
  116. super.mouseMoved(with: event)
  117. }
  118. public override func mouseExited(with event: NSEvent) {
  119. super.mouseExited(with: event)
  120. if properties.isDisabled == false {
  121. properties.state = .normal
  122. }
  123. refreshUI()
  124. }
  125. public override func mouseDown(with event: NSEvent) {
  126. // super.mouseDown(with: event)
  127. }
  128. public override func mouseUp(with event: NSEvent) {
  129. super.mouseUp(with: event)
  130. var eventContinue = true
  131. let point = convert(event.locationInWindow, from: nil)
  132. if helpTooltips.isHidden == false {
  133. if CGRectContainsPoint(helpTooltips.frame, point) {
  134. eventContinue = false
  135. }
  136. }
  137. if properties.isDisabled == false && eventContinue == true {
  138. if properties.isDisabled == false {
  139. properties.state = .normal
  140. }
  141. if properties.checkboxType != .selected {
  142. properties.checkboxType = .selected
  143. }
  144. refreshUI()
  145. if let target = target, let action = action {
  146. _ = target.perform(action, with: self)
  147. }
  148. }
  149. }
  150. }