KMDesignStepperView.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // KMDesignStepperView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/6/8.
  6. //
  7. // 步进器 组件
  8. class KMDesignStepperView: KMDesignBaseView {
  9. private var _stepperView: KMCustomStepperView?
  10. weak var delegate: KMTextFieldDelegate?
  11. override var kmEnabled: Bool {
  12. get {
  13. return super.kmEnabled
  14. }
  15. set {
  16. super.kmEnabled = newValue
  17. self._stepperView?.kmEnabled = newValue
  18. }
  19. }
  20. var minValue: Double = CGFLOAT_MIN {
  21. didSet {
  22. self._stepperView?.minValue = self.minValue
  23. }
  24. }
  25. var maxValue: Double = CGFLOAT_MAX {
  26. didSet {
  27. self._stepperView?.maxValue = self.maxValue
  28. }
  29. }
  30. var increment: Double = 1 {
  31. didSet {
  32. self._stepperView?.increment = self.increment
  33. }
  34. }
  35. var autorepeat = true {
  36. didSet {
  37. self._stepperView?.autorepeat = self.autorepeat
  38. }
  39. }
  40. var kmValue: Double {
  41. get {
  42. if let value = self._stepperView?.kmValue {
  43. return value
  44. }
  45. return 0
  46. }
  47. set {
  48. self._stepperView?.kmValue = newValue
  49. }
  50. }
  51. override func initSubViews() {
  52. super.initSubViews()
  53. self._stepperView = KMCustomStepperView()
  54. self._stepperView?.delegate = self
  55. self.addSubview(self._stepperView!)
  56. self._stepperView?.hoverAction = { [unowned self] sender, action in
  57. if (!self.canHover()) {
  58. return
  59. }
  60. if (action == .enter) {
  61. self.state = .Hov
  62. } else if (action == .exit) {
  63. self.state = .Norm
  64. }
  65. }
  66. self._stepperView?.rightHoverAction = { [unowned self] button, action in
  67. if (action == .enter) {
  68. button.wantsLayer = true
  69. button.layer?.backgroundColor = KMDesignToken.shared.fill(withToken: self.find_arrowBgToken(state: .Hov)).cgColor
  70. } else if (action == .exit) {
  71. button.wantsLayer = true
  72. button.layer?.backgroundColor = .clear
  73. }
  74. }
  75. }
  76. override func initDefaultValue() {
  77. super.initDefaultValue()
  78. self._stepperView?.backgroundView.wantsLayer = true
  79. let model = KMDesignToken.shared.tokenUsesModel(withToken: self.find_bgToken(state: .Norm))
  80. if let data = model.height as? String, let _height = Float(data) {
  81. self.height = CGFloat(_height)
  82. }
  83. if let data = model.paddingLeft as? String, let _paddingLeft = Float(data) {
  84. self._stepperView?.contentOffset = CGFloat(_paddingLeft)
  85. }
  86. self._stepperView?.itemWidth = 20
  87. let arrowBgModel = KMDesignToken.shared.tokenUsesModel(withToken: self.find_arrowBgToken(state: .Norm))
  88. if let data = arrowBgModel.height as? String, let _height = Float(data) {
  89. self._stepperView?.itemHeight = CGFloat(_height)
  90. }
  91. self._stepperView?.horizontalDividerHeight = self.horDividerHeight()
  92. self._stepperView?.horizontalDividerColor = self.horDividerColor()
  93. self._stepperView?.verticalDividerWidth = self.verDividerWidth()
  94. self._stepperView?.verticalDividerColor = self.verDividerColor()
  95. self.state = .Norm
  96. }
  97. override func layout() {
  98. super.layout()
  99. let width: CGFloat = NSWidth(self.bounds)
  100. let heigth: CGFloat = NSHeight(self.bounds)
  101. let y: CGFloat = (heigth - self.height) * 0.5
  102. self._stepperView?.frame = NSMakeRect(0, y, width, self.height)
  103. }
  104. override func find_bgToken(state: KMDesignTokenState) -> KMDesignTokenKey {
  105. if (state == .Norm) {
  106. return "stepper.bg.norm"
  107. } else if (state == .Hov) {
  108. return "stepper.bg.hov"
  109. } else if (state == .Focus) {
  110. return "stepper.bg.focus"
  111. } else if (state == .Disabled) {
  112. return "stepper.bg.dis"
  113. }
  114. return KMDesignTokenNone
  115. }
  116. override func find_textToken(state: KMDesignTokenState) -> KMDesignTokenKey {
  117. if (state == .Norm) {
  118. return "stepper.mac-text.def"
  119. } else if (state == .Hov) {
  120. return "stepper.mac-text.def"
  121. } else if (state == .Focus) {
  122. return "stepper.mac-text.def"
  123. } else if (state == .Disabled) {
  124. return "stepper.mac-text.dis"
  125. }
  126. return KMDesignTokenNone
  127. }
  128. func find_arrowBgToken(state: KMDesignTokenState) -> KMDesignTokenKey {
  129. if (state == .Norm) {
  130. return "stepper.arrow.bg.def"
  131. } else if (state == .Hov) {
  132. return "stepper.arrow.bg.hov"
  133. } else if (state == .Focus) {
  134. return "stepper.arrow.bg.def"
  135. } else if (state == .Disabled) {
  136. return "stepper.arrow.bg.def"
  137. }
  138. return KMDesignTokenNone
  139. }
  140. override func updateUI(state: KMDesignTokenState) {
  141. super.updateUI(state: state)
  142. self._stepperView?.backgroundView.layer?.backgroundColor = self.fillColor.cgColor
  143. self._stepperView?.backgroundView.layer?.borderWidth = self.borderWidth
  144. self._stepperView?.backgroundView.layer?.borderColor = self.borderColor.cgColor
  145. self._stepperView?.backgroundView.layer?.cornerRadius = self.borderRadius
  146. self._stepperView?.textField.textColor = self.textColor
  147. if let _font = NSFont(name: self.fontName, size: self.fontSize) {
  148. self._stepperView?.textField.font = _font
  149. }
  150. }
  151. }
  152. extension KMDesignStepperView: KMTextFieldDelegate {
  153. func km_didBecomeFirstResponder(textField: AnyObject) {
  154. if (textField.isEqual(to: self._stepperView)) {
  155. self.state = .Focus
  156. }
  157. self.delegate?.km_didBecomeFirstResponder(textField: self)
  158. }
  159. func km_controlTextShouldBeginEditing(textField: AnyObject) -> Bool {
  160. if let should = self.delegate?.km_controlTextShouldBeginEditing(textField: self) {
  161. return should
  162. }
  163. return true
  164. }
  165. func km_controlTextDidBeginEditing(textField: AnyObject) {
  166. self.delegate?.km_controlTextDidBeginEditing(textField: self)
  167. }
  168. func km_controlTextDidChange(textField: AnyObject) {
  169. self.delegate?.km_controlTextDidChange(textField: self)
  170. }
  171. func km_controlTextShouldEndEditing(textField: AnyObject) -> Bool {
  172. if let should = self.delegate?.km_controlTextShouldEndEditing(textField: self) {
  173. return should
  174. }
  175. return true
  176. }
  177. func km_controlTextDidEndEditing(textField: AnyObject) {
  178. if (textField.isEqual(to: self._stepperView)) {
  179. self.state = .Norm
  180. }
  181. self.delegate?.km_controlTextDidEndEditing(textField: self)
  182. }
  183. }