KMSecureEncryptPasswordCellView.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // KMSecureEncryptPasswordCellView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/11/28.
  6. //
  7. import Cocoa
  8. class KMSecureEncryptPasswordCellView: NSTableCellView {
  9. var checkBox: NSButton = NSButton(checkboxWithTitle: "", target: nil, action: nil)
  10. private var passwordView: KMSecureTextFiled = KMSecureTextFiled()
  11. private var openOrClosePasswordButton: NSButton?
  12. private var hover = KMCoverButton()
  13. var kmEnabled = false
  14. private var isFocus = false
  15. var itemClick: KMItemClickBlock<KMSecureEncryptPasswordCellView>?
  16. var valueChange: KMValueDidChangeBlock?
  17. func setPlaceholderString(_ string: String) {
  18. self.passwordView.placeholderString = string
  19. }
  20. override init(frame frameRect: NSRect) {
  21. super.init(frame: frameRect)
  22. self.initSubViews()
  23. }
  24. required init?(coder: NSCoder) {
  25. super.init(coder: coder)
  26. self.initSubViews()
  27. }
  28. override var isFlipped: Bool {
  29. return true
  30. }
  31. func initSubViews() {
  32. self.addSubview(self.checkBox)
  33. self.addSubview(self.passwordView)
  34. self.hover.isBordered = false
  35. self.hover.title = ""
  36. self.hover.wantsLayer = true
  37. self.hover.layer?.backgroundColor = .clear
  38. self.addSubview(self.hover, positioned: .below, relativeTo: self.passwordView)
  39. self.hover.coverAction = { [unowned self] _, action in
  40. if (!self.passwordViewCanHover()) {
  41. return
  42. }
  43. if (action == .enter) {
  44. self.refreshPasswordView(state: .Hov)
  45. } else if (action == .exit) {
  46. self.refreshPasswordView(state: .Norm)
  47. }
  48. }
  49. self.checkBox.target = self
  50. self.checkBox.action = #selector(checkBoxAction)
  51. self.passwordView.backgroundView.wantsLayer = true
  52. self.passwordView.backgroundView.layer?.borderWidth = 1
  53. self.passwordView.backgroundView.layer?.cornerRadius = 4
  54. self.passwordView.kmEnabled = false
  55. self.isFocus = false
  56. self.refreshPasswordView(state: .Norm)
  57. let rightView = NSView()
  58. rightView.frame = NSMakeRect(0, 0, 32, 32);
  59. self.passwordView.rightView = rightView
  60. let openPasswordButton = NSButton()
  61. openPasswordButton.image = NSImage(named: "KMImageNameSecureHide")
  62. openPasswordButton.isEnabled = false
  63. rightView.addSubview(openPasswordButton)
  64. openPasswordButton.frame = NSMakeRect(8, 8, 16, 16)
  65. openPasswordButton.wantsLayer = true
  66. openPasswordButton.title = ""
  67. openPasswordButton.isBordered = false
  68. openPasswordButton.target = self
  69. openPasswordButton.action = #selector(openOrClosePasswordButtonAction)
  70. self.openOrClosePasswordButton = openPasswordButton
  71. self.passwordView.valueDidChange = { [unowned self] string in
  72. self.isFocus = true
  73. self.refreshPasswordView(state: .Focus)
  74. guard let callback = self.valueChange else {
  75. return
  76. }
  77. self.updatePasswordViewRightViewState()
  78. callback(string, nil)
  79. }
  80. self.passwordView.becomeFirstResponderHandler = { [unowned self] result in
  81. self.isFocus = true
  82. self.refreshPasswordView(state: .Focus)
  83. self.updatePasswordViewRightViewState()
  84. }
  85. self.passwordView.didEndEditHandler = { [unowned self] _ in
  86. self.isFocus = false
  87. self.refreshPasswordView(state: .Norm)
  88. self.updatePasswordViewRightViewState()
  89. }
  90. }
  91. override func layout() {
  92. super.layout()
  93. let width: CGFloat = NSWidth(self.bounds)
  94. self.checkBox.frame = NSMakeRect(8, 6, width-12*2, 22)
  95. let passwordY: CGFloat = self.checkBox.frame.maxY + 8
  96. self.passwordView.frame = NSMakeRect(8, passwordY, width-8-8, 32)
  97. self.hover.frame = self.passwordView.frame
  98. }
  99. @objc func checkBoxAction(sender: NSButton) {
  100. if (sender.state == .on) {
  101. self.passwordView.kmEnabled = true
  102. self.kmEnabled = true
  103. self.openOrClosePasswordButton?.isEnabled = true
  104. let _ = self.passwordView.becomeFirstResponder()
  105. } else {
  106. self.passwordView.kmEnabled = false
  107. self.kmEnabled = false
  108. self.openOrClosePasswordButton?.isEnabled = false
  109. }
  110. self.updatePasswordViewRightViewState()
  111. guard let callback = self.itemClick else {
  112. return
  113. }
  114. callback(self, 1)
  115. }
  116. @objc func openOrClosePasswordButtonAction(sender: NSButton) {
  117. if (self.passwordView.mode == .ciphertext) { /// 切换到明文
  118. self.openOrClosePasswordButton?.image = NSImage(named: "KMImageNameSecureShow")
  119. self.passwordView.switchMode(mode: .plaintext)
  120. } else { /// 切换到密文
  121. self.openOrClosePasswordButton?.image = NSImage(named: "KMImageNameSecureHide")
  122. self.passwordView.switchMode(mode: .ciphertext)
  123. }
  124. }
  125. func updatePasswordViewRightViewState() {
  126. if (self.passwordView.password().isEmpty) {
  127. self.passwordView.rightView?.isHidden = true
  128. } else {
  129. self.passwordView.rightView?.isHidden = false
  130. }
  131. }
  132. private func passwordViewCanHover() -> Bool {
  133. if (!self.passwordView.kmEnabled) {
  134. return false
  135. }
  136. if (self.isFocus) {
  137. return false
  138. }
  139. return true
  140. }
  141. private func refreshPasswordView(state: KMDesignTokenState) {
  142. if (state == .Focus) {
  143. self.passwordView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#1770F4").cgColor
  144. } else if (state == .Norm) {
  145. self.passwordView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  146. } else if (state == .Hov) {
  147. self.passwordView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#68ACF8").cgColor
  148. }
  149. }
  150. }