KMBaseTextField.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // KMBaseTextField.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2022/12/12.
  6. //
  7. import Cocoa
  8. typealias KMBaseTextFieldTextDidEndEditing = (_ string: String) -> Void
  9. typealias KMBaseTextFieldTextTextDidChange = (_ string: String) -> Void
  10. typealias KMBaseTextFieldTextTextDidBeginEditing = (_ string: String) -> Void
  11. typealias KMBaseTextFieldTextTextDeleteAction = (_ string: String) -> Void
  12. class KMBaseTextField: NSView {
  13. var textDidEndEditing: KMBaseTextFieldTextDidEndEditing?
  14. var textDidChange: KMBaseTextFieldTextTextDidChange?
  15. var textDidBeginEditing: KMBaseTextFieldTextTextDidBeginEditing?
  16. var textDeleteAction: KMBaseTextFieldTextTextDeleteAction?
  17. var presenter: KMBaseTextFieldPresenter = KMBaseTextFieldPresenter()
  18. var model: KMBaseTextFieldModel = KMBaseTextFieldModel() {
  19. didSet {
  20. self.textFormatter.model = model
  21. }
  22. }
  23. lazy var textField: FocusAwareTextField = FocusAwareTextField()
  24. var stringValue: String {
  25. set {
  26. textField.stringValue = newValue + self.model.specialChart
  27. self.model.value = stringValue
  28. }
  29. get {
  30. self.model.value
  31. }
  32. }
  33. var placeholderString: String? {
  34. didSet {
  35. self.textField.placeholderString = placeholderString!
  36. self.model.placeholderString = placeholderString!
  37. }
  38. }
  39. var isEnabled: Bool? {
  40. didSet {
  41. self.textField.isEnabled = isEnabled!
  42. self.model.isEnabled = isEnabled!
  43. }
  44. }
  45. var minLen: Int? {
  46. didSet {
  47. self.model.minLen = minLen!
  48. }
  49. }
  50. var maxLen: Int = 999 {
  51. didSet {
  52. self.model.maxLen = maxLen
  53. }
  54. }
  55. var minValue: Int? {
  56. didSet {
  57. self.model.minValue = minValue!
  58. }
  59. }
  60. var maxValue: Int? {
  61. didSet {
  62. self.model.maxValue = maxValue!
  63. }
  64. }
  65. var onlyNumber: Bool? {
  66. didSet {
  67. self.model.onlyNumber = onlyNumber!
  68. if onlyNumber! {
  69. self.textField.formatter = self.textFormatter
  70. } else {
  71. self.textField.formatter = nil
  72. }
  73. }
  74. }
  75. var inputType: KMBaseTextFieldInputType? {
  76. didSet {
  77. if (inputType == .int) {
  78. self.model.specialChart = "-"
  79. } else if (inputType == .float) {
  80. self.model.specialChart = ".-"
  81. } else if (inputType == .percent) {
  82. self.model.specialChart = "%"
  83. }
  84. }
  85. }
  86. var textFormatter: KMBaseTextFormatter = KMBaseTextFormatter()
  87. override func awakeFromNib() {
  88. self.addSubview(self.textField)
  89. self.textField.translatesAutoresizingMaskIntoConstraints = false
  90. NSLayoutConstraint.activate([
  91. self.textField.topAnchor.constraint(equalTo: topAnchor),
  92. self.textField.leftAnchor.constraint(equalTo: leftAnchor),
  93. self.textField.rightAnchor.constraint(equalTo: rightAnchor),
  94. self.textField.bottomAnchor.constraint(equalTo: bottomAnchor)])
  95. self.textField.updateConstraintsForSubtreeIfNeeded()
  96. self.textField.formatter = self.textFormatter
  97. self.textField.wantsLayer = true
  98. self.textField.layer?.borderWidth = 0
  99. self.textField.layer?.borderColor = NSColor.clear.cgColor
  100. self.textField.drawsBackground = false
  101. self.textField.layer?.backgroundColor = NSColor.clear.cgColor
  102. self.textField.backgroundColor = NSColor.clear
  103. self.textField.isBordered = false
  104. // self.textField.font = FONT_R(13.);
  105. // self.textField.textColor = [[NSColor whiteColor] colorWithAlphaComponent:0.87];
  106. self.textField.delegate = self
  107. self.textFormatter.model = self.model
  108. self.presenter.initPresenter(delegate: self, model: self.model)
  109. }
  110. }
  111. extension KMBaseTextField: NSTextFieldDelegate {
  112. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  113. switch commandSelector {
  114. case #selector(NSResponder.insertNewline(_:)):
  115. if let inputView = control as? NSTextField {
  116. //当当前TextField按下enter
  117. if inputView == textField {
  118. KMPrint("按下 enter")
  119. if self.textDidEndEditing != nil {
  120. self.textDidEndEditing!(self.model.stringValue)
  121. }
  122. }
  123. }
  124. return true
  125. case #selector(NSResponder.deleteBackward(_:)):
  126. if let inputView = control as? NSTextField {
  127. //当当前TextField按下enter
  128. if inputView == textField {
  129. KMPrint("按下 delete")
  130. if inputView.stringValue == "" {
  131. if self.textDeleteAction != nil {
  132. self.textDeleteAction!(self.model.stringValue)
  133. }
  134. return true
  135. }
  136. }
  137. }
  138. return false
  139. default:
  140. return false
  141. }
  142. }
  143. func controlTextDidChange(_ obj: Notification) {
  144. KMPrint("controlTextDidChange")
  145. if self.textField.stringValue.count >= self.maxLen {
  146. var string = self.textField.stringValue
  147. string = String(string.prefix(1))
  148. self.textField.stringValue = string
  149. }
  150. self.presenter.changeValue(value: self.textField.stringValue)
  151. if self.textDidChange != nil {
  152. self.textDidChange!(self.model.stringValue)
  153. }
  154. }
  155. func controlTextDidEndEditing(_ obj: Notification) {
  156. KMPrint("controlTextDidEndEditing")
  157. self.presenter.changeValue(value: self.textField.stringValue)
  158. if self.textDidEndEditing != nil {
  159. self.textDidEndEditing!(self.model.stringValue)
  160. }
  161. }
  162. func controlTextDidBeginEditing(_ obj: Notification) {
  163. KMPrint("controlTextDidBeginEditing")
  164. if self.textDidBeginEditing != nil {
  165. self.textDidBeginEditing!(self.model.stringValue)
  166. }
  167. }
  168. func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool {
  169. KMPrint("textShouldEndEditing")
  170. return true
  171. }
  172. }
  173. extension KMBaseTextField: KMBaseTextFieldPresenterDelegate {
  174. func valueDidChange(presenter: KMBaseTextFieldPresenter, model: KMBaseTextFieldModel) {
  175. self.model = model
  176. self.textField.stringValue = model.stringValue
  177. }
  178. }
  179. //extension NSTextField {
  180. // open override func becomeFirstResponder() -> Bool {
  181. // let success = super.becomeFirstResponder()
  182. // if success {
  183. // let textView: NSTextView = self.currentEditor() as! NSTextView
  184. // if textView?.responds(to: #selector(insetionpoint)) {
  185. // let color = self.textColor
  186. // textView.setInsertionPointColor(color)
  187. // }
  188. // }
  189. // return success
  190. // }
  191. //}
  192. //extension NSTextField {
  193. //// var onFocus: () -> Void = {}
  194. //// var onUnfocus: () -> Void = {}
  195. //
  196. // open override func becomeFirstResponder() -> Bool {
  197. //// onFocus()
  198. // let textView = window?.fieldEditor(true, for: nil) as? NSTextView
  199. // textView?.insertionPointColor = NSColor.red
  200. // return super.becomeFirstResponder()
  201. // }
  202. //
  203. // open override func resignFirstResponder() -> Bool {
  204. //// onUnfocus()
  205. // return super.resignFirstResponder()
  206. // }
  207. //}
  208. class FocusAwareTextField: NSTextField, NSTextFieldDelegate {
  209. var onFocus: () -> Void = {}
  210. var onUnfocus: () -> Void = {}
  211. override func becomeFirstResponder() -> Bool {
  212. onFocus()
  213. let textView = window?.fieldEditor(true, for: nil) as? NSTextView
  214. textView?.insertionPointColor = NSColor.km_init(hex: "#252629")
  215. return super.becomeFirstResponder()
  216. }
  217. // override func resignFirstResponder() -> Bool {
  218. // onUnfocus()
  219. // return super.resignFirstResponder()
  220. // }
  221. func controlTextDidEndEditing(_ obj: Notification) {
  222. let textField = obj.object as? NSTextField
  223. if (textField == self) {
  224. onUnfocus()
  225. }
  226. }
  227. }