123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565 |
- //
- // ComponentSelect.swift
- // KMComponentLibrary
- //
- // Created by Niehaoyu on 2024/9/3.
- //
- import Cocoa
- import AppKit
- @objc public protocol ComponentSelectDelegate: AnyObject {
-
- @objc optional func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?)
-
- @objc optional func componentSelectTextDidBeginEditing(_ view: ComponentSelect)
-
- @objc optional func componentSelectTextDidChange(_ view: ComponentSelect)
-
- @objc optional func componentSelectTextDidEndEditing(_ view: ComponentSelect)
-
- @objc optional func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?)
-
- @objc optional func componentSelectDidMouseDown(_ view: ComponentSelect, with event: NSEvent)
-
- @objc optional func componentSelectDidMouseUp(_ view: ComponentSelect, with event: NSEvent)
- }
- public class ComponentSelect: ComponentBaseXibView {
-
- @IBOutlet var contendBox: NSBox!
- @IBOutlet var leftIconImage: NSImageView!
- @IBOutlet var rightIconImage: NSImageView!
- @IBOutlet var inputField: ComponentTextField!
- @IBOutlet var errorTipLabel: NSTextField!
-
- @IBOutlet var contendBoxBottomConst: NSLayoutConstraint!
- @IBOutlet var leftIconWidthConst: NSLayoutConstraint!
- @IBOutlet var fieldLeftConst: NSLayoutConstraint!
-
- private var workItem: DispatchWorkItem?
- // MARK: Private Property
- private var _properties : ComponentSelectProperties = ComponentSelectProperties()
-
- private var isGroupViewShow: Bool = false
-
- private var groupView: ComponentGroup!
-
- private var menuitemPropertys: [ComponentMenuitemProperty] = []
-
- private var selItemProperty: ComponentMenuitemProperty?
-
- weak open var delegate: ComponentSelectDelegate?
-
- // MARK: 初始化
- deinit {
- NotificationCenter.default.removeObserver(self)
-
- }
-
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
-
- setUp()
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- setUp()
- }
-
- public override func awakeFromNib() {
- super.awakeFromNib()
-
- setUp()
- }
-
- func setUp() {
- inputField.componentDelegate = self
- inputField.focusRingType = .none
-
- NotificationCenter.default.removeObserver(self)
- NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidBeginEditingNotification(_:)), name: NSControl.textDidBeginEditingNotification, object: inputField)
- NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeNotification(_:)), name: NSControl.textDidChangeNotification, object: inputField)
- NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: inputField)
-
- }
-
- //Setter
- public var properties : ComponentSelectProperties {
- get {
- return _properties
- }
- set {
- _properties = newValue
-
- ComponentLibrary.shared.configSelectComponent(properties: _properties)
-
- reloadData()
-
- window?.makeFirstResponder(nil)
-
- }
- }
-
- //MARK: - Public
- public func beginEditing() {
- if properties.creatable == true {
- self.window?.makeFirstResponder(inputField)
- }
- }
-
- public func reloadData() {
-
- setupUI()
-
- refreshUI()
- }
-
- public func resetText(_ text: String) {
- inputField.stringValue = text
- }
-
- public func updateMenuItemsArr(_ propertys: [ComponentMenuitemProperty]) {
- menuitemPropertys = propertys
-
- }
-
- public func selectItemAtIndex(_ index: Int) {
- if index >= 0 && index < menuitemPropertys.count {
- let chooseIndex: Int = index
- var filteredArray = menuitemPropertys.filter { !($0.type == .divider) }
- filteredArray = filteredArray.filter { !($0.type == .header) }
-
- let chooseProperty = filteredArray[chooseIndex]
-
- chooseItem(chooseProperty)
- }
- }
-
- public func indexOfSelect() -> NSInteger {
- if let selProperty = selItemProperty {
-
- var filteredArray = menuitemPropertys.filter { !($0.type == .divider) }
- filteredArray = filteredArray.filter { !($0.type == .header) }
-
- if let index = filteredArray.firstIndex(of: selProperty) {
- return index
- }
- }
- return -1
- }
-
- //MARK: - SetupUI
- func setupUI() {
- if properties.showLeftIcon == true {
- leftIconImage.isHidden = false
- leftIconWidthConst.constant = properties.propertyInfo.leftIconWidth ?? 16
-
- fieldLeftConst.constant = leftIconWidthConst.constant + 12
- } else {
- leftIconImage.isHidden = true
- fieldLeftConst.constant = 8
- }
-
- if properties.isError == true {
- contendBoxBottomConst.constant = 18
- errorTipLabel.isHidden = false
- } else {
- contendBoxBottomConst.constant = 0
- errorTipLabel.isHidden = true
- }
-
- if properties.isDisabled == false {
- inputField.isEditable = true
- inputField.isSelectable = true
- if properties.creatable == false {
- inputField.isEditable = false
- inputField.isSelectable = false
- }
- } else {
- inputField.isEditable = false
- }
- inputField.font = properties.propertyInfo.textFont
-
- if let text = properties.text {
- inputField.stringValue = text
-
- if let unit = properties.textUnit {
- if text.hasSuffix(unit) == false &&
- text != "-" {
- inputField.stringValue = text + unit
- }
- }
- }
-
- }
-
- func refreshUI() {
-
- contendBox.cornerRadius = properties.propertyInfo.cornerRadius
- contendBox.borderWidth = properties.propertyInfo.borderWidth
-
- var fillColor: NSColor?
- var borderColor: NSColor?
- var letIcon: NSImage?
-
- if properties.state == .normal {
- fillColor = properties.propertyInfo.color_nor
- borderColor = properties.propertyInfo.borderColor_nor
- if let img = properties.propertyInfo.leftIconImage_nor {
- letIcon = img
- }
- } else if properties.state == .hover {
- fillColor = properties.propertyInfo.color_hov
- if properties.isError == true {
- fillColor = properties.propertyInfo.color_error_hov
- }
- borderColor = properties.propertyInfo.borderColor_hov
- if let img = properties.propertyInfo.leftIconImage_hov {
- letIcon = img
- }
- } else if properties.state == .pressed {
- fillColor = properties.propertyInfo.color_active
- borderColor = properties.propertyInfo.borderColor_active
- if let img = properties.propertyInfo.leftIconImage_active {
- letIcon = img
- }
- }
-
- var textColor: NSColor = properties.propertyInfo.textColor
- if properties.isDisabled == true {
- fillColor = properties.propertyInfo.color_dis
- borderColor = properties.propertyInfo.borderColor_dis
- textColor = properties.propertyInfo.textColor_dis
- }
-
- if properties.isError == true {
- borderColor = properties.propertyInfo.borderColor_error
- }
-
- if let color = fillColor {
- contendBox.fillColor = color
- }
-
- if let color = borderColor {
- contendBox.borderColor = color
- }
-
- if let placeholder = properties.placeholder {
- inputField.placeholderString = placeholder
- }
- inputField.textColor = textColor
-
- if let img = letIcon {
- leftIconImage.image = img
- }
-
- rightIconImage.image = ComponentLibrary.shared.image(forResource: "suffix")
- if properties.isDisabled {
- rightIconImage.image = ComponentLibrary.shared.image(forResource: "suffix_dis")
- }
-
- errorTipLabel.textColor = properties.propertyInfo.errorTipTextColor
-
- errorTipLabel.font = properties.propertyInfo.errorTipTextFont
- if let errorText = properties.errorText {
- errorTipLabel.stringValue = errorText
- }
-
- }
-
- func showGroupView() {
- if (groupView?.superview) != nil {
- return
- }
-
- if menuitemPropertys.count == 0 {
- return
- }
-
- var viewHeight: CGFloat = 8.0
- var viewWidth: CGFloat = CGRectGetWidth(self.frame)
- var selectIndex: Int = -1
-
- for item in menuitemPropertys {
- item.state = .normal
- if item == selItemProperty && selItemProperty?.text == properties.text {
- item.itemSelected = true
- selectIndex = menuitemPropertys.firstIndex(of: item) ?? -1
-
- } else if item.text == properties.text{
- item.itemSelected = true
- selItemProperty = item
-
- selectIndex = menuitemPropertys.firstIndex(of: item) ?? -1
- } else if let text = properties.text, let unitText = properties.textUnit, item.text == text + unitText {
- item.itemSelected = true
- selItemProperty = item
-
- selectIndex = menuitemPropertys.firstIndex(of: item) ?? -1
- } else {
- item.itemSelected = false
- }
-
- if item.type == .normal {
- viewHeight += 36
- ComponentLibrary.shared.configMenuItemComponent(properties: item)
- viewWidth = max(viewWidth, item.propertyInfo.viewWidth)
- } else if item.type == .divider {
- viewHeight += 8
- }
- }
-
- if viewHeight > 360 {
- viewHeight = 360
- }
-
- var point = convert(contendBox.frame.origin, to: nil)
- point.y -= viewHeight
-
- if groupView == nil {
- groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
- }
- groupView?.frame = CGRectMake(310, 0, viewWidth, viewHeight)
- groupView.groupDelegate = self
- groupView.showWithPoint(point, relativeTo: contendBox)
- groupView.choosedIndex = selectIndex
- groupView?.updateGroupInfo(menuitemPropertys)
- isGroupViewShow = true
- }
-
- func hideGroupView() {
- groupView?.removeGroupView()
-
- isGroupViewShow = false
- }
-
- func stringByDeleteCharString(_ originStr: String, _ deleteStr: String) -> String {
- var result = ""
- for character in originStr {
- var canAdd = true
- for deleteChar in (deleteStr as String) {
- if deleteChar == character {
- canAdd = false
- break
- }
- }
- if canAdd {
- result.append(character)
- }
- }
- return result
- }
-
- func chooseItem(_ menuItemProperty: ComponentMenuitemProperty?) {
- inputField.stringValue = menuItemProperty?.text ?? ""
- properties.text = inputField.stringValue
-
- if let image = menuItemProperty?.lefticon {
- properties.showLeftIcon = true
- properties.propertyInfo.leftIconImage_nor = image
- }
- properties.state = .normal
-
- reloadData()
-
- selItemProperty = menuItemProperty
- }
-
- //MARK: - TextNotification
- @objc func textFieldDidBeginEditingNotification(_ notification: Notification) {
-
- properties.text = inputField.stringValue
-
- delegate?.componentSelectTextDidBeginEditing?(self)
-
- }
-
- @objc func textFieldDidChangeNotification(_ notification: Notification) {
- if let regexString = properties.regexString {
- guard let textField = notification.object as? NSTextField else { return }
- let currentText = textField.stringValue
- let allowedCharacterSet = CharacterSet(charactersIn: regexString)
- if currentText.rangeOfCharacter(from: allowedCharacterSet.inverted) != nil {
- textField.stringValue = currentText.trimmingCharacters(in: allowedCharacterSet.inverted)
- }
- }
-
- properties.text = inputField.stringValue
-
- delegate?.componentSelectTextDidChange?(self)
-
- }
-
- @objc func textFieldDidEndEditingNotification(_ notification: Notification) {
- if properties.isDisabled == false &&
- isGroupViewShow == false {
- properties.state = .normal
-
- //拼接单位
- if let unit = properties.textUnit, unit.count > 0 {
- var string = inputField.stringValue
- if string.hasSuffix(unit) == false {
- string = string + unit
- }
- inputField.stringValue = string
- }
-
- refreshUI()
-
- properties.text = inputField.stringValue
-
- delegate?.componentSelectTextDidEndEditing?(self)
-
- if let text = properties.text, let unitStr = properties.textUnit {
- let resultStr = self.stringByDeleteCharString(text, unitStr)
-
- delegate?.componentSelectTextDidEndEditing?(self, removeUnit: resultStr)
- } else {
- delegate?.componentSelectTextDidEndEditing?(self, removeUnit: properties.text)
- }
-
- }
- }
-
- //MARK: - MouseEvent
- public override func mouseEntered(with event: NSEvent) {
- super.mouseEntered(with: event)
-
- if properties.isDisabled == false &&
- inputField.isResponder == false &&
- isGroupViewShow == false {
- properties.state = .hover
- }
- refreshUI()
- }
-
- public override func mouseMoved(with event: NSEvent) {
- super.mouseMoved(with: event)
-
- }
-
- public override func mouseExited(with event: NSEvent) {
- super.mouseExited(with: event)
-
- if properties.isDisabled == false &&
- inputField.isResponder == false &&
- isGroupViewShow == false {
- properties.state = .normal
- }
-
- refreshUI()
- }
-
- public override func mouseDown(with event: NSEvent) {
- if properties.creatable == true {
- let point = convert(event.locationInWindow, from: nil)
- if CGRectContainsPoint(rightIconImage.frame, point) {
-
- } else {
- super.mouseDown(with: event)
- }
- }
- delegate?.componentSelectDidMouseDown?(self, with: event)
- }
-
- public override func mouseUp(with event: NSEvent) {
- super.mouseUp(with: event)
-
- if let item = workItem {
- item.cancel()
- }
- if properties.isDisabled == false {
- if properties.creatable == true {
- let point = convert(event.locationInWindow, from: nil)
- if CGRectContainsPoint(rightIconImage.frame, point) {
- if self.isGroupViewShow == false {
- window?.makeFirstResponder(rightIconImage)
-
- properties.state = .pressed
-
- showGroupView()
- } else {
- hideGroupView()
- }
- }
- } else {
- if self.isGroupViewShow == false {
- window?.makeFirstResponder(rightIconImage)
-
- properties.state = .pressed
-
- showGroupView()
- } else {
- hideGroupView()
- }
- }
- }
-
- if properties.isDisabled == false &&
- inputField.isResponder == false &&
- isGroupViewShow == false {
- properties.state = .normal
- }
- refreshUI()
-
- delegate?.componentSelectDidMouseUp?(self, with: event)
- }
- }
- extension ComponentSelect: ComponentTextFieldDelegate {
-
- func componentTextFieldDidResponderChanged(textField: NSTextField) {
- if properties.isDisabled == false {
- if inputField.isResponder {
- properties.state = .pressed
-
- //拼接单位
- if let unit = properties.textUnit, unit.count > 0 {
- var string = inputField.stringValue
- if string.hasSuffix(unit) {
- string = String(string.dropLast(unit.count))
- }
- inputField.stringValue = string
- }
-
- if properties.autoShowPopupView == true {
- showGroupView()
- }
- } else {
- properties.state = .normal
- }
- }
- refreshUI()
- }
- }
- extension ComponentSelect: ComponentGroupDelegate {
- public func componentGroupDidDismiss(group: ComponentGroup?) {
- properties.state = .normal
-
- let newWorkItem = DispatchWorkItem {
- self.hideGroupView()
- }
- workItem = newWorkItem
- if let item = workItem {
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.15, execute: item)
- }
- window?.makeFirstResponder(nil)
-
- refreshUI()
- }
-
- public func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
- if selItemProperty != menuItemProperty {
- chooseItem(menuItemProperty)
- delegate?.componentSelectDidSelect?(view: self, menuItemProperty: menuItemProperty)
- }
- window?.makeFirstResponder(nil)
- }
- }
|