AccountInputView.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // AccountInputView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/10/28.
  6. //
  7. import Cocoa
  8. class AccountInputView: NSView {
  9. lazy var titleLabel: NSTextField = {
  10. let view = NSTextField(labelWithString: "")
  11. view.font = .systemFont(ofSize: 20)
  12. view.textColor = KMAppearance.titleColor()
  13. return view
  14. }()
  15. lazy var subTitleLabel: NSTextField = {
  16. let view = NSTextField(labelWithString: "")
  17. return view
  18. }()
  19. lazy var button: NSButton = {
  20. let view = NSButton()
  21. view.isBordered = false
  22. view.title = ""
  23. return view
  24. }()
  25. private lazy var accountTF_: KMCustomTextField = {
  26. let view = KMCustomTextField()
  27. view.placeholderString = NSLocalizedString("Email Address", comment: "")
  28. view.backgroundView.wantsLayer = true
  29. view.backgroundView.layer?.backgroundColor = .white
  30. view.backgroundView.layer?.borderWidth = 1
  31. view.backgroundView.layer?.borderColor = NSColor(hex: "#DFE1E5").cgColor
  32. view.backgroundView.layer?.cornerRadius = 4
  33. view.delegate = self
  34. view.offset = 8
  35. return view
  36. }()
  37. private lazy var passwordTF_: KMSecureTextFiled = {
  38. let view = KMSecureTextFiled()
  39. view.maxLength = 16
  40. view.backgroundView.wantsLayer = true
  41. view.backgroundView.layer?.borderWidth = 1
  42. // border: 1px solid #D9D9D9
  43. // view.backgroundView.layer?.borderColor = KMAppearance.Layout.mColor().cgColor
  44. view.backgroundView.layer?.cornerRadius = 4
  45. view.placeholderString = NSLocalizedString("Password", comment: "")
  46. view.backgroundView.layer?.backgroundColor = .white
  47. view.backgroundView.layer?.borderColor = NSColor(hex: "#DFE1E5").cgColor
  48. view.rightViewMode = .always
  49. let rightView = NSView()
  50. rightView.frame = NSMakeRect(0, 0, 40, 44);
  51. view.rightView = rightView
  52. let clearButton = NSButton()
  53. rightView.addSubview(clearButton)
  54. clearButton.frame = NSMakeRect(10, 12, 20, 20)
  55. clearButton.wantsLayer = true
  56. clearButton.image = NSImage(named: "KMImageNamePwdOpen")
  57. clearButton.isBordered = false
  58. clearButton.target = self
  59. clearButton.action = #selector(_lookButtonAction)
  60. rightView.isHidden = true
  61. view.becomeFirstResponderHandler = { [unowned self] securetextFiled in
  62. // let mySecureTextField: KMSecureTextFiled = securetextFiled as! KMSecureTextFiled
  63. // mySecureTextField.backgroundView.wantsLayer = true
  64. // mySecureTextField.backgroundView.layer?.borderColor = KMAppearance.Layout.mColor().cgColor
  65. // if mySecureTextField.password().isEmpty {
  66. // self.secureTextFiled.rightView?.isHidden = true
  67. // } else {
  68. // self.secureTextFiled.rightView?.isHidden = false
  69. // }
  70. // self.passwordErrorLabel.isHidden = true
  71. }
  72. view.valueDidChange = { [unowned self] view, string in
  73. // view.backgroundView.layer?.borderColor = KMAppearance.Layout.mColor().cgColor
  74. self.hidePasswordError()
  75. if string.isEmpty {
  76. view.rightView?.isHidden = true
  77. } else {
  78. view.rightView?.isHidden = false
  79. }
  80. for char in string {
  81. let str = "\(char)"
  82. if kPwdInputStrings.contains(str) == false {
  83. let err = NSLocalizedString("Password only contains alphabets, numbers and special characters.", comment: "")
  84. self.showPasswordError(err)
  85. break
  86. }
  87. }
  88. }
  89. view.enterAction = { [unowned self] in
  90. // self.confirmButtonAction()
  91. }
  92. return view
  93. }()
  94. private lazy var emailErrorLabel_: NSTextField = {
  95. let view = NSTextField(labelWithString: "")
  96. view.font = .systemFont(ofSize: 12)
  97. view.textColor = NSColor(hex: "#FF0000")
  98. return view
  99. }()
  100. private lazy var passwordErrorLabel_: NSTextField = {
  101. let view = NSTextField(labelWithString: "")
  102. view.font = .systemFont(ofSize: 12)
  103. view.textColor = NSColor(hex: "#FF0000")
  104. return view
  105. }()
  106. var accountTextFiled: KMCustomTextField {
  107. get {
  108. return self.accountTF_
  109. }
  110. }
  111. var passwordTextFiled: KMSecureTextFiled {
  112. get {
  113. return self.passwordTF_
  114. }
  115. }
  116. override func draw(_ dirtyRect: NSRect) {
  117. super.draw(dirtyRect)
  118. // Drawing code here.
  119. }
  120. convenience init() {
  121. self.init(frame: .init(x: 0, y: 0, width: 300, height: 160))
  122. self.initSubviews()
  123. self.initDefaultValue()
  124. }
  125. override func awakeFromNib() {
  126. super.awakeFromNib()
  127. self.initSubviews()
  128. self.initDefaultValue()
  129. }
  130. func initSubviews() {
  131. addSubview(titleLabel)
  132. addSubview(subTitleLabel)
  133. addSubview(button)
  134. addSubview(accountTF_)
  135. addSubview(passwordTF_)
  136. titleLabel.km_add_leading_constraint()
  137. titleLabel.km_add_top_constraint()
  138. subTitleLabel.km_add_leading_constraint()
  139. subTitleLabel.km_add_top_constraint(equalTo: titleLabel, attribute: .bottom, constant: 4)
  140. button.km_add_leading_constraint(equalTo: subTitleLabel, attribute: .trailing, constant: 2)
  141. button.km_add_centerY_constraint(equalTo: subTitleLabel)
  142. accountTF_.km_add_leading_constraint(constant: 0)
  143. accountTF_.km_add_top_constraint(equalTo: subTitleLabel, attribute: .bottom, constant: 30)
  144. accountTF_.km_add_trailing_constraint(constant: 0)
  145. accountTF_.km_add_height_constraint(constant: 44)
  146. passwordTF_.km_add_leading_constraint(constant: 0)
  147. passwordTF_.km_add_top_constraint(equalTo: accountTF_, attribute: .bottom, constant: 24)
  148. passwordTF_.km_add_trailing_constraint(constant: 0)
  149. passwordTF_.km_add_height_constraint(constant: 44)
  150. addSubview(emailErrorLabel_)
  151. addSubview(passwordErrorLabel_)
  152. emailErrorLabel_.km_add_leading_constraint(constant: 0)
  153. emailErrorLabel_.km_add_top_constraint(equalTo: self.accountTF_, attribute: .bottom, constant: 0)
  154. emailErrorLabel_.km_add_height_constraint(constant: 16)
  155. passwordErrorLabel_.km_add_leading_constraint(constant: 0)
  156. passwordErrorLabel_.km_add_top_constraint(equalTo: self.passwordTF_, attribute: .bottom, constant: 0)
  157. passwordErrorLabel_.km_add_height_constraint(constant: 16)
  158. self.emailErrorLabel_.isHidden = true
  159. self.passwordErrorLabel_.isHidden = true
  160. }
  161. func initDefaultValue() {
  162. }
  163. @objc private func _lookButtonAction(sender: NSButton) {
  164. let mode = self.passwordTF_.mode
  165. if mode == .ciphertext {
  166. self.passwordTF_.switchMode(mode: .plaintext)
  167. sender.image = NSImage(named: "KMImageNamePwdHide")
  168. } else {
  169. self.passwordTF_.switchMode(mode: .ciphertext)
  170. sender.image = NSImage(named: "KMImageNamePwdOpen")
  171. }
  172. }
  173. func showAccountError(_ string: String) {
  174. DispatchQueue.main.async {
  175. self.emailErrorLabel_.isHidden = false
  176. self.emailErrorLabel_.stringValue = string
  177. }
  178. }
  179. func hideAccountError() {
  180. DispatchQueue.main.async {
  181. self.emailErrorLabel_.isHidden = true
  182. self.emailErrorLabel_.stringValue = ""
  183. }
  184. }
  185. func showPasswordError(_ string: String) {
  186. DispatchQueue.main.async {
  187. self.passwordErrorLabel_.isHidden = false
  188. self.passwordErrorLabel_.stringValue = string
  189. }
  190. }
  191. func hidePasswordError() {
  192. DispatchQueue.main.async {
  193. self.passwordErrorLabel_.isHidden = true
  194. self.passwordErrorLabel_.stringValue = ""
  195. }
  196. }
  197. }
  198. extension AccountInputView: KMTextFieldDelegate {
  199. func km_controlTextDidChange(textField: AnyObject) {
  200. let string = self.accountTF_.stringValue
  201. let cnt = string?.count ?? 0
  202. if cnt > 50 {
  203. // self.accountTF_.stringValue = string?.substring(to: 50)
  204. let string = NSLocalizedString("Email address must be no longer than 50 characters.", comment: "")
  205. self.showAccountError(string)
  206. } else {
  207. self.hideAccountError()
  208. }
  209. }
  210. }