123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- //
- // KMSecureEncryptPasswordCellView.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2022/11/28.
- //
- import Cocoa
- class KMSecureEncryptPasswordCellView: NSTableCellView {
-
- var checkBox: NSButton = NSButton(checkboxWithTitle: "", target: nil, action: nil)
- private var passwordView: KMSecureTextFiled = KMSecureTextFiled()
- private var openOrClosePasswordButton: NSButton?
-
- private var hover = KMCoverButton()
-
- var kmEnabled = false
- private var isFocus = false
-
- var itemClick: KMItemClickBlock<KMSecureEncryptPasswordCellView>?
- var valueChange: KMValueDidChangeBlock?
-
- func setPlaceholderString(_ string: String) {
- self.passwordView.placeholderString = string
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- self.initSubViews()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- self.initSubViews()
- }
-
- override var isFlipped: Bool {
- return true
- }
-
- func initSubViews() {
- self.addSubview(self.checkBox)
- self.addSubview(self.passwordView)
-
- self.hover.isBordered = false
- self.hover.title = ""
- self.hover.wantsLayer = true
- self.hover.layer?.backgroundColor = .clear
- self.addSubview(self.hover, positioned: .below, relativeTo: self.passwordView)
- self.hover.coverAction = { [unowned self] _, action in
- if (!self.passwordViewCanHover()) {
- return
- }
- if (action == .enter) {
- self.refreshPasswordView(state: .Hov)
- } else if (action == .exit) {
- self.refreshPasswordView(state: .Norm)
- }
- }
-
- self.checkBox.target = self
- self.checkBox.action = #selector(checkBoxAction)
-
- self.passwordView.backgroundView.wantsLayer = true
- self.passwordView.backgroundView.layer?.borderWidth = 1
- self.passwordView.backgroundView.layer?.cornerRadius = 4
- self.passwordView.kmEnabled = false
- self.isFocus = false
- self.refreshPasswordView(state: .Norm)
-
- let rightView = NSView()
- rightView.frame = NSMakeRect(0, 0, 32, 32);
- self.passwordView.rightView = rightView
- let openPasswordButton = NSButton()
- openPasswordButton.image = NSImage(named: "KMImageNameSecureHide")
- openPasswordButton.isEnabled = false
- rightView.addSubview(openPasswordButton)
- openPasswordButton.frame = NSMakeRect(8, 8, 16, 16)
- openPasswordButton.wantsLayer = true
- openPasswordButton.title = ""
- openPasswordButton.isBordered = false
- openPasswordButton.target = self
- openPasswordButton.action = #selector(openOrClosePasswordButtonAction)
- self.openOrClosePasswordButton = openPasswordButton
-
- self.passwordView.valueDidChange = { [weak self] view, string in
- self?.isFocus = true
- self?.refreshPasswordView(state: .Focus)
-
- guard let callback = self?.valueChange else {
- return
- }
-
- self?.updatePasswordViewRightViewState()
- callback(string, nil)
- }
- self.passwordView.becomeFirstResponderHandler = { [weak self] result in
- self?.isFocus = true
- self?.refreshPasswordView(state: .Focus)
- self?.updatePasswordViewRightViewState()
- }
- self.passwordView.didEndEditHandler = { [weak self] _ in
- self?.isFocus = false
- self?.refreshPasswordView(state: .Norm)
- self?.updatePasswordViewRightViewState()
- }
- }
-
- override func layout() {
- super.layout()
-
- let width: CGFloat = NSWidth(self.bounds)
- self.checkBox.frame = NSMakeRect(8, 6, width-12*2, 22)
-
- let passwordY: CGFloat = self.checkBox.frame.maxY + 8
- self.passwordView.frame = NSMakeRect(8, passwordY, width-8-8, 32)
-
- self.hover.frame = self.passwordView.frame
- }
- @objc func checkBoxAction(sender: NSButton) {
- if (sender.state == .on) {
- self.passwordView.kmEnabled = true
- self.kmEnabled = true
- self.openOrClosePasswordButton?.isEnabled = true
-
- let _ = self.passwordView.becomeFirstResponder()
- } else {
- self.passwordView.kmEnabled = false
- self.kmEnabled = false
- self.openOrClosePasswordButton?.isEnabled = false
- }
-
- self.updatePasswordViewRightViewState()
- guard let callback = self.itemClick else {
- return
- }
-
- callback(self, 1)
- }
-
- @objc func openOrClosePasswordButtonAction(sender: NSButton) {
- if (self.passwordView.mode == .ciphertext) { /// 切换到明文
- self.openOrClosePasswordButton?.image = NSImage(named: "KMImageNameSecureShow")
- self.passwordView.switchMode(mode: .plaintext)
- } else { /// 切换到密文
- self.openOrClosePasswordButton?.image = NSImage(named: "KMImageNameSecureHide")
- self.passwordView.switchMode(mode: .ciphertext)
- }
- }
-
- func updatePasswordViewRightViewState() {
- if (self.passwordView.password().isEmpty) {
- self.passwordView.rightView?.isHidden = true
- } else {
- self.passwordView.rightView?.isHidden = false
- }
- }
-
- private func passwordViewCanHover() -> Bool {
- if (!self.passwordView.kmEnabled) {
- return false
- }
- if (self.isFocus) {
- return false
- }
- return true
- }
-
- private func refreshPasswordView(state: KMDesignTokenState) {
- if (state == .Focus) {
- self.passwordView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#1770F4").cgColor
- } else if (state == .Norm) {
- self.passwordView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
- } else if (state == .Hov) {
- self.passwordView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#68ACF8").cgColor
- }
- }
- }
|