KMDesignTextField.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // KMDesignTextField.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/6/7.
  6. //
  7. // 输入框 组件
  8. class KMDesignTextField: KMDesignBaseView {
  9. private var textField: KMCustomTextField?
  10. var textF: KMCustomTextField? {
  11. get {
  12. return self.textField
  13. }
  14. }
  15. private var _designSize: KMDesignSize = .medium
  16. var designSize: KMDesignSize {
  17. get {
  18. return self._designSize
  19. }
  20. }
  21. var stringValue: String? {
  22. get {
  23. return self.textField?.stringValue
  24. }
  25. set {
  26. self.textField?.stringValue = newValue ?? ""
  27. }
  28. }
  29. override var kmEnabled: Bool {
  30. get {
  31. return super.kmEnabled
  32. }
  33. set {
  34. super.kmEnabled = newValue
  35. self.textField?.kmEnabled = newValue
  36. if (newValue) {
  37. // self.window?.makeFirstResponder(self.textField)
  38. } else {
  39. self.window?.makeFirstResponder(nil)
  40. }
  41. }
  42. }
  43. weak var delegate: KMTextFieldDelegate?
  44. var kFormatter: Formatter? {
  45. set {
  46. self.textField?.textFiled.formatter = newValue
  47. }
  48. get {
  49. return self.textField?.textFiled.formatter
  50. }
  51. }
  52. var enterKeyEndEdit: Bool {
  53. set {
  54. let textFiled = self.textField?.textFiled as? KMTextField
  55. textFiled?.enterKeyEndEdit = newValue
  56. }
  57. get {
  58. let textFiled = self.textField?.textFiled as? KMTextField
  59. return textFiled?.enterKeyEndEdit ?? false
  60. }
  61. }
  62. var useToken = true
  63. convenience init(designSize: KMDesignSize = .medium) {
  64. self.init()
  65. self._designSize = designSize
  66. }
  67. override func initSubViews() {
  68. super.initSubViews()
  69. let textFiled = KMCustomTextField()
  70. self.addSubview(textFiled)
  71. self.textField = textFiled
  72. self.textField?.delegate = self
  73. }
  74. override func initDefaultValue() {
  75. super.initDefaultValue()
  76. self.textField?.backgroundView.wantsLayer = true
  77. self.textField?.hoverAction = { [unowned self] sender, action in
  78. if (!self.canHover()) {
  79. return
  80. }
  81. if (action == .enter) {
  82. self.state = .Hov
  83. } else if (action == .exit) {
  84. self.state = .Norm
  85. }
  86. }
  87. let model = KMDesignToken.shared.tokenUsesModel(withToken: self.find_bgToken(state: .Norm))
  88. if let data = model.height as? String, let _height = Float(data) {
  89. self.height = CGFloat(_height)
  90. }
  91. if let data = model.horizontalPadding as? String, let _vSpace = Float(data) {
  92. self.horizontalPadding = CGFloat(_vSpace)
  93. self.textField?.offset = self.horizontalPadding
  94. }
  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.textField?.frame = NSMakeRect(0, y, width, self.height)
  103. }
  104. override func find_bgToken(state: KMDesignTokenState) -> KMDesignTokenKey {
  105. if (state == .Norm) {
  106. return self.designSize == .medium ? "input.m.bg.norm" : "input.s.bg.norm"
  107. } else if (state == .Hov) {
  108. return self.designSize == .medium ? "input.m.bg.hov" : "input.s.bg.hov"
  109. } else if (state == .Focus) {
  110. return self.designSize == .medium ? "input.m.bg.focus" : "input.s.bg.focus"
  111. } else if (state == .Disabled) {
  112. return self.designSize == .medium ? "input.m.bg.dis" : "input.s.bg.dis"
  113. } else if (state == .Error_def) {
  114. return self.designSize == .medium ? "input.m.bg.error-def" : "input.s.bg.error-def"
  115. } else if (state == .Error_focus) {
  116. return self.designSize == .medium ? "input.m.bg.error-focus" : "input.s.bg.error-focus"
  117. }
  118. return KMDesignTokenNone
  119. }
  120. override func updateUI(state: KMDesignTokenState) {
  121. if self.useToken {
  122. super.updateUI(state: state)
  123. }
  124. self.textField?.backgroundView.layer?.backgroundColor = self.fillColor.cgColor
  125. self.textField?.backgroundView.layer?.borderWidth = self.borderWidth
  126. if self.useToken {
  127. self.textField?.backgroundView.layer?.borderColor = self.borderColor.cgColor
  128. } else {
  129. if state == .Hov {
  130. let borderColor = NSColor(hex: "68ACF8")
  131. self.textField?.backgroundView.layer?.borderColor = borderColor.cgColor
  132. } else if state == .Focus {
  133. let borderColor = NSColor(hex: "68ACF8")
  134. self.textField?.backgroundView.layer?.borderColor = borderColor.cgColor
  135. } else {
  136. if KMAppearance.isDarkMode() {
  137. // let borderColor = KMAppearance.Interactive.s0Color()
  138. let borderColor = NSColor(hex: "56585A")
  139. self.textField?.backgroundView.layer?.borderColor = borderColor.cgColor
  140. } else {
  141. let borderColor = NSColor(hex: "DFE1E5")
  142. self.textField?.backgroundView.layer?.borderColor = borderColor.cgColor
  143. }
  144. }
  145. }
  146. self.textField?.backgroundView.layer?.cornerRadius = self.borderRadius
  147. }
  148. }
  149. extension KMDesignTextField: KMTextFieldDelegate {
  150. func km_didBecomeFirstResponder(textField: AnyObject) {
  151. if (textField.isEqual(to: self.textField)) {
  152. self.state = .Focus
  153. }
  154. self.delegate?.km_didBecomeFirstResponder(textField: self)
  155. }
  156. func km_controlTextShouldBeginEditing(textField: AnyObject) -> Bool {
  157. if let should = self.delegate?.km_controlTextShouldBeginEditing(textField: self) {
  158. return should
  159. }
  160. return true
  161. }
  162. func km_controlTextDidBeginEditing(textField: AnyObject) {
  163. self.delegate?.km_controlTextDidBeginEditing(textField: self)
  164. }
  165. func km_controlTextDidChange(textField: AnyObject) {
  166. self.delegate?.km_controlTextDidChange(textField: self)
  167. }
  168. func km_controlTextShouldEndEditing(textField: AnyObject) -> Bool {
  169. if let should = self.delegate?.km_controlTextShouldEndEditing(textField: self) {
  170. return should
  171. }
  172. return true
  173. }
  174. func km_controlTextDidEndEditing(textField: AnyObject) {
  175. if (textField.isEqual(to: self.textField)) {
  176. self.state = .Norm
  177. }
  178. self.delegate?.km_controlTextDidEndEditing(textField: self)
  179. }
  180. }