ControllerVC.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // ControllerVC.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by Niehaoyu on 2024/9/5.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class ControllerVC: NSViewController {
  10. //Selector
  11. @IBOutlet weak var cSelectorItem: ComponentCSelector!
  12. @IBOutlet weak var cSelectorGroup: ComponentCSelectorGroup!
  13. @IBOutlet weak var sizeBox: NSComboBox!
  14. @IBOutlet weak var showLabelBtn: NSButton!
  15. @IBOutlet weak var cselectorGroupWidthConst: NSLayoutConstraint!
  16. @IBOutlet weak var cselectorGroupHeightConst: NSLayoutConstraint!
  17. //Color
  18. @IBOutlet weak var cColorItem: ComponentCColorItem!
  19. @IBOutlet weak var colorItemBox: NSComboBox!
  20. @IBOutlet weak var choosedColorBox: NSBox!
  21. @IBOutlet weak var cCustomColorItem: ComponentCColorCustom!
  22. @IBOutlet weak var cColorGroup: ComponentCColorGroup!
  23. @IBOutlet weak var cPositionItem: ComponentCPosition!
  24. @IBOutlet weak var positionDashBtn: NSButton!
  25. @IBOutlet weak var positionRowField: NSTextField!
  26. @IBOutlet weak var positionColumnField: NSTextField!
  27. @IBOutlet weak var positionResultLabel: NSTextField!
  28. @IBOutlet weak var positionWidthConst: NSLayoutConstraint!
  29. @IBOutlet weak var positionHeightConst: NSLayoutConstraint!
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. // Do view setup here.
  33. self.sizeBox.selectItem(at: 0)
  34. self.colorItemBox.selectItem(at: 0)
  35. self.reloadData()
  36. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: positionRowField)
  37. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: positionRowField)
  38. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: positionRowField)
  39. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: positionColumnField)
  40. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: positionColumnField)
  41. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: positionColumnField)
  42. }
  43. func reloadData() {
  44. var size: ComponentSize = .m
  45. if self.sizeBox.indexOfSelectedItem == 0 {
  46. size = .m
  47. } else if self.sizeBox.indexOfSelectedItem == 1 {
  48. size = .s
  49. }
  50. let showlabel = self.showLabelBtn.state == .on
  51. //CSelector
  52. let properties: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: size, state: .normal, text: showlabel ? "Label" : nil)
  53. self.cSelectorItem.properties = properties
  54. //CSelectorGroup
  55. var itemArr: [ComponentCSelectorProperty] = []
  56. for _ in 0...3 {
  57. let propertyItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: size, state: .normal, text: showlabel ? "Label" : nil)
  58. itemArr.append(propertyItem)
  59. }
  60. cSelectorGroup.updateItemProperty(itemArr)
  61. let firStProperties = itemArr.first
  62. let widthValue = CGFloat(itemArr.count) * (firStProperties?.propertyInfo.viewWidth ?? 0) + 2
  63. let heightValue = (firStProperties?.propertyInfo.viewWidth ?? 0) + 2
  64. self.cselectorGroupWidthConst.constant = widthValue
  65. self.cselectorGroupHeightConst.constant = heightValue
  66. //Color
  67. var colorType: ComponentColorType = .color
  68. if self.colorItemBox.indexOfSelectedItem == 0 {
  69. colorType = .color
  70. } else if self.colorItemBox.indexOfSelectedItem == 1 {
  71. colorType = .colorNone
  72. } else if self.colorItemBox.indexOfSelectedItem == 2 {
  73. colorType = .colorPanel
  74. }
  75. let colorProperty: ComponentCColorProperty = ComponentCColorProperty(colorType: colorType, state: .normal, isCustom: false, color: NSColor.red)
  76. self.cColorItem.properties = colorProperty
  77. self.cColorItem.delegate = self
  78. let customColorProperty: ComponentCColorProperty = ComponentCColorProperty(colorType: colorType, state: .normal, isCustom: true, color: NSColor.yellow)
  79. self.cCustomColorItem.properties = customColorProperty
  80. self.cCustomColorItem.delegate = self
  81. //Color Group
  82. self.cColorGroup.delegate = self
  83. let colorGroupProperty1: ComponentCColorProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.randomColor())
  84. let colorGroupProperty2: ComponentCColorProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.randomColor())
  85. let colorGroupProperty3: ComponentCColorProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.randomColor())
  86. let colorGroupProperty4: ComponentCColorProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.randomColor())
  87. let colorGroupCustomProperty: ComponentCColorProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: NSColor.randomColor())
  88. self.cColorGroup.setUpWithColorPropertys([colorGroupProperty1, colorGroupProperty2, colorGroupProperty3, colorGroupProperty4], customItemProperty: colorGroupCustomProperty)
  89. //Position
  90. self.cPositionItem.delegate = self
  91. let row = self.positionRowField.intValue
  92. let column = self.positionColumnField.intValue
  93. self.positionWidthConst.constant = CGFloat(column * 30) + CGFloat(column) + 1
  94. self.positionHeightConst.constant = CGFloat(row * 30) + CGFloat(row) + 1
  95. let positionProperty: ComponentCPositionProperty = ComponentCPositionProperty(rowCount: Int(row),
  96. columnCount: Int(column),
  97. dash: self.positionDashBtn.state == .on)
  98. self.cPositionItem.properties = positionProperty
  99. }
  100. @IBAction func sizeBoxAction(_ sender: Any) {
  101. self.reloadData()
  102. }
  103. @IBAction func buttonAction(_ sender: Any) {
  104. self.reloadData()
  105. }
  106. @IBAction func resetAction(_ sender: Any) {
  107. self.reloadData()
  108. }
  109. //MARK: - TextNotification
  110. @objc func textFieldDidBeginEditingNotification(_ notification: Notification) {
  111. }
  112. @objc func textFieldDidChangeNotification(_ notification: Notification) {
  113. }
  114. @objc func textFieldDidEndEditingNotification(_ notification: Notification) {
  115. print("textFieldDidEndEditingNotification")
  116. self.reloadData()
  117. self.reloadData()
  118. }
  119. }
  120. extension ControllerVC: ComponentCColorDelegate, ComponentCPositionDelegate {
  121. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  122. if let chooseColor = color {
  123. self.choosedColorBox.fillColor = chooseColor
  124. } else {
  125. self.choosedColorBox.fillColor = NSColor.clear
  126. }
  127. }
  128. func componentCPositionDidChoose(_ view: NSView, _ row: Int, _ column: Int) {
  129. self.positionResultLabel.stringValue = String(format: "当前选中了%d行%d列", row, column)
  130. }
  131. }