KMSecureTextFiled.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. //
  2. // KMSecureTextFiled.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/11/28.
  6. //
  7. import Cocoa
  8. enum KMSecureTextFiledMode: Int {
  9. case ciphertext = 0 /// 密文
  10. case plaintext = 1 /// 明文
  11. }
  12. enum KMSecureTextFiledViewMode: Int {
  13. case never = 0
  14. case whileEditing
  15. case unlessEditing
  16. case always
  17. }
  18. typealias KMSecureTextFiledFirstResponderHandler = (Bool)->()
  19. private class KMSecureTextFiled_TextFiled: NSTextField {
  20. var firstResponderHandler: KMSecureTextFiledFirstResponderHandler?
  21. override func becomeFirstResponder() -> Bool {
  22. let result = super.becomeFirstResponder()
  23. guard let callback = firstResponderHandler else {
  24. return result
  25. }
  26. callback(result)
  27. return result
  28. }
  29. override func performKeyEquivalent(with event: NSEvent) -> Bool {
  30. if event.modifierFlags.contains(.command) {
  31. // if event.modifierFlags.contains(.deviceIndependentFlagsMask) {
  32. if event.charactersIgnoringModifiers == "x" {
  33. // return NSApp.sendAction(NSSelectorFromString("cut:"), to: self.window?.firstResponder, from: self)
  34. return true
  35. } else if event.charactersIgnoringModifiers == "c" {
  36. // return NSApp.sendAction(NSSelectorFromString("copy:"), to: self.window?.firstResponder, from: self)
  37. return true
  38. }
  39. // else if event.charactersIgnoringModifiers == "v" {
  40. // return NSApp.sendAction(NSSelectorFromString("paste:"), to: self.window?.firstResponder, from: self)
  41. // return true
  42. // }
  43. }
  44. return super.performKeyEquivalent(with: event)
  45. }
  46. }
  47. private class KMSecureTextFiled_SecureTextFiled: NSSecureTextField {
  48. var firstResponderHandler: KMSecureTextFiledFirstResponderHandler?
  49. override func becomeFirstResponder() -> Bool {
  50. let result = super.becomeFirstResponder()
  51. guard let callback = firstResponderHandler else {
  52. return result
  53. }
  54. callback(result)
  55. return result
  56. }
  57. }
  58. typealias KMSecureTextFiledValueDidChange = (_ view: KMSecureTextFiled, _ string: String) -> Void
  59. typealias KMSecureTextFiledBecomeFirstResponderHandler = (NSView)->()
  60. typealias KMSecureTextFiledEnterAction = () -> ()
  61. class KMSecureTextFiled: NSView {
  62. var enterAction: KMSecureTextFiledEnterAction?
  63. var backgroundView = NSView()
  64. private var textFiled = KMSecureTextFiled_TextFiled()
  65. private var secureTextField = KMSecureTextFiled_SecureTextFiled()
  66. var mode: KMSecureTextFiledMode = .ciphertext
  67. private var _rightView: NSView?
  68. var rightView: NSView? {
  69. get {
  70. return self._rightView
  71. }
  72. set {
  73. if self._rightView != newValue {
  74. if let view = self._rightView {
  75. view.removeFromSuperview()
  76. }
  77. self._rightView = newValue
  78. }
  79. if let view = self._rightView {
  80. self.addSubview(view)
  81. self.layoutSubtreeIfNeeded()
  82. }
  83. self.updateRightViewStateIfNeed(editing: self.kmEnabled)
  84. }
  85. }
  86. private var _rightViewMode = KMSecureTextFiledViewMode.whileEditing
  87. var rightViewMode: KMSecureTextFiledViewMode {
  88. get {
  89. return self._rightViewMode
  90. }
  91. set {
  92. self._rightViewMode = newValue
  93. self.updateRightViewStateIfNeed(editing: self.kmEnabled)
  94. }
  95. }
  96. private var _enabled = true
  97. var kmEnabled: Bool {
  98. get {
  99. return self._enabled
  100. }
  101. set {
  102. self._enabled = newValue
  103. textFiled.isEnabled = newValue
  104. secureTextField.isEnabled = newValue
  105. self.updateRightViewStateIfNeed(editing: newValue)
  106. }
  107. }
  108. var placeholderString: String {
  109. get {
  110. if (self.mode == .plaintext) {
  111. return self.textFiled.placeholderString ?? ""
  112. } else {
  113. return self.secureTextField.placeholderString ?? ""
  114. }
  115. }
  116. set {
  117. if (self.mode == .plaintext) {
  118. self.textFiled.placeholderString = newValue
  119. } else {
  120. self.secureTextField.placeholderString = newValue
  121. }
  122. }
  123. }
  124. var maxLength: Int = 0
  125. var valueDidChange: KMSecureTextFiledValueDidChange?
  126. var becomeFirstResponderHandler: KMSecureTextFiledBecomeFirstResponderHandler?
  127. var didEndEditHandler: ((String?)->())?
  128. override init(frame frameRect: NSRect) {
  129. super.init(frame: frameRect)
  130. initDefaultValue()
  131. initSubViews()
  132. }
  133. required init?(coder: NSCoder) {
  134. super.init(coder: coder)
  135. }
  136. override func awakeFromNib() {
  137. super.awakeFromNib()
  138. initDefaultValue()
  139. initSubViews()
  140. }
  141. func initDefaultValue() {
  142. mode = .ciphertext
  143. self.secureTextField.isHidden = false
  144. self.secureTextField.isBezeled = false
  145. self.secureTextField.focusRingType = .none
  146. self.secureTextField.delegate = self
  147. self.textFiled.isHidden = true
  148. self.textFiled.isBezeled = false
  149. self.textFiled.focusRingType = .none
  150. self.textFiled.delegate = self
  151. // self.secureTextField.allow
  152. let cell = self.textFiled.cell as? NSTextFieldCell
  153. cell?.allowedInputSourceLocales = [NSAllRomanInputSourcesLocaleIdentifier]
  154. self.textFiled.firstResponderHandler = { [unowned self] result in
  155. self.updateRightViewStateIfNeed(editing: result)
  156. guard let callback = self.becomeFirstResponderHandler else {
  157. return
  158. }
  159. callback(self)
  160. }
  161. self.secureTextField.firstResponderHandler = { [unowned self] result in
  162. self.updateRightViewStateIfNeed(editing: result)
  163. guard let callback = self.becomeFirstResponderHandler else {
  164. return
  165. }
  166. callback(self)
  167. }
  168. }
  169. func initSubViews() {
  170. self.addSubview(self.backgroundView)
  171. self.addSubview(self.secureTextField)
  172. self.addSubview(self.textFiled)
  173. }
  174. override func layout() {
  175. super.layout()
  176. let width: CGFloat = NSWidth(self.bounds)
  177. let height: CGFloat = NSHeight(self.bounds)
  178. self.backgroundView.frame = self.bounds
  179. var textFieldHeight: CGFloat = 22
  180. if let font = self.textFiled.font {
  181. textFieldHeight = font.pointSize * 1.5
  182. }
  183. let textFieldX: CGFloat = 12
  184. if let rightView = self.rightView {
  185. let rightWidth = NSWidth(rightView.frame)
  186. rightView.frame = NSMakeRect(width-rightWidth, 0, rightWidth, height)
  187. let textFieldFrame = NSMakeRect(textFieldX, (height-textFieldHeight)*0.5, width-textFieldX*2-rightWidth-5, textFieldHeight)
  188. self.secureTextField.frame = textFieldFrame
  189. self.textFiled.frame = textFieldFrame
  190. } else {
  191. let textFieldFrame = NSMakeRect(textFieldX, (height-textFieldHeight)*0.5, width-textFieldX*2, textFieldHeight)
  192. self.secureTextField.frame = textFieldFrame
  193. self.textFiled.frame = textFieldFrame
  194. }
  195. }
  196. func switchMode(mode: KMSecureTextFiledMode) {
  197. self.mode = mode
  198. if (mode == .ciphertext) { /// 密文
  199. self.secureTextField.isHidden = false
  200. self.secureTextField.stringValue = self.textFiled.stringValue
  201. self.window?.makeFirstResponder(self.secureTextField)
  202. self.textFiled.isHidden = true
  203. } else { /// 明文
  204. self.textFiled.isHidden = false
  205. self.textFiled.stringValue = self.secureTextField.stringValue
  206. self.window?.makeFirstResponder(self.textFiled)
  207. self.secureTextField.isHidden = true
  208. }
  209. }
  210. func clear() {
  211. self.secureTextField.stringValue = ""
  212. self.textFiled.stringValue = ""
  213. guard let callback = valueDidChange else {
  214. return
  215. }
  216. callback(self, "")
  217. }
  218. func password() -> String {
  219. return self.mode == .ciphertext ? self.secureTextField.stringValue : self.textFiled.stringValue
  220. }
  221. override func becomeFirstResponder() -> Bool {
  222. self.window?.makeFirstResponder(self.mode == .ciphertext ? self.secureTextField : self.textFiled)
  223. return super.becomeFirstResponder()
  224. }
  225. internal func updateRightViewStateIfNeed(editing: Bool) {
  226. guard let view = self.rightView else {
  227. return
  228. }
  229. if (self.rightViewMode == .always) {
  230. view.isHidden = false
  231. return
  232. }
  233. if (self.rightViewMode == .never) {
  234. view.isHidden = true
  235. return
  236. }
  237. if (editing) { // 开始编辑
  238. view.isHidden = self.rightViewMode != .whileEditing
  239. } else { // 结束编辑
  240. view.isHidden = self.rightViewMode == .whileEditing
  241. }
  242. }
  243. }
  244. extension KMSecureTextFiled: NSTextFieldDelegate {
  245. func controlTextDidChange(_ obj: Notification) {
  246. if (self.mode == .ciphertext) {
  247. if (self.secureTextField.isEqual(to: obj.object)) {
  248. if self.maxLength != 0 {
  249. var string = self.secureTextField.stringValue
  250. if string.count > self.maxLength {
  251. self.secureTextField.stringValue = string.substring(to: self.maxLength)
  252. }
  253. }
  254. guard let callback = valueDidChange else {
  255. return
  256. }
  257. callback(self, self.secureTextField.stringValue)
  258. }
  259. } else {
  260. if (self.textFiled.isEqual(to: obj.object)) {
  261. if self.maxLength != 0 {
  262. var string = self.textFiled.stringValue
  263. if string.count > self.maxLength {
  264. self.textFiled.stringValue = string.substring(to: self.maxLength)
  265. }
  266. }
  267. guard let callback = valueDidChange else {
  268. return
  269. }
  270. callback(self, self.secureTextField.stringValue)
  271. }
  272. }
  273. }
  274. func controlTextDidEndEditing(_ obj: Notification) {
  275. if (!self.secureTextField.isEqual(to: obj.object) && !self.textFiled.isEqual(to: obj.object)) {
  276. return
  277. }
  278. self.updateRightViewStateIfNeed(editing: false)
  279. guard let callback = self.didEndEditHandler else {
  280. return
  281. }
  282. callback(self.password())
  283. }
  284. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  285. switch commandSelector {
  286. case #selector(NSResponder.insertNewline(_:)):
  287. if let inputView = control as? NSTextField {
  288. // //当当前TextField按下enter
  289. if inputView == self.textFiled ||
  290. inputView == self.secureTextField {
  291. guard let callBack = enterAction else { return false}
  292. callBack()
  293. }
  294. }
  295. return true
  296. default:
  297. return false
  298. }
  299. }
  300. }