123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- //
- // AccountInputView.swift
- // PDF Reader Pro
- //
- // Created by User-Tangchao on 2024/10/28.
- //
- import Cocoa
- class AccountInputView: NSView {
- lazy var titleLabel: NSTextField = {
- let view = NSTextField(labelWithString: "")
- view.font = .systemFont(ofSize: 20)
- view.textColor = KMAppearance.titleColor()
- return view
- }()
-
- lazy var subTitleLabel: NSTextField = {
- let view = NSTextField(labelWithString: "")
- return view
- }()
-
- lazy var button: NSButton = {
- let view = NSButton()
- view.isBordered = false
- view.title = ""
- return view
- }()
-
- private lazy var accountTF_: KMCustomTextField = {
- let view = KMCustomTextField()
- view.placeholderString = NSLocalizedString("Email Address", comment: "")
-
- view.backgroundView.wantsLayer = true
- view.backgroundView.layer?.backgroundColor = .white
- view.backgroundView.layer?.borderWidth = 1
- view.backgroundView.layer?.borderColor = NSColor(hex: "#DFE1E5").cgColor
- view.backgroundView.layer?.cornerRadius = 4
- view.delegate = self
- view.offset = 8
- return view
- }()
- private lazy var passwordTF_: KMSecureTextFiled = {
- let view = KMSecureTextFiled()
- view.maxLength = 16
-
- view.backgroundView.wantsLayer = true
- view.backgroundView.layer?.borderWidth = 1
- // border: 1px solid #D9D9D9
- // view.backgroundView.layer?.borderColor = KMAppearance.Layout.mColor().cgColor
- view.backgroundView.layer?.cornerRadius = 4
- view.placeholderString = NSLocalizedString("Password", comment: "")
- view.backgroundView.layer?.backgroundColor = .white
- view.backgroundView.layer?.borderColor = NSColor(hex: "#DFE1E5").cgColor
-
- view.rightViewMode = .always
- let rightView = NSView()
- rightView.frame = NSMakeRect(0, 0, 40, 44);
- view.rightView = rightView
- let clearButton = NSButton()
- rightView.addSubview(clearButton)
- clearButton.frame = NSMakeRect(10, 12, 20, 20)
- clearButton.wantsLayer = true
- clearButton.image = NSImage(named: "KMImageNamePwdOpen")
- clearButton.isBordered = false
- clearButton.target = self
- clearButton.action = #selector(_lookButtonAction)
- rightView.isHidden = true
- view.becomeFirstResponderHandler = { [unowned self] securetextFiled in
- // let mySecureTextField: KMSecureTextFiled = securetextFiled as! KMSecureTextFiled
- // mySecureTextField.backgroundView.wantsLayer = true
- // mySecureTextField.backgroundView.layer?.borderColor = KMAppearance.Layout.mColor().cgColor
-
- // if mySecureTextField.password().isEmpty {
- // self.secureTextFiled.rightView?.isHidden = true
- // } else {
- // self.secureTextFiled.rightView?.isHidden = false
- // }
- // self.passwordErrorLabel.isHidden = true
- }
- view.valueDidChange = { [unowned self] view, string in
- // view.backgroundView.layer?.borderColor = KMAppearance.Layout.mColor().cgColor
- self.hidePasswordError()
- if string.isEmpty {
- view.rightView?.isHidden = true
- } else {
- view.rightView?.isHidden = false
- }
- for char in string {
- let str = "\(char)"
- if kPwdInputStrings.contains(str) == false {
- let err = NSLocalizedString("Password only contains alphabets, numbers and special characters.", comment: "")
- self.showPasswordError(err)
- break
- }
- }
- }
-
- view.enterAction = { [unowned self] in
- // self.confirmButtonAction()
- }
-
- return view
- }()
-
- private lazy var emailErrorLabel_: NSTextField = {
- let view = NSTextField(labelWithString: "")
- view.font = .systemFont(ofSize: 12)
- view.textColor = NSColor(hex: "#FF0000")
- return view
- }()
-
- private lazy var passwordErrorLabel_: NSTextField = {
- let view = NSTextField(labelWithString: "")
- view.font = .systemFont(ofSize: 12)
- view.textColor = NSColor(hex: "#FF0000")
- return view
- }()
-
- var accountTextFiled: KMCustomTextField {
- get {
- return self.accountTF_
- }
- }
-
- var passwordTextFiled: KMSecureTextFiled {
- get {
- return self.passwordTF_
- }
- }
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
-
- convenience init() {
- self.init(frame: .init(x: 0, y: 0, width: 300, height: 160))
-
- self.initSubviews()
- self.initDefaultValue()
- }
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
- self.initSubviews()
- self.initDefaultValue()
- }
-
- func initSubviews() {
- addSubview(titleLabel)
- addSubview(subTitleLabel)
- addSubview(button)
-
- addSubview(accountTF_)
- addSubview(passwordTF_)
-
- titleLabel.km_add_leading_constraint()
- titleLabel.km_add_top_constraint()
- subTitleLabel.km_add_leading_constraint()
- subTitleLabel.km_add_top_constraint(equalTo: titleLabel, attribute: .bottom, constant: 4)
- button.km_add_leading_constraint(equalTo: subTitleLabel, attribute: .trailing, constant: 2)
- button.km_add_centerY_constraint(equalTo: subTitleLabel)
-
- accountTF_.km_add_leading_constraint(constant: 0)
- accountTF_.km_add_top_constraint(equalTo: subTitleLabel, attribute: .bottom, constant: 30)
- accountTF_.km_add_trailing_constraint(constant: 0)
- accountTF_.km_add_height_constraint(constant: 44)
-
- passwordTF_.km_add_leading_constraint(constant: 0)
- passwordTF_.km_add_top_constraint(equalTo: accountTF_, attribute: .bottom, constant: 24)
- passwordTF_.km_add_trailing_constraint(constant: 0)
- passwordTF_.km_add_height_constraint(constant: 44)
-
- addSubview(emailErrorLabel_)
- addSubview(passwordErrorLabel_)
- emailErrorLabel_.km_add_leading_constraint(constant: 0)
- emailErrorLabel_.km_add_top_constraint(equalTo: self.accountTF_, attribute: .bottom, constant: 0)
- emailErrorLabel_.km_add_height_constraint(constant: 16)
- passwordErrorLabel_.km_add_leading_constraint(constant: 0)
- passwordErrorLabel_.km_add_top_constraint(equalTo: self.passwordTF_, attribute: .bottom, constant: 0)
- passwordErrorLabel_.km_add_height_constraint(constant: 16)
-
- self.emailErrorLabel_.isHidden = true
- self.passwordErrorLabel_.isHidden = true
-
-
- }
-
- func initDefaultValue() {
-
- }
-
- @objc private func _lookButtonAction(sender: NSButton) {
- let mode = self.passwordTF_.mode
- if mode == .ciphertext {
- self.passwordTF_.switchMode(mode: .plaintext)
- sender.image = NSImage(named: "KMImageNamePwdHide")
- } else {
- self.passwordTF_.switchMode(mode: .ciphertext)
- sender.image = NSImage(named: "KMImageNamePwdOpen")
- }
- }
-
- func showAccountError(_ string: String) {
- DispatchQueue.main.async {
- self.emailErrorLabel_.isHidden = false
- self.emailErrorLabel_.stringValue = string
- }
- }
-
- func hideAccountError() {
- DispatchQueue.main.async {
- self.emailErrorLabel_.isHidden = true
- self.emailErrorLabel_.stringValue = ""
- }
- }
-
- func showPasswordError(_ string: String) {
- DispatchQueue.main.async {
- self.passwordErrorLabel_.isHidden = false
- self.passwordErrorLabel_.stringValue = string
- }
- }
-
- func hidePasswordError() {
- DispatchQueue.main.async {
- self.passwordErrorLabel_.isHidden = true
- self.passwordErrorLabel_.stringValue = ""
- }
- }
- }
- extension AccountInputView: KMTextFieldDelegate {
- func km_controlTextDidChange(textField: AnyObject) {
- let string = self.accountTF_.stringValue
- let cnt = string?.count ?? 0
- if cnt > 50 {
- // self.accountTF_.stringValue = string?.substring(to: 50)
- let string = NSLocalizedString("Email address must be no longer than 50 characters.", comment: "")
- self.showAccountError(string)
- } else {
- self.hideAccountError()
- }
- }
- }
|