AccountInputView.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // self.dealConfirmButtonEnabledState(enabled: false)
  78. } else {
  79. view.rightView?.isHidden = false
  80. // self.dealConfirmButtonEnabledState(enabled: true)
  81. }
  82. for char in string {
  83. let str = "\(char)"
  84. if kPwdInputStrings.contains(str) == false {
  85. let err = NSLocalizedString("Password only contains alphabets, numbers and special characters.", comment: "")
  86. self.showPasswordError(err)
  87. break
  88. }
  89. }
  90. }
  91. view.enterAction = { [unowned self] in
  92. // self.confirmButtonAction()
  93. }
  94. return view
  95. }()
  96. private lazy var emailErrorLabel_: NSTextField = {
  97. let view = NSTextField(labelWithString: "")
  98. view.font = .systemFont(ofSize: 12)
  99. view.textColor = NSColor(hex: "#FF0000")
  100. return view
  101. }()
  102. private lazy var passwordErrorLabel_: NSTextField = {
  103. let view = NSTextField(labelWithString: "")
  104. view.font = .systemFont(ofSize: 12)
  105. view.textColor = NSColor(hex: "#FF0000")
  106. return view
  107. }()
  108. var accountTextFiled: KMCustomTextField {
  109. get {
  110. return self.accountTF_
  111. }
  112. }
  113. var passwordTextFiled: KMSecureTextFiled {
  114. get {
  115. return self.passwordTF_
  116. }
  117. }
  118. override func draw(_ dirtyRect: NSRect) {
  119. super.draw(dirtyRect)
  120. // Drawing code here.
  121. }
  122. convenience init() {
  123. self.init(frame: .init(x: 0, y: 0, width: 300, height: 160))
  124. self.initSubviews()
  125. self.initDefaultValue()
  126. }
  127. override func awakeFromNib() {
  128. super.awakeFromNib()
  129. self.initSubviews()
  130. self.initDefaultValue()
  131. }
  132. func initSubviews() {
  133. addSubview(titleLabel)
  134. addSubview(subTitleLabel)
  135. addSubview(button)
  136. addSubview(accountTF_)
  137. addSubview(passwordTF_)
  138. titleLabel.km_add_leading_constraint()
  139. titleLabel.km_add_top_constraint()
  140. subTitleLabel.km_add_leading_constraint()
  141. subTitleLabel.km_add_top_constraint(equalTo: titleLabel, attribute: .bottom, constant: 4)
  142. button.km_add_leading_constraint(equalTo: subTitleLabel, attribute: .trailing, constant: 2)
  143. button.km_add_centerY_constraint(equalTo: subTitleLabel)
  144. accountTF_.km_add_leading_constraint(constant: 0)
  145. accountTF_.km_add_top_constraint(equalTo: subTitleLabel, attribute: .bottom, constant: 30)
  146. accountTF_.km_add_trailing_constraint(constant: 0)
  147. accountTF_.km_add_height_constraint(constant: 44)
  148. passwordTF_.km_add_leading_constraint(constant: 0)
  149. passwordTF_.km_add_top_constraint(equalTo: accountTF_, attribute: .bottom, constant: 24)
  150. passwordTF_.km_add_trailing_constraint(constant: 0)
  151. passwordTF_.km_add_height_constraint(constant: 44)
  152. addSubview(emailErrorLabel_)
  153. addSubview(passwordErrorLabel_)
  154. emailErrorLabel_.km_add_leading_constraint(constant: 0)
  155. emailErrorLabel_.km_add_top_constraint(equalTo: self.accountTF_, attribute: .bottom, constant: 0)
  156. emailErrorLabel_.km_add_height_constraint(constant: 16)
  157. passwordErrorLabel_.km_add_leading_constraint(constant: 0)
  158. passwordErrorLabel_.km_add_top_constraint(equalTo: self.passwordTF_, attribute: .bottom, constant: 0)
  159. passwordErrorLabel_.km_add_height_constraint(constant: 16)
  160. self.emailErrorLabel_.isHidden = true
  161. self.passwordErrorLabel_.isHidden = true
  162. }
  163. func initDefaultValue() {
  164. }
  165. @objc private func _lookButtonAction(sender: NSButton) {
  166. let mode = self.passwordTF_.mode
  167. if mode == .ciphertext {
  168. self.passwordTF_.switchMode(mode: .plaintext)
  169. sender.image = NSImage(named: "KMImageNamePwdHide")
  170. } else {
  171. self.passwordTF_.switchMode(mode: .ciphertext)
  172. sender.image = NSImage(named: "KMImageNamePwdOpen")
  173. }
  174. }
  175. func showAccountError(_ string: String) {
  176. DispatchQueue.main.async {
  177. self.emailErrorLabel_.isHidden = false
  178. self.emailErrorLabel_.stringValue = string
  179. }
  180. }
  181. func hideAccountError() {
  182. DispatchQueue.main.async {
  183. self.emailErrorLabel_.isHidden = true
  184. self.emailErrorLabel_.stringValue = ""
  185. }
  186. }
  187. func showPasswordError(_ string: String) {
  188. DispatchQueue.main.async {
  189. self.passwordErrorLabel_.isHidden = false
  190. self.passwordErrorLabel_.stringValue = string
  191. }
  192. }
  193. func hidePasswordError() {
  194. DispatchQueue.main.async {
  195. self.passwordErrorLabel_.isHidden = true
  196. self.passwordErrorLabel_.stringValue = ""
  197. }
  198. }
  199. }
  200. extension AccountInputView: KMTextFieldDelegate {
  201. func km_controlTextDidChange(textField: AnyObject) {
  202. let string = self.accountTF_.stringValue
  203. let cnt = string?.count ?? 0
  204. if cnt > 50 {
  205. // self.accountTF_.stringValue = string?.substring(to: 50)
  206. let string = NSLocalizedString("Email address must be no longer than 50 characters.", comment: "")
  207. self.showAccountError(string)
  208. } else {
  209. self.hideAccountError()
  210. }
  211. }
  212. }