123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- //
- // ComponentRadio.swift
- // KMComponentLibrary
- //
- // Created by Niehaoyu on 2024/8/29.
- //
- import Cocoa
- import AppKit
- public class ComponentRadio: ComponentBaseXibView {
- @IBOutlet var contendBox: NSBox!
- @IBOutlet var checkboxImage: NSImageView!
- @IBOutlet var titleLabel: NSTextField!
- @IBOutlet var helpTooltips: ComponentToolTipsHelp!
-
- // MARK: Private Property
- private var _properties : ComponentCheckBoxProperty = ComponentCheckBoxProperty()
-
- private var action: Selector? // 点击事件
- private weak var target: AnyObject? // 对象目标
-
- public override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
-
- // MARK: 初始化
- deinit {
- NotificationCenter.default.removeObserver(self)
-
- }
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
-
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- }
-
- public override func awakeFromNib() {
- super.awakeFromNib()
-
- contendBox.fillColor = .clear
- contendBox.borderWidth = 0
-
- }
-
- // MARK: - Set & Get
-
- public override var toolTip: String? {
- didSet {
- if properties.showhelp {
-
- helpTooltips.toolTip = toolTipString
-
- toolTipString = nil
- }
- }
- }
-
- public var properties : ComponentCheckBoxProperty {
- get {
- return _properties
- }
- set {
- _properties = newValue
-
- ComponentLibrary.shared.configRadioComponent(properties: _properties)
-
- reloadData()
- }
- }
-
- public func reloadData() {
-
- setupUI()
-
- refreshUI()
- }
-
- public func setTarget(_ target: AnyObject?, action: Selector?) {
- self.target = target!
- self.action = action!
- }
-
- func setupUI() {
-
- if (properties.text) != nil {
- titleLabel.stringValue = properties.text ?? ""
- titleLabel.textColor = properties.propertyInfo.textColor
- if properties.isDisabled == true {
- titleLabel.textColor = properties.propertyInfo.textColor_dis
- }
- titleLabel.font = properties.propertyInfo.textFont
- }
-
- if properties.showhelp == true {
- helpTooltips.isHidden = false
- } else {
- helpTooltips.isHidden = true
- }
-
- if (properties.text) != nil {
- titleLabel.isHidden = false
- } else {
- titleLabel.isHidden = true
- }
-
- }
-
- func refreshUI() {
- var imageName = "radio_default_nor"
- if properties.checkboxType == .normal {
- if properties.state == .normal {
- imageName = "radio_default_nor"
- } else if properties.state == .hover {
- imageName = "radio_default_hov"
- }
- if properties.isDisabled == true {
- imageName = "radio_default_dis"
- }
- } else if properties.checkboxType == .selected {
- if properties.state == .normal {
- imageName = "radio_sel_nor"
- } else if properties.state == .hover {
- imageName = "radio_sel_hov"
- }
- if properties.isDisabled == true {
- imageName = "radio_sel_dis"
- }
- }
- checkboxImage.image = ComponentLibrary.shared.image(forResource: imageName)
-
- }
-
-
- //MARK: - MouseEvent
- public override func mouseEntered(with event: NSEvent) {
- super.mouseEntered(with: event)
-
- if properties.isDisabled == false {
- properties.state = .hover
- }
- refreshUI()
- }
-
- public override func mouseMoved(with event: NSEvent) {
- super.mouseMoved(with: event)
-
- }
-
- public override func mouseExited(with event: NSEvent) {
- super.mouseExited(with: event)
-
- if properties.isDisabled == false {
- properties.state = .normal
- }
-
- refreshUI()
- }
-
- public override func mouseDown(with event: NSEvent) {
- // super.mouseDown(with: event)
-
- }
-
- public override func mouseUp(with event: NSEvent) {
- super.mouseUp(with: event)
-
- var eventContinue = true
- let point = convert(event.locationInWindow, from: nil)
- if helpTooltips.isHidden == false {
- if CGRectContainsPoint(helpTooltips.frame, point) {
- eventContinue = false
- }
- }
-
- if properties.isDisabled == false && eventContinue == true {
- if properties.isDisabled == false {
- properties.state = .normal
- }
- if properties.checkboxType != .selected {
- properties.checkboxType = .selected
- }
- refreshUI()
-
- if let target = target, let action = action {
- _ = target.perform(action, with: self)
- }
- }
-
- }
-
- }
|