DropdownDemoVC.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // DropdownDemoVC.swift
  3. // KMComponentLibraryDemo
  4. //
  5. // Created by wanjun on 2024/8/2.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class DropdownDemoVC: NSViewController, ComponentGroupDelegate, ComponentDropdownDelegate {
  10. @IBOutlet weak var dropdownView: ComponentDropdown!
  11. @IBOutlet weak var typeComboBox: NSComboBox!
  12. @IBOutlet weak var sizeComboBox: NSComboBox!
  13. @IBOutlet weak var disabledButton: NSButton!
  14. @IBOutlet weak var viewHeight: NSLayoutConstraint!
  15. @IBOutlet weak var viewWidth: NSLayoutConstraint!
  16. @IBOutlet weak var menuitemView: ComponentMenuItem!
  17. @IBOutlet weak var menuItemWidthConst: NSLayoutConstraint!
  18. @IBOutlet weak var menuitemHeightConst: NSLayoutConstraint!
  19. @IBOutlet weak var itemMultiSelBtn: NSButton!
  20. @IBOutlet weak var itemDisableBtn: NSButton!
  21. @IBOutlet weak var itemlefticonBtn: NSButton!
  22. @IBOutlet weak var itemRighticonBtn: NSButton!
  23. @IBOutlet weak var itemTexttipField: NSTextField!
  24. @IBOutlet weak var seprateMenuItem: ComponentMenuItem!
  25. //DropdownTool
  26. @IBOutlet weak var dropdownTool: ComponentDropdownTool!
  27. @IBOutlet weak var toolLeftIconBtn: NSButton!
  28. @IBOutlet weak var toolDropdownBtn: NSButton!
  29. @IBOutlet weak var toolDisableBtn: NSButton!
  30. @IBOutlet weak var toolWidthConst: NSLayoutConstraint!
  31. let properties_Dropdown: ComponentDropdownProperty = ComponentDropdownProperty(type: .split_primary,
  32. size: .m,
  33. state: .normal,
  34. split: false,
  35. disabled: false,
  36. text: "Dropdown")
  37. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(type: .normal, multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: "menu item")
  38. let dropdownToolProperty: ComponentDropdownToolProperty = ComponentDropdownToolProperty(state: .normal, text: "Dropdown Tool", disabled: false, leftIcon: nil, showDropdown: false)
  39. var groupView: ComponentGroup!
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. // Do view setup here.
  43. dropdownView.componentDelegate = self
  44. self.reloadData()
  45. self.menuitemReload()
  46. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: itemTexttipField)
  47. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: itemTexttipField)
  48. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: itemTexttipField)
  49. }
  50. func reloadData() {
  51. if let type = DropdownType(rawValue: self.typeComboBox.indexOfSelectedItem) {
  52. properties_Dropdown.type = type
  53. }
  54. if self.sizeComboBox.indexOfSelectedItem == 0 {
  55. properties_Dropdown.size = .m
  56. } else if self.sizeComboBox.indexOfSelectedItem == 1 {
  57. properties_Dropdown.size = .s
  58. } else if self.sizeComboBox.indexOfSelectedItem == 2 {
  59. properties_Dropdown.size = .xs
  60. } else if self.sizeComboBox.indexOfSelectedItem == 3 {
  61. properties_Dropdown.size = .xxs
  62. } else if self.sizeComboBox.indexOfSelectedItem == 4 {
  63. properties_Dropdown.size = .xxxs
  64. }
  65. properties_Dropdown.isDisabled = self.disabledButton.state == .on
  66. dropdownView.properties = properties_Dropdown
  67. viewWidth.constant = self.dropdownView.properties.propertyInfo.viewWidth
  68. viewHeight.constant = self.dropdownView.properties.propertyInfo.viewHeight
  69. let separatorProperty: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  70. self.seprateMenuItem.properties = separatorProperty
  71. //Tool
  72. self.dropdownToolProperty.isDisabled = self.toolDisableBtn.state == .on
  73. if self.toolLeftIconBtn.state == .on {
  74. self.dropdownToolProperty.leftIcon = ComponentLibrary.shared.image(forResource: "test")
  75. } else {
  76. self.dropdownToolProperty.leftIcon = nil
  77. }
  78. self.dropdownToolProperty.showDropdown = self.toolDropdownBtn.state == .on
  79. self.dropdownTool.properties = self.dropdownToolProperty
  80. self.toolWidthConst.constant = self.dropdownTool.properties.propertyInfo.viewWidth
  81. }
  82. func menuitemReload() {
  83. properties_Menuitem.multipleSelect = self.itemMultiSelBtn.state == .on
  84. properties_Menuitem.keyEquivalent = self.itemTexttipField.stringValue
  85. properties_Menuitem.isDisabled = self.itemDisableBtn.state == .on
  86. if properties_Menuitem.multipleSelect == true {
  87. self.itemlefticonBtn.isEnabled = false
  88. self.itemRighticonBtn.isEnabled = false
  89. } else {
  90. self.itemlefticonBtn.isEnabled = true
  91. self.itemRighticonBtn.isEnabled = true
  92. }
  93. menuitemView.properties = properties_Menuitem
  94. self.menuItemWidthConst.constant = menuitemView.properties.propertyInfo.viewWidth
  95. self.menuitemHeightConst.constant = menuitemView.properties.propertyInfo.viewHeight
  96. }
  97. // MARK: Action
  98. @IBAction func typeMenuItemAction(_ sender: NSComboBox) {
  99. self.reloadData()
  100. }
  101. @IBAction func sizeMenuItemAction(_ sender: NSComboBox) {
  102. self.reloadData()
  103. }
  104. @IBAction func disabledButtonAction(_ sender: NSButton) {
  105. self.reloadData()
  106. }
  107. @IBAction func itemAction(_ sender: Any) {
  108. self.menuitemReload()
  109. self.reloadData()
  110. }
  111. @IBAction func dropdownAction(_ sender: NSView) {
  112. }
  113. @IBAction func buttonAction_test(_ sender: NSView) {
  114. var viewHeight: CGFloat = 0
  115. var menuItemArr: [ComponentMenuitemProperty] = []
  116. for i in 0...5 {
  117. if i == 2 {
  118. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  119. menuItemArr.append(property)
  120. viewHeight += 8
  121. } else {
  122. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: "menu item")
  123. menuItemArr.append(properties_Menuitem)
  124. viewHeight += 36
  125. }
  126. }
  127. if groupView == nil {
  128. groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  129. }
  130. groupView.groupDelegate = self
  131. groupView?.frame = CGRectMake(310, 0, viewWidth.constant, viewHeight)
  132. groupView.updateGroupInfo(menuItemArr)
  133. }
  134. func removeGroupView() {
  135. if groupView != nil {
  136. groupView.removeFromSuperview()
  137. }
  138. }
  139. func resetState() {
  140. self.dropdownView.properties.state = .normal
  141. self.dropdownView.reloadData()
  142. self.dropdownTool.properties.state = .normal
  143. self.dropdownTool.reloadData()
  144. }
  145. //MARK: -
  146. func componentDropdownDidClicked(dropdown: ComponentDropdown) {
  147. self.dropdownAction(dropdown)
  148. }
  149. func componentDropdownDidShowMenuItem(dropdown: ComponentDropdown) {
  150. self.buttonAction_test(dropdown)
  151. }
  152. //MARK: - ComponentGroupDelegate
  153. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  154. self.removeGroupView()
  155. self.resetState()
  156. }
  157. func componentGroupDidDismiss(group: ComponentGroup?) {
  158. self.resetState()
  159. }
  160. //MARK: - TextNotification
  161. @objc func textFieldDidBeginEditingNotification(_ notification: Notification) {
  162. print("textFieldDidBeginEditingNotification")
  163. }
  164. @objc func textFieldDidChangeNotification(_ notification: Notification) {
  165. print("textFieldDidChangeNotification")
  166. self.menuitemReload()
  167. }
  168. @objc func textFieldDidEndEditingNotification(_ notification: Notification) {
  169. print("textFieldDidEndEditingNotification")
  170. self.menuitemReload()
  171. }
  172. override func mouseDown(with event: NSEvent) {
  173. super.mouseDown(with: event)
  174. }
  175. }