KMSecureEncryptPasswordCellView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. var kmEnabled = false
  13. var itemClick: KMItemClickBlock<KMSecureEncryptPasswordCellView>?
  14. var valueChange: KMValueDidChangeBlock?
  15. func setPlaceholderString(_ string: String) {
  16. self.passwordView.placeholderString = string
  17. }
  18. override init(frame frameRect: NSRect) {
  19. super.init(frame: frameRect)
  20. self.initSubViews()
  21. }
  22. required init?(coder: NSCoder) {
  23. super.init(coder: coder)
  24. self.initSubViews()
  25. }
  26. override var isFlipped: Bool {
  27. return true
  28. }
  29. func initSubViews() {
  30. self.addSubview(self.checkBox)
  31. self.addSubview(self.passwordView)
  32. self.checkBox.target = self
  33. self.checkBox.action = #selector(checkBoxAction)
  34. self.passwordView.backgroundView.wantsLayer = true
  35. self.passwordView.backgroundView.layer?.borderWidth = 1
  36. self.passwordView.backgroundView.layer?.borderColor = NSColor(hex: "#DFE1E5").cgColor
  37. self.passwordView.backgroundView.layer?.cornerRadius = 4
  38. self.passwordView.kmEnabled = false
  39. let rightView = NSView()
  40. rightView.frame = NSMakeRect(0, 0, 32, 32);
  41. self.passwordView.rightView = rightView
  42. let openPasswordButton = NSButton()
  43. openPasswordButton.image = NSImage(named: "KMImageNameSecureHide")
  44. openPasswordButton.isEnabled = false
  45. rightView.addSubview(openPasswordButton)
  46. openPasswordButton.frame = NSMakeRect(8, 8, 16, 16)
  47. openPasswordButton.wantsLayer = true
  48. openPasswordButton.title = ""
  49. openPasswordButton.isBordered = false
  50. openPasswordButton.target = self
  51. openPasswordButton.action = #selector(openOrClosePasswordButtonAction)
  52. self.openOrClosePasswordButton = openPasswordButton
  53. self.passwordView.valueDidChange = { [unowned self] string in
  54. guard let callback = self.valueChange else {
  55. return
  56. }
  57. self.updatePasswordViewRightViewState()
  58. callback(string, nil)
  59. }
  60. self.passwordView.becomeFirstResponderHandler = { [unowned self] result in
  61. self.updatePasswordViewRightViewState()
  62. }
  63. self.passwordView.didEndEditHandler = { [unowned self] _ in
  64. self.updatePasswordViewRightViewState()
  65. }
  66. }
  67. override func layout() {
  68. super.layout()
  69. let width: CGFloat = NSWidth(self.bounds)
  70. self.checkBox.frame = NSMakeRect(8, 6, width-12*2, 22)
  71. let passwordY: CGFloat = self.checkBox.frame.maxY + 8
  72. self.passwordView.frame = NSMakeRect(8, passwordY, width-8-8, 32)
  73. }
  74. @objc func checkBoxAction(sender: NSButton) {
  75. if (sender.state == .on) {
  76. self.passwordView.kmEnabled = true
  77. self.kmEnabled = true
  78. self.openOrClosePasswordButton?.isEnabled = true
  79. self.passwordView.backgroundView.wantsLayer = true
  80. self.passwordView.backgroundView.layer?.borderColor = NSColor(hex: "#DFE1E5").cgColor
  81. let _ = self.passwordView.becomeFirstResponder()
  82. } else {
  83. self.passwordView.kmEnabled = false
  84. self.kmEnabled = false
  85. self.openOrClosePasswordButton?.isEnabled = false
  86. self.passwordView.backgroundView.wantsLayer = true
  87. self.passwordView.backgroundView.layer?.borderColor = NSColor(hex: "#DFE1E5").cgColor
  88. }
  89. self.updatePasswordViewRightViewState()
  90. guard let callback = self.itemClick else {
  91. return
  92. }
  93. callback(self, 1)
  94. }
  95. @objc func openOrClosePasswordButtonAction(sender: NSButton) {
  96. if (self.passwordView.mode == .ciphertext) { /// 切换到明文
  97. self.openOrClosePasswordButton?.image = NSImage(named: "KMImageNameSecureShow")
  98. self.passwordView.switchMode(mode: .plaintext)
  99. } else { /// 切换到密文
  100. self.openOrClosePasswordButton?.image = NSImage(named: "KMImageNameSecureHide")
  101. self.passwordView.switchMode(mode: .ciphertext)
  102. }
  103. }
  104. func updatePasswordViewRightViewState() {
  105. if (self.passwordView.password().isEmpty) {
  106. self.passwordView.rightView?.isHidden = true
  107. } else {
  108. self.passwordView.rightView?.isHidden = false
  109. }
  110. }
  111. }