CheckBoxVC.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // CheckBoxVC.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by Niehaoyu on 2024/8/29.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class CheckBoxVC: NSViewController {
  10. @IBOutlet weak var checkBox: ComponentCheckBox!
  11. @IBOutlet weak var radio: ComponentRadio!
  12. @IBOutlet weak var typeBox: NSComboBox!
  13. @IBOutlet weak var sizeBox: NSComboBox!
  14. @IBOutlet weak var disableButton: NSButton!
  15. @IBOutlet weak var showLabelBtn: NSButton!
  16. @IBOutlet weak var showHelpButton: NSButton!
  17. @IBOutlet weak var inputField: NSTextField!
  18. @IBOutlet weak var indeteBtn: NSButton!
  19. @IBOutlet weak var checkBoxWidthConst: NSLayoutConstraint!
  20. @IBOutlet weak var checkBoxHeightConst: NSLayoutConstraint!
  21. @IBOutlet weak var radioWidthConst: NSLayoutConstraint!
  22. @IBOutlet weak var radioHeightConst: NSLayoutConstraint!
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. // Do view setup here.
  26. self.typeBox.selectItem(at: 0)
  27. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: inputField)
  28. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: inputField)
  29. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: inputField)
  30. self.reloadData()
  31. }
  32. func reloadData() {
  33. self.checkBox.isHidden = true
  34. self.radio.isHidden = true
  35. self.indeteBtn.isEnabled = true
  36. if self.typeBox.indexOfSelectedItem == 0 {
  37. self.checkBox.isHidden = false
  38. self.indeteBtn.isEnabled = false
  39. } else if self.typeBox.indexOfSelectedItem == 1 {
  40. self.radio.isHidden = false
  41. }
  42. var size: ComponentSize = .m
  43. if self.sizeBox.indexOfSelectedItem == 0 {
  44. size = .m
  45. } else if self.sizeBox.indexOfSelectedItem == 1 {
  46. size = .s
  47. } else if self.sizeBox.indexOfSelectedItem == 2 {
  48. size = .xs
  49. } else if self.sizeBox.indexOfSelectedItem == 3 {
  50. size = .xxs
  51. }
  52. let isDisabel = self.disableButton.state == .on
  53. let showlabel = self.showLabelBtn.state == .on
  54. let showhelp = self.showHelpButton.state == .on
  55. let string = self.inputField.stringValue
  56. var checkboxType: componentCheckboxType = .normal
  57. if self.indeteBtn.state == .on {
  58. checkboxType = .indeterminate
  59. }
  60. let properties: ComponentCheckBoxProperty = ComponentCheckBoxProperty.init(size: size, state: .normal, isDisabled: isDisabel, showhelp: showhelp, text: string, checkboxType: checkboxType)
  61. checkBox.toolTip = "12312312312312"
  62. checkBox.properties = properties
  63. let radioproperties: ComponentCheckBoxProperty = ComponentCheckBoxProperty.init(size: size, state: .normal, isDisabled: isDisabel, showhelp: showhelp, text: string, checkboxType: checkboxType)
  64. radio.properties = radioproperties
  65. self.checkBoxWidthConst.constant = checkBox.properties.propertyInfo.viewWidth
  66. self.checkBoxHeightConst.constant = checkBox.properties.propertyInfo.viewHeight
  67. self.radioWidthConst.constant = radio.properties.propertyInfo.viewWidth
  68. self.radioHeightConst.constant = radio.properties.propertyInfo.viewHeight
  69. }
  70. @IBAction func clickAction(_ sender: Any) {
  71. self.reloadData()
  72. }
  73. @IBAction func sizeBoxAction(_ sender: Any) {
  74. self.reloadData()
  75. }
  76. //MARK: - TextNotification
  77. @objc func textFieldDidBeginEditingNotification(_ notification: Notification) {
  78. print("textFieldDidBeginEditingNotification")
  79. }
  80. @objc func textFieldDidChangeNotification(_ notification: Notification) {
  81. print("textFieldDidChangeNotification")
  82. self.reloadData()
  83. }
  84. @objc func textFieldDidEndEditingNotification(_ notification: Notification) {
  85. print("textFieldDidEndEditingNotification")
  86. self.reloadData()
  87. }
  88. }