// // KMDesignStepperView.swift // PDF Reader Pro // // Created by tangchao on 2023/6/8. // // 步进器 组件 class KMDesignStepperView: KMDesignBaseView { private var _stepperView: KMCustomStepperView? weak var delegate: KMTextFieldDelegate? override var kmEnabled: Bool { get { return super.kmEnabled } set { super.kmEnabled = newValue self._stepperView?.kmEnabled = newValue } } var minValue: Double = CGFloat.leastNormalMagnitude { didSet { self._stepperView?.minValue = self.minValue } } var maxValue: Double = CGFloat.greatestFiniteMagnitude { didSet { self._stepperView?.maxValue = self.maxValue } } var increment: Double = 1 { didSet { self._stepperView?.increment = self.increment } } var autorepeat = true { didSet { self._stepperView?.autorepeat = self.autorepeat } } var kmValue: Double { get { if let value = self._stepperView?.kmValue { return value } return 0 } set { self._stepperView?.kmValue = newValue } } override func initSubViews() { super.initSubViews() self._stepperView = KMCustomStepperView() self._stepperView?.delegate = self self.addSubview(self._stepperView!) self._stepperView?.hoverAction = { [unowned self] sender, action in if (!self.canHover()) { return } if (action == .enter) { self.state = .Hov } else if (action == .exit) { self.state = .Norm } } self._stepperView?.rightHoverAction = { [unowned self] button, action in if (action == .enter) { button.wantsLayer = true button.layer?.backgroundColor = KMDesignToken.shared.fill(withToken: self.find_arrowBgToken(state: .Hov)).cgColor } else if (action == .exit) { button.wantsLayer = true button.layer?.backgroundColor = .clear } } } override func initDefaultValue() { super.initDefaultValue() self._stepperView?.backgroundView.wantsLayer = true let model = KMDesignToken.shared.tokenUsesModel(withToken: self.find_bgToken(state: .Norm)) if let data = model.height as? String, let _height = Float(data) { self.height = CGFloat(_height) } if let data = model.paddingLeft as? String, let _paddingLeft = Float(data) { self._stepperView?.contentOffset = CGFloat(_paddingLeft) } self._stepperView?.itemWidth = 20 let arrowBgModel = KMDesignToken.shared.tokenUsesModel(withToken: self.find_arrowBgToken(state: .Norm)) if let data = arrowBgModel.height as? String, let _height = Float(data) { self._stepperView?.itemHeight = CGFloat(_height) } self._stepperView?.horizontalDividerHeight = self.horDividerHeight() self._stepperView?.horizontalDividerColor = self.horDividerColor() self._stepperView?.verticalDividerWidth = self.verDividerWidth() self._stepperView?.verticalDividerColor = self.verDividerColor() self.state = .Norm } override func layout() { super.layout() let width: CGFloat = NSWidth(self.bounds) let heigth: CGFloat = NSHeight(self.bounds) let y: CGFloat = (heigth - self.height) * 0.5 self._stepperView?.frame = NSMakeRect(0, y, width, self.height) } override func find_bgToken(state: KMDesignTokenState) -> KMDesignTokenKey { if (state == .Norm) { return "stepper.bg.norm" } else if (state == .Hov) { return "stepper.bg.hov" } else if (state == .Focus) { return "stepper.bg.focus" } else if (state == .Disabled) { return "stepper.bg.dis" } return KMDesignTokenNone } override func find_textToken(state: KMDesignTokenState) -> KMDesignTokenKey { if (state == .Norm) { return "stepper.mac-text.def" } else if (state == .Hov) { return "stepper.mac-text.def" } else if (state == .Focus) { return "stepper.mac-text.def" } else if (state == .Disabled) { return "stepper.mac-text.dis" } return KMDesignTokenNone } func find_arrowBgToken(state: KMDesignTokenState) -> KMDesignTokenKey { if (state == .Norm) { return "stepper.arrow.bg.def" } else if (state == .Hov) { return "stepper.arrow.bg.hov" } else if (state == .Focus) { return "stepper.arrow.bg.def" } else if (state == .Disabled) { return "stepper.arrow.bg.def" } return KMDesignTokenNone } override func updateUI(state: KMDesignTokenState) { super.updateUI(state: state) self._stepperView?.backgroundView.layer?.backgroundColor = self.fillColor.cgColor self._stepperView?.backgroundView.layer?.borderWidth = self.borderWidth self._stepperView?.backgroundView.layer?.borderColor = self.borderColor.cgColor self._stepperView?.backgroundView.layer?.cornerRadius = self.borderRadius self._stepperView?.textField.textColor = self.textColor if let _font = NSFont(name: self.fontName, size: self.fontSize) { self._stepperView?.textField.font = _font } } } extension KMDesignStepperView: KMTextFieldDelegate { func km_didBecomeFirstResponder(textField: AnyObject) { if (textField.isEqual(to: self._stepperView)) { self.state = .Focus } self.delegate?.km_didBecomeFirstResponder(textField: self) } func km_controlTextShouldBeginEditing(textField: AnyObject) -> Bool { if let should = self.delegate?.km_controlTextShouldBeginEditing(textField: self) { return should } return true } func km_controlTextDidBeginEditing(textField: AnyObject) { self.delegate?.km_controlTextDidBeginEditing(textField: self) } func km_controlTextDidChange(textField: AnyObject) { self.delegate?.km_controlTextDidChange(textField: self) } func km_controlTextShouldEndEditing(textField: AnyObject) -> Bool { if let should = self.delegate?.km_controlTextShouldEndEditing(textField: self) { return should } return true } func km_controlTextDidEndEditing(textField: AnyObject) { if (textField.isEqual(to: self._stepperView)) { self.state = .Norm } self.delegate?.km_controlTextDidEndEditing(textField: self) } }