// // DropdownDemoVC.swift // KMComponentLibraryDemo // // Created by wanjun on 2024/8/2. // import Cocoa import KMComponentLibrary class DropdownDemoVC: NSViewController, ComponentGroupDelegate, ComponentDropdownDelegate { @IBOutlet weak var dropdownView: ComponentDropdown! @IBOutlet weak var typeComboBox: NSComboBox! @IBOutlet weak var sizeComboBox: NSComboBox! @IBOutlet weak var disabledButton: NSButton! @IBOutlet weak var viewHeight: NSLayoutConstraint! @IBOutlet weak var viewWidth: NSLayoutConstraint! @IBOutlet weak var menuitemView: ComponentMenuItem! @IBOutlet weak var menuItemWidthConst: NSLayoutConstraint! @IBOutlet weak var menuitemHeightConst: NSLayoutConstraint! @IBOutlet weak var itemMultiSelBtn: NSButton! @IBOutlet weak var itemDisableBtn: NSButton! @IBOutlet weak var itemlefticonBtn: NSButton! @IBOutlet weak var itemRighticonBtn: NSButton! @IBOutlet weak var itemTexttipField: NSTextField! @IBOutlet weak var seprateMenuItem: ComponentMenuItem! //DropdownTool @IBOutlet weak var dropdownTool: ComponentDropdownTool! @IBOutlet weak var toolLeftIconBtn: NSButton! @IBOutlet weak var toolDropdownBtn: NSButton! @IBOutlet weak var toolDisableBtn: NSButton! @IBOutlet weak var toolWidthConst: NSLayoutConstraint! let properties_Dropdown: ComponentDropdownProperty = ComponentDropdownProperty(type: .split_primary, size: .m, state: .normal, split: false, disabled: false, text: "Dropdown") let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(type: .normal, multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: "menu item") let dropdownToolProperty: ComponentDropdownToolProperty = ComponentDropdownToolProperty(state: .normal, text: "Dropdown Tool", disabled: false, leftIcon: nil, showDropdown: false) var groupView: ComponentGroup! override func viewDidLoad() { super.viewDidLoad() // Do view setup here. dropdownView.componentDelegate = self self.reloadData() self.menuitemReload() NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: itemTexttipField) NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: itemTexttipField) NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: itemTexttipField) } func reloadData() { if let type = DropdownType(rawValue: self.typeComboBox.indexOfSelectedItem) { properties_Dropdown.type = type } if self.sizeComboBox.indexOfSelectedItem == 0 { properties_Dropdown.size = .m } else if self.sizeComboBox.indexOfSelectedItem == 1 { properties_Dropdown.size = .s } else if self.sizeComboBox.indexOfSelectedItem == 2 { properties_Dropdown.size = .xs } else if self.sizeComboBox.indexOfSelectedItem == 3 { properties_Dropdown.size = .xxs } else if self.sizeComboBox.indexOfSelectedItem == 4 { properties_Dropdown.size = .xxxs } properties_Dropdown.isDisabled = self.disabledButton.state == .on dropdownView.properties = properties_Dropdown viewWidth.constant = self.dropdownView.properties.propertyInfo.viewWidth viewHeight.constant = self.dropdownView.properties.propertyInfo.viewHeight let separatorProperty: ComponentMenuitemProperty = ComponentMenuitemProperty.divider() self.seprateMenuItem.properties = separatorProperty //Tool self.dropdownToolProperty.isDisabled = self.toolDisableBtn.state == .on if self.toolLeftIconBtn.state == .on { self.dropdownToolProperty.leftIcon = ComponentLibrary.shared.image(forResource: "test") } else { self.dropdownToolProperty.leftIcon = nil } self.dropdownToolProperty.showDropdown = self.toolDropdownBtn.state == .on self.dropdownTool.properties = self.dropdownToolProperty self.toolWidthConst.constant = self.dropdownTool.properties.propertyInfo.viewWidth } func menuitemReload() { properties_Menuitem.multipleSelect = self.itemMultiSelBtn.state == .on properties_Menuitem.keyEquivalent = self.itemTexttipField.stringValue properties_Menuitem.isDisabled = self.itemDisableBtn.state == .on if properties_Menuitem.multipleSelect == true { self.itemlefticonBtn.isEnabled = false self.itemRighticonBtn.isEnabled = false } else { self.itemlefticonBtn.isEnabled = true self.itemRighticonBtn.isEnabled = true } menuitemView.properties = properties_Menuitem self.menuItemWidthConst.constant = menuitemView.properties.propertyInfo.viewWidth self.menuitemHeightConst.constant = menuitemView.properties.propertyInfo.viewHeight } // MARK: Action @IBAction func typeMenuItemAction(_ sender: NSComboBox) { self.reloadData() } @IBAction func sizeMenuItemAction(_ sender: NSComboBox) { self.reloadData() } @IBAction func disabledButtonAction(_ sender: NSButton) { self.reloadData() } @IBAction func itemAction(_ sender: Any) { self.menuitemReload() self.reloadData() } @IBAction func dropdownAction(_ sender: NSView) { } @IBAction func buttonAction_test(_ sender: NSView) { var viewHeight: CGFloat = 0 var menuItemArr: [ComponentMenuitemProperty] = [] for i in 0...5 { if i == 2 { let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider() menuItemArr.append(property) viewHeight += 8 } else { let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: "menu item") menuItemArr.append(properties_Menuitem) viewHeight += 36 } } if groupView == nil { groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle()) } groupView.groupDelegate = self groupView?.frame = CGRectMake(310, 0, viewWidth.constant, viewHeight) groupView.updateGroupInfo(menuItemArr) } func removeGroupView() { if groupView != nil { groupView.removeFromSuperview() } } func resetState() { self.dropdownView.properties.state = .normal self.dropdownView.reloadData() self.dropdownTool.properties.state = .normal self.dropdownTool.reloadData() } //MARK: - func componentDropdownDidClicked(dropdown: ComponentDropdown) { self.dropdownAction(dropdown) } func componentDropdownDidShowMenuItem(dropdown: ComponentDropdown) { self.buttonAction_test(dropdown) } //MARK: - ComponentGroupDelegate func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) { self.removeGroupView() self.resetState() } func componentGroupDidDismiss(group: ComponentGroup?) { self.resetState() } //MARK: - TextNotification @objc func textFieldDidBeginEditingNotification(_ notification: Notification) { print("textFieldDidBeginEditingNotification") } @objc func textFieldDidChangeNotification(_ notification: Notification) { print("textFieldDidChangeNotification") self.menuitemReload() } @objc func textFieldDidEndEditingNotification(_ notification: Notification) { print("textFieldDidEndEditingNotification") self.menuitemReload() } override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) } }