123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- //
- // 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_MIN {
- didSet {
- self._stepperView?.minValue = self.minValue
- }
- }
- var maxValue: Double = CGFLOAT_MAX {
- 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)
- }
- }
|