KMWatermarkSaveWindow.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // KMWatermarkSaveWindow.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/5.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMWatermarkSaveWindow: KMNBaseWindowController {
  10. @IBOutlet var contendView: NSView!
  11. @IBOutlet var titleLabel: NSTextField!
  12. @IBOutlet var nameLabel: NSTextField!
  13. @IBOutlet var nameInputView: ComponentInput!
  14. @IBOutlet var cancelButton: ComponentButton!
  15. @IBOutlet var saveButton: ComponentButton!
  16. public var saveHandler: ((String?) -> Void)!
  17. public var nameValue: String = ""
  18. override func windowDidLoad() {
  19. super.windowDidLoad()
  20. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  21. setupProperty()
  22. }
  23. override func beginSheetFinish() {
  24. super.beginSheetFinish()
  25. reloadData()
  26. window?.makeFirstResponder(nil)
  27. nameInputView.beginEditing()
  28. }
  29. func setupProperty() {
  30. contendView.wantsLayer = true
  31. contendView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/popup").cgColor
  32. titleLabel.stringValue = KMLocalizedString("Add Watermark")
  33. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  34. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  35. nameLabel.stringValue = KMLocalizedString("Name:")
  36. nameLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-nor")
  37. nameLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-regular")
  38. nameInputView.properties = ComponentInputProperty(size: .m,
  39. state:.normal,
  40. showClear: true,
  41. placeholder: "",
  42. text: "")
  43. nameInputView.delegate = self
  44. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, buttonText: KMLocalizedString("Cancel"), keepPressState: false)
  45. cancelButton.setTarget(self, action: #selector(buttonClicked(_:)))
  46. saveButton.properties = ComponentButtonProperty(type: .primary, size: .s, buttonText: KMLocalizedString("Save"), keepPressState: false)
  47. saveButton.setTarget(self, action: #selector(buttonClicked(_:)))
  48. }
  49. func reloadData() {
  50. nameInputView.properties.text = nameValue
  51. nameInputView.reloadData()
  52. if nameValue.isEmpty == true {
  53. saveButton.properties.isDisabled = true
  54. } else {
  55. saveButton.properties.isDisabled = false
  56. }
  57. saveButton.reloadData()
  58. }
  59. //MARK: - action
  60. @objc func buttonClicked(_ sender: ComponentButton) {
  61. if sender == saveButton {
  62. guard let callback = self.saveHandler else {
  63. return
  64. }
  65. callback(nameInputView.properties.text)
  66. }
  67. self.own_closeEndSheet()
  68. }
  69. //MARK: - MouseEvent
  70. override func mouseDown(with event: NSEvent) {
  71. super.mouseDown(with: event)
  72. window?.makeFirstResponder(nil)
  73. }
  74. }
  75. //MARK: - ComponentInputDelegate
  76. extension KMWatermarkSaveWindow: ComponentInputDelegate {
  77. func componentInputDidChanged(inputView: ComponentInput) {
  78. nameValue = inputView.properties.text
  79. reloadData()
  80. }
  81. func componentInputDidEndEditing(inputView: ComponentInput) {
  82. nameValue = inputView.properties.text
  83. reloadData()
  84. }
  85. }