// // ControllerVC.swift // KMComponentLibrary // // Created by Niehaoyu on 2024/9/5. // import Cocoa import KMComponentLibrary class ControllerVC: NSViewController { //Selector @IBOutlet weak var cSelectorItem: ComponentCSelector! @IBOutlet weak var cSelectorGroup: ComponentCSelectorGroup! @IBOutlet weak var sizeBox: NSComboBox! @IBOutlet weak var showLabelBtn: NSButton! @IBOutlet weak var cselectorGroupWidthConst: NSLayoutConstraint! @IBOutlet weak var cselectorGroupHeightConst: NSLayoutConstraint! //Color @IBOutlet weak var cColorItem: ComponentCColorItem! @IBOutlet weak var colorItemBox: NSComboBox! @IBOutlet weak var choosedColorBox: NSBox! @IBOutlet weak var cCustomColorItem: ComponentCColorCustom! @IBOutlet weak var cColorGroup: ComponentCColorGroup! @IBOutlet weak var cPositionItem: ComponentCPosition! @IBOutlet weak var positionDashBtn: NSButton! @IBOutlet weak var positionRowField: NSTextField! @IBOutlet weak var positionColumnField: NSTextField! @IBOutlet weak var positionResultLabel: NSTextField! @IBOutlet weak var positionWidthConst: NSLayoutConstraint! @IBOutlet weak var positionHeightConst: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() // Do view setup here. self.sizeBox.selectItem(at: 0) self.colorItemBox.selectItem(at: 0) self.reloadData() NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: positionRowField) NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: positionRowField) NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: positionRowField) NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: positionColumnField) NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: positionColumnField) NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: positionColumnField) } func reloadData() { var size: ComponentSize = .m if self.sizeBox.indexOfSelectedItem == 0 { size = .m } else if self.sizeBox.indexOfSelectedItem == 1 { size = .s } let showlabel = self.showLabelBtn.state == .on //CSelector let properties: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: size, state: .normal, text: showlabel ? "Label" : nil) self.cSelectorItem.properties = properties //CSelectorGroup var itemArr: [ComponentCSelectorProperty] = [] for _ in 0...3 { let propertyItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: size, state: .normal, text: showlabel ? "Label" : nil) itemArr.append(propertyItem) } cSelectorGroup.updateItemProperty(itemArr) let firStProperties = itemArr.first let widthValue = CGFloat(itemArr.count) * (firStProperties?.propertyInfo.viewWidth ?? 0) + 2 let heightValue = (firStProperties?.propertyInfo.viewWidth ?? 0) + 2 self.cselectorGroupWidthConst.constant = widthValue self.cselectorGroupHeightConst.constant = heightValue //Color var colorType: ComponentColorType = .color if self.colorItemBox.indexOfSelectedItem == 0 { colorType = .color } else if self.colorItemBox.indexOfSelectedItem == 1 { colorType = .colorNone } else if self.colorItemBox.indexOfSelectedItem == 2 { colorType = .colorPanel } let colorProperty: ComponentCColorProperty = ComponentCColorProperty(colorType: colorType, state: .normal, isCustom: false, color: NSColor.red) self.cColorItem.properties = colorProperty self.cColorItem.delegate = self let customColorProperty: ComponentCColorProperty = ComponentCColorProperty(colorType: colorType, state: .normal, isCustom: true, color: NSColor.yellow) self.cCustomColorItem.properties = customColorProperty self.cCustomColorItem.delegate = self //Color Group self.cColorGroup.delegate = self let colorGroupProperty1: ComponentCColorProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.randomColor()) let colorGroupProperty2: ComponentCColorProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.randomColor()) let colorGroupProperty3: ComponentCColorProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.randomColor()) let colorGroupProperty4: ComponentCColorProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.randomColor()) let colorGroupCustomProperty: ComponentCColorProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: NSColor.randomColor()) self.cColorGroup.setUpWithColorPropertys([colorGroupProperty1, colorGroupProperty2, colorGroupProperty3, colorGroupProperty4], customItemProperty: colorGroupCustomProperty) //Position self.cPositionItem.delegate = self let row = self.positionRowField.intValue let column = self.positionColumnField.intValue self.positionWidthConst.constant = CGFloat(column * 30) + CGFloat(column) + 1 self.positionHeightConst.constant = CGFloat(row * 30) + CGFloat(row) + 1 let positionProperty: ComponentCPositionProperty = ComponentCPositionProperty(rowCount: Int(row), columnCount: Int(column), dash: self.positionDashBtn.state == .on) self.cPositionItem.properties = positionProperty } @IBAction func sizeBoxAction(_ sender: Any) { self.reloadData() } @IBAction func buttonAction(_ sender: Any) { self.reloadData() } @IBAction func resetAction(_ sender: Any) { self.reloadData() } //MARK: - TextNotification @objc func textFieldDidBeginEditingNotification(_ notification: Notification) { } @objc func textFieldDidChangeNotification(_ notification: Notification) { } @objc func textFieldDidEndEditingNotification(_ notification: Notification) { print("textFieldDidEndEditingNotification") self.reloadData() self.reloadData() } } extension ControllerVC: ComponentCColorDelegate, ComponentCPositionDelegate { func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) { if let chooseColor = color { self.choosedColorBox.fillColor = chooseColor } else { self.choosedColorBox.fillColor = NSColor.clear } } func componentCPositionDidChoose(_ view: NSView, _ row: Int, _ column: Int) { self.positionResultLabel.stringValue = String(format: "当前选中了%d行%d列", row, column) } }