// // CheckBoxVC.swift // KMComponentLibrary // // Created by Niehaoyu on 2024/8/29. // import Cocoa import KMComponentLibrary class CheckBoxVC: NSViewController { @IBOutlet weak var checkBox: ComponentCheckBox! @IBOutlet weak var radio: ComponentRadio! @IBOutlet weak var typeBox: NSComboBox! @IBOutlet weak var sizeBox: NSComboBox! @IBOutlet weak var disableButton: NSButton! @IBOutlet weak var showLabelBtn: NSButton! @IBOutlet weak var showHelpButton: NSButton! @IBOutlet weak var inputField: NSTextField! @IBOutlet weak var indeteBtn: NSButton! @IBOutlet weak var checkBoxWidthConst: NSLayoutConstraint! @IBOutlet weak var checkBoxHeightConst: NSLayoutConstraint! @IBOutlet weak var radioWidthConst: NSLayoutConstraint! @IBOutlet weak var radioHeightConst: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() // Do view setup here. self.typeBox.selectItem(at: 0) NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: inputField) NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: inputField) NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: inputField) self.reloadData() } func reloadData() { self.checkBox.isHidden = true self.radio.isHidden = true self.indeteBtn.isEnabled = true if self.typeBox.indexOfSelectedItem == 0 { self.checkBox.isHidden = false self.indeteBtn.isEnabled = false } else if self.typeBox.indexOfSelectedItem == 1 { self.radio.isHidden = false } var size: ComponentSize = .m if self.sizeBox.indexOfSelectedItem == 0 { size = .m } else if self.sizeBox.indexOfSelectedItem == 1 { size = .s } else if self.sizeBox.indexOfSelectedItem == 2 { size = .xs } else if self.sizeBox.indexOfSelectedItem == 3 { size = .xxs } let isDisabel = self.disableButton.state == .on let showlabel = self.showLabelBtn.state == .on let showhelp = self.showHelpButton.state == .on let string = self.inputField.stringValue var checkboxType: componentCheckboxType = .normal if self.indeteBtn.state == .on { checkboxType = .indeterminate } let properties: ComponentCheckBoxProperty = ComponentCheckBoxProperty.init(size: size, state: .normal, isDisabled: isDisabel, showhelp: showhelp, text: string, checkboxType: checkboxType) checkBox.toolTip = "12312312312312" checkBox.properties = properties let radioproperties: ComponentCheckBoxProperty = ComponentCheckBoxProperty.init(size: size, state: .normal, isDisabled: isDisabel, showhelp: showhelp, text: string, checkboxType: checkboxType) radio.properties = radioproperties self.checkBoxWidthConst.constant = checkBox.properties.propertyInfo.viewWidth self.checkBoxHeightConst.constant = checkBox.properties.propertyInfo.viewHeight self.radioWidthConst.constant = radio.properties.propertyInfo.viewWidth self.radioHeightConst.constant = radio.properties.propertyInfo.viewHeight } @IBAction func clickAction(_ sender: Any) { self.reloadData() } @IBAction func sizeBoxAction(_ sender: Any) { self.reloadData() } //MARK: - TextNotification @objc func textFieldDidBeginEditingNotification(_ notification: Notification) { print("textFieldDidBeginEditingNotification") } @objc func textFieldDidChangeNotification(_ notification: Notification) { print("textFieldDidChangeNotification") self.reloadData() } @objc func textFieldDidEndEditingNotification(_ notification: Notification) { print("textFieldDidEndEditingNotification") self.reloadData() } }