ComponentSelect.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. //
  2. // ComponentSelect.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by Niehaoyu on 2024/9/3.
  6. //
  7. import Cocoa
  8. import AppKit
  9. @objc public protocol ComponentSelectDelegate: AnyObject {
  10. @objc optional func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?)
  11. @objc optional func componentSelectTextDidBeginEditing(_ view: ComponentSelect)
  12. @objc optional func componentSelectTextDidChange(_ view: ComponentSelect)
  13. @objc optional func componentSelectTextDidEndEditing(_ view: ComponentSelect)
  14. @objc optional func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?)
  15. @objc optional func componentSelectDidMouseDown(_ view: ComponentSelect, with event: NSEvent)
  16. @objc optional func componentSelectDidMouseUp(_ view: ComponentSelect, with event: NSEvent)
  17. }
  18. public class ComponentSelect: ComponentBaseXibView {
  19. @IBOutlet var contendBox: NSBox!
  20. @IBOutlet var leftIconImage: NSImageView!
  21. @IBOutlet var rightIconImage: NSImageView!
  22. @IBOutlet var inputField: ComponentTextField!
  23. @IBOutlet var errorTipLabel: NSTextField!
  24. @IBOutlet var contendBoxBottomConst: NSLayoutConstraint!
  25. @IBOutlet var leftIconWidthConst: NSLayoutConstraint!
  26. @IBOutlet var fieldLeftConst: NSLayoutConstraint!
  27. private var workItem: DispatchWorkItem?
  28. // MARK: Private Property
  29. private var _properties : ComponentSelectProperties = ComponentSelectProperties()
  30. private var isGroupViewShow: Bool = false
  31. private var groupView: ComponentGroup!
  32. private var menuitemPropertys: [ComponentMenuitemProperty] = []
  33. private var selItemProperty: ComponentMenuitemProperty?
  34. weak open var delegate: ComponentSelectDelegate?
  35. // MARK: 初始化
  36. deinit {
  37. NotificationCenter.default.removeObserver(self)
  38. }
  39. public required init?(coder decoder: NSCoder) {
  40. super.init(coder: decoder)
  41. setUp()
  42. }
  43. override init(frame frameRect: NSRect) {
  44. super.init(frame: frameRect)
  45. setUp()
  46. }
  47. public override func awakeFromNib() {
  48. super.awakeFromNib()
  49. setUp()
  50. }
  51. func setUp() {
  52. inputField.componentDelegate = self
  53. inputField.focusRingType = .none
  54. NotificationCenter.default.removeObserver(self)
  55. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: inputField)
  56. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: inputField)
  57. NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: inputField)
  58. }
  59. //Setter
  60. public var properties : ComponentSelectProperties {
  61. get {
  62. return _properties
  63. }
  64. set {
  65. _properties = newValue
  66. ComponentLibrary.shared.configSelectComponent(properties: _properties)
  67. reloadData()
  68. window?.makeFirstResponder(nil)
  69. }
  70. }
  71. //MARK: - Public
  72. public func beginEditing() {
  73. if properties.creatable == true {
  74. self.window?.makeFirstResponder(inputField)
  75. }
  76. }
  77. public func reloadData() {
  78. setupUI()
  79. refreshUI()
  80. }
  81. public func resetText(_ text: String) {
  82. inputField.stringValue = text
  83. }
  84. public func updateMenuItemsArr(_ propertys: [ComponentMenuitemProperty]) {
  85. menuitemPropertys = propertys
  86. }
  87. public func selectItemAtIndex(_ index: Int) {
  88. if index >= 0 && index < menuitemPropertys.count {
  89. let chooseIndex: Int = index
  90. var filteredArray = menuitemPropertys.filter { !($0.type == .divider) }
  91. filteredArray = filteredArray.filter { !($0.type == .header) }
  92. let chooseProperty = filteredArray[chooseIndex]
  93. chooseItem(chooseProperty)
  94. }
  95. }
  96. public func indexOfSelect() -> NSInteger {
  97. if let selProperty = selItemProperty {
  98. var filteredArray = menuitemPropertys.filter { !($0.type == .divider) }
  99. filteredArray = filteredArray.filter { !($0.type == .header) }
  100. if let index = filteredArray.firstIndex(of: selProperty) {
  101. return index
  102. }
  103. }
  104. return -1
  105. }
  106. //MARK: - SetupUI
  107. func setupUI() {
  108. if properties.showLeftIcon == true {
  109. leftIconImage.isHidden = false
  110. leftIconWidthConst.constant = properties.propertyInfo.leftIconWidth ?? 16
  111. fieldLeftConst.constant = leftIconWidthConst.constant + 12
  112. } else {
  113. leftIconImage.isHidden = true
  114. fieldLeftConst.constant = 8
  115. }
  116. if properties.isError == true {
  117. contendBoxBottomConst.constant = 18
  118. errorTipLabel.isHidden = false
  119. } else {
  120. contendBoxBottomConst.constant = 0
  121. errorTipLabel.isHidden = true
  122. }
  123. if properties.isDisabled == false {
  124. inputField.isEditable = true
  125. inputField.isSelectable = true
  126. if properties.creatable == false {
  127. inputField.isEditable = false
  128. inputField.isSelectable = false
  129. }
  130. } else {
  131. inputField.isEditable = false
  132. }
  133. inputField.font = properties.propertyInfo.textFont
  134. if let text = properties.text {
  135. inputField.stringValue = text
  136. if let unit = properties.textUnit {
  137. if text.hasSuffix(unit) == false &&
  138. text != "-" {
  139. inputField.stringValue = text + unit
  140. }
  141. }
  142. }
  143. }
  144. func refreshUI() {
  145. contendBox.cornerRadius = properties.propertyInfo.cornerRadius
  146. contendBox.borderWidth = properties.propertyInfo.borderWidth
  147. var fillColor: NSColor?
  148. var borderColor: NSColor?
  149. var letIcon: NSImage?
  150. if properties.state == .normal {
  151. fillColor = properties.propertyInfo.color_nor
  152. borderColor = properties.propertyInfo.borderColor_nor
  153. if let img = properties.propertyInfo.leftIconImage_nor {
  154. letIcon = img
  155. }
  156. } else if properties.state == .hover {
  157. fillColor = properties.propertyInfo.color_hov
  158. if properties.isError == true {
  159. fillColor = properties.propertyInfo.color_error_hov
  160. }
  161. borderColor = properties.propertyInfo.borderColor_hov
  162. if let img = properties.propertyInfo.leftIconImage_hov {
  163. letIcon = img
  164. }
  165. } else if properties.state == .pressed {
  166. fillColor = properties.propertyInfo.color_active
  167. borderColor = properties.propertyInfo.borderColor_active
  168. if let img = properties.propertyInfo.leftIconImage_active {
  169. letIcon = img
  170. }
  171. }
  172. var textColor: NSColor = properties.propertyInfo.textColor
  173. if properties.isDisabled == true {
  174. fillColor = properties.propertyInfo.color_dis
  175. borderColor = properties.propertyInfo.borderColor_dis
  176. textColor = properties.propertyInfo.textColor_dis
  177. }
  178. if properties.isError == true {
  179. borderColor = properties.propertyInfo.borderColor_error
  180. }
  181. if let color = fillColor {
  182. contendBox.fillColor = color
  183. }
  184. if let color = borderColor {
  185. contendBox.borderColor = color
  186. }
  187. if let placeholder = properties.placeholder {
  188. inputField.placeholderString = placeholder
  189. }
  190. inputField.textColor = textColor
  191. if let img = letIcon {
  192. leftIconImage.image = img
  193. }
  194. rightIconImage.image = ComponentLibrary.shared.image(forResource: "suffix")
  195. if properties.isDisabled {
  196. rightIconImage.image = ComponentLibrary.shared.image(forResource: "suffix_dis")
  197. }
  198. errorTipLabel.textColor = properties.propertyInfo.errorTipTextColor
  199. errorTipLabel.font = properties.propertyInfo.errorTipTextFont
  200. if let errorText = properties.errorText {
  201. errorTipLabel.stringValue = errorText
  202. }
  203. }
  204. func showGroupView() {
  205. if (groupView?.superview) != nil {
  206. return
  207. }
  208. if menuitemPropertys.count == 0 {
  209. return
  210. }
  211. var viewHeight: CGFloat = 8.0
  212. var viewWidth: CGFloat = CGRectGetWidth(self.frame)
  213. var selectIndex: Int = -1
  214. for item in menuitemPropertys {
  215. item.state = .normal
  216. if item == selItemProperty && selItemProperty?.text == properties.text {
  217. item.itemSelected = true
  218. selectIndex = menuitemPropertys.firstIndex(of: item) ?? -1
  219. } else if item.text == properties.text{
  220. item.itemSelected = true
  221. selItemProperty = item
  222. selectIndex = menuitemPropertys.firstIndex(of: item) ?? -1
  223. } else if let text = properties.text, let unitText = properties.textUnit, item.text == text + unitText {
  224. item.itemSelected = true
  225. selItemProperty = item
  226. selectIndex = menuitemPropertys.firstIndex(of: item) ?? -1
  227. } else {
  228. item.itemSelected = false
  229. }
  230. if item.type == .normal {
  231. viewHeight += 36
  232. ComponentLibrary.shared.configMenuItemComponent(properties: item)
  233. viewWidth = max(viewWidth, item.propertyInfo.viewWidth)
  234. } else if item.type == .divider {
  235. viewHeight += 8
  236. }
  237. }
  238. if viewHeight > 360 {
  239. viewHeight = 360
  240. }
  241. var point = convert(contendBox.frame.origin, to: nil)
  242. point.y -= viewHeight
  243. if groupView == nil {
  244. groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  245. }
  246. groupView?.frame = CGRectMake(310, 0, viewWidth, viewHeight)
  247. groupView.groupDelegate = self
  248. groupView.showWithPoint(point, relativeTo: contendBox)
  249. groupView.choosedIndex = selectIndex
  250. groupView?.updateGroupInfo(menuitemPropertys)
  251. isGroupViewShow = true
  252. }
  253. func hideGroupView() {
  254. groupView?.removeGroupView()
  255. isGroupViewShow = false
  256. }
  257. func stringByDeleteCharString(_ originStr: String, _ deleteStr: String) -> String {
  258. var result = ""
  259. for character in originStr {
  260. var canAdd = true
  261. for deleteChar in (deleteStr as String) {
  262. if deleteChar == character {
  263. canAdd = false
  264. break
  265. }
  266. }
  267. if canAdd {
  268. result.append(character)
  269. }
  270. }
  271. return result
  272. }
  273. func chooseItem(_ menuItemProperty: ComponentMenuitemProperty?) {
  274. inputField.stringValue = menuItemProperty?.text ?? ""
  275. properties.text = inputField.stringValue
  276. if let image = menuItemProperty?.lefticon {
  277. properties.showLeftIcon = true
  278. properties.propertyInfo.leftIconImage_nor = image
  279. }
  280. properties.state = .normal
  281. reloadData()
  282. selItemProperty = menuItemProperty
  283. }
  284. //MARK: - TextNotification
  285. @objc func textFieldDidBeginEditingNotification(_ notification: Notification) {
  286. properties.text = inputField.stringValue
  287. delegate?.componentSelectTextDidBeginEditing?(self)
  288. }
  289. @objc func textFieldDidChangeNotification(_ notification: Notification) {
  290. if let regexString = properties.regexString {
  291. guard let textField = notification.object as? NSTextField else { return }
  292. let currentText = textField.stringValue
  293. let allowedCharacterSet = CharacterSet(charactersIn: regexString)
  294. if currentText.rangeOfCharacter(from: allowedCharacterSet.inverted) != nil {
  295. textField.stringValue = currentText.trimmingCharacters(in: allowedCharacterSet.inverted)
  296. }
  297. }
  298. properties.text = inputField.stringValue
  299. delegate?.componentSelectTextDidChange?(self)
  300. }
  301. @objc func textFieldDidEndEditingNotification(_ notification: Notification) {
  302. if properties.isDisabled == false &&
  303. isGroupViewShow == false {
  304. properties.state = .normal
  305. //拼接单位
  306. if let unit = properties.textUnit, unit.count > 0 {
  307. var string = inputField.stringValue
  308. if string.hasSuffix(unit) == false {
  309. string = string + unit
  310. }
  311. inputField.stringValue = string
  312. }
  313. refreshUI()
  314. properties.text = inputField.stringValue
  315. delegate?.componentSelectTextDidEndEditing?(self)
  316. if let text = properties.text, let unitStr = properties.textUnit {
  317. let resultStr = self.stringByDeleteCharString(text, unitStr)
  318. delegate?.componentSelectTextDidEndEditing?(self, removeUnit: resultStr)
  319. } else {
  320. delegate?.componentSelectTextDidEndEditing?(self, removeUnit: properties.text)
  321. }
  322. }
  323. }
  324. //MARK: - MouseEvent
  325. public override func mouseEntered(with event: NSEvent) {
  326. super.mouseEntered(with: event)
  327. if properties.isDisabled == false &&
  328. inputField.isResponder == false &&
  329. isGroupViewShow == false {
  330. properties.state = .hover
  331. }
  332. refreshUI()
  333. }
  334. public override func mouseMoved(with event: NSEvent) {
  335. super.mouseMoved(with: event)
  336. }
  337. public override func mouseExited(with event: NSEvent) {
  338. super.mouseExited(with: event)
  339. if properties.isDisabled == false &&
  340. inputField.isResponder == false &&
  341. isGroupViewShow == false {
  342. properties.state = .normal
  343. }
  344. refreshUI()
  345. }
  346. public override func mouseDown(with event: NSEvent) {
  347. if properties.creatable == true {
  348. let point = convert(event.locationInWindow, from: nil)
  349. if CGRectContainsPoint(rightIconImage.frame, point) {
  350. } else {
  351. super.mouseDown(with: event)
  352. }
  353. }
  354. delegate?.componentSelectDidMouseDown?(self, with: event)
  355. }
  356. public override func mouseUp(with event: NSEvent) {
  357. super.mouseUp(with: event)
  358. if let item = workItem {
  359. item.cancel()
  360. }
  361. if properties.isDisabled == false {
  362. if properties.creatable == true {
  363. let point = convert(event.locationInWindow, from: nil)
  364. if CGRectContainsPoint(rightIconImage.frame, point) {
  365. if self.isGroupViewShow == false {
  366. window?.makeFirstResponder(rightIconImage)
  367. properties.state = .pressed
  368. showGroupView()
  369. } else {
  370. hideGroupView()
  371. }
  372. }
  373. } else {
  374. if self.isGroupViewShow == false {
  375. window?.makeFirstResponder(rightIconImage)
  376. properties.state = .pressed
  377. showGroupView()
  378. } else {
  379. hideGroupView()
  380. }
  381. }
  382. }
  383. if properties.isDisabled == false &&
  384. inputField.isResponder == false &&
  385. isGroupViewShow == false {
  386. properties.state = .normal
  387. }
  388. refreshUI()
  389. delegate?.componentSelectDidMouseUp?(self, with: event)
  390. }
  391. }
  392. extension ComponentSelect: ComponentTextFieldDelegate {
  393. func componentTextFieldDidResponderChanged(textField: NSTextField) {
  394. if properties.isDisabled == false {
  395. if inputField.isResponder {
  396. properties.state = .pressed
  397. //拼接单位
  398. if let unit = properties.textUnit, unit.count > 0 {
  399. var string = inputField.stringValue
  400. if string.hasSuffix(unit) {
  401. string = String(string.dropLast(unit.count))
  402. }
  403. inputField.stringValue = string
  404. }
  405. if properties.autoShowPopupView == true {
  406. showGroupView()
  407. }
  408. } else {
  409. properties.state = .normal
  410. }
  411. }
  412. refreshUI()
  413. }
  414. }
  415. extension ComponentSelect: ComponentGroupDelegate {
  416. public func componentGroupDidDismiss(group: ComponentGroup?) {
  417. properties.state = .normal
  418. let newWorkItem = DispatchWorkItem {
  419. self.hideGroupView()
  420. }
  421. workItem = newWorkItem
  422. if let item = workItem {
  423. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.15, execute: item)
  424. }
  425. window?.makeFirstResponder(nil)
  426. refreshUI()
  427. }
  428. public func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  429. if selItemProperty != menuItemProperty {
  430. chooseItem(menuItemProperty)
  431. delegate?.componentSelectDidSelect?(view: self, menuItemProperty: menuItemProperty)
  432. }
  433. window?.makeFirstResponder(nil)
  434. }
  435. }