KMEditToolbarView.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // KMEditToolbarView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/5.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMEditToolbarViewDelegate: AnyObject {
  10. @objc optional func kmEditToolbarViewDidUpdateMode(_ view: KMEditToolbarView)
  11. @objc optional func kmEditToolbarViewDidChooseBatch(_ view: KMEditToolbarView)
  12. @objc optional func kmEditToolbarViewDidChooseApply(_ view: KMEditToolbarView)
  13. @objc optional func kmEditToolbarViewDidChooseExit(_ view: KMEditToolbarView)
  14. }
  15. class KMEditToolbarView: BaseXibView {
  16. @IBOutlet var contendView: NSView!
  17. @IBOutlet var titleLabel: NSTextField!
  18. @IBOutlet var pageRangeSelectView: KMPageRangeSelectView!
  19. @IBOutlet var sepDivider: ComponentDivider!
  20. @IBOutlet var templateButton: ComponentButton!
  21. @IBOutlet var addButton: ComponentButton!
  22. @IBOutlet var batchButton: ComponentButton!
  23. @IBOutlet var applyButton: ComponentButton!
  24. @IBOutlet var exitButton: ComponentButton!
  25. @IBOutlet var templateButtonWidthConst: NSLayoutConstraint!
  26. @IBOutlet var addButtonWidthConst: NSLayoutConstraint!
  27. @IBOutlet var batchButtonWidthConst: NSLayoutConstraint!
  28. weak open var delegate: KMEditToolbarViewDelegate?
  29. var editType: KMPDFEditModeType = .none
  30. var editSubType: KMPDFEditSubModeType = .none
  31. var applyEnable: Bool = false
  32. //MARK: - func
  33. public override func draw(_ dirtyRect: NSRect) {
  34. super.draw(dirtyRect)
  35. // Drawing code here.
  36. }
  37. public required init?(coder decoder: NSCoder) {
  38. super.init(coder: decoder)
  39. }
  40. override init(frame frameRect: NSRect) {
  41. super.init(frame: frameRect)
  42. }
  43. public override func awakeFromNib() {
  44. super.awakeFromNib()
  45. self.setupProperty()
  46. }
  47. func setupProperty() {
  48. contendView.wantsLayer = true
  49. contendView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  50. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  51. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  52. sepDivider.properties = ComponentDividerProperty(type: .vertical, dash: false)
  53. templateButton.properties = ComponentButtonProperty(type: .text_gray_opacity, size: .xs, showLeftIcon: true, buttonText: KMLocalizedString("Templates"), icon: NSImage(named: "icon_wm_template"))
  54. templateButtonWidthConst.constant = templateButton.properties.propertyInfo.viewWidth
  55. templateButton.setTarget(self, action: #selector(buttonClicked(_:)))
  56. addButton.properties = ComponentButtonProperty(type: .text_gray_opacity, size: .xs, showLeftIcon: true, buttonText: KMLocalizedString("Add Watermark"), icon: NSImage(named: "toolbar_plus"))
  57. addButtonWidthConst.constant = addButton.properties.propertyInfo.viewWidth
  58. addButton.setTarget(self, action: #selector(buttonClicked(_:)))
  59. batchButton.properties = ComponentButtonProperty(type: .text_gray_opacity, size: .xs, showLeftIcon: true, buttonText: KMLocalizedString("Batch"), icon: NSImage(named: "tools_batch"), keepPressState: false)
  60. batchButtonWidthConst.constant = batchButton.properties.propertyInfo.viewWidth
  61. batchButton.setTarget(self, action: #selector(buttonClicked(_:)))
  62. applyButton.properties = ComponentButtonProperty(type: .primary, size: .xs, buttonText: KMLocalizedString("Apply"), keepPressState: false)
  63. applyButton.setTarget(self, action: #selector(buttonClicked(_:)))
  64. exitButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .xs, buttonText: KMLocalizedString("Exit"), keepPressState: false)
  65. exitButton.setTarget(self, action: #selector(buttonClicked(_:)))
  66. }
  67. func reloadData() {
  68. if editType == .none {
  69. } else if editType == .watermark {
  70. titleLabel.stringValue = KMLocalizedString("Add Watermark")
  71. addButton.properties.buttonText = KMLocalizedString("Add Watermark")
  72. } else if editType == .background {
  73. titleLabel.stringValue = KMLocalizedString("Add Background")
  74. addButton.properties.buttonText = KMLocalizedString("Add Background")
  75. } else if editType == .header_Footer {
  76. titleLabel.stringValue = KMLocalizedString("Add Header & Footer")
  77. addButton.properties.buttonText = KMLocalizedString("Add Header & Footer")
  78. } else if editType == .bates {
  79. titleLabel.stringValue = KMLocalizedString("Add Bates Numbers")
  80. addButton.properties.buttonText = KMLocalizedString("Add Bates Numbers")
  81. }
  82. if editSubType == .template {
  83. templateButton.properties.state = .pressed
  84. addButton.properties.state = .normal
  85. } else if editSubType == .add || editSubType == .edit {
  86. templateButton.properties.state = .normal
  87. addButton.properties.state = .pressed
  88. }
  89. templateButton.reloadData()
  90. addButton.properties = addButton.properties
  91. addButtonWidthConst.constant = addButton.properties.propertyInfo.viewWidth
  92. applyButton.properties.isDisabled = !applyEnable
  93. applyButton.reloadData()
  94. }
  95. //MARK: - Action
  96. @objc func buttonClicked(_ sender: ComponentButton) {
  97. if sender == templateButton {
  98. editSubType = .template
  99. delegate?.kmEditToolbarViewDidUpdateMode?(self)
  100. } else if sender == addButton {
  101. editSubType = .add
  102. delegate?.kmEditToolbarViewDidUpdateMode?(self)
  103. } else if sender == batchButton {
  104. delegate?.kmEditToolbarViewDidChooseBatch?(self)
  105. } else if sender == applyButton {
  106. delegate?.kmEditToolbarViewDidChooseApply?(self)
  107. } else if sender == exitButton {
  108. delegate?.kmEditToolbarViewDidChooseExit?(self)
  109. }
  110. reloadData()
  111. }
  112. //MARK: - Mouse
  113. override func mouseDown(with event: NSEvent) {
  114. super.mouseDown(with: event)
  115. self.window?.makeFirstResponder(nil)
  116. }
  117. }
  118. extension KMEditToolbarView: ComponentSelectDelegate {
  119. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  120. if menuItemProperty?.identifier == KMLocalizedString("e.g. 1,3-5,10") {
  121. view?.properties.creatable = true
  122. view?.properties.text = ""
  123. } else {
  124. view?.properties.creatable = false
  125. }
  126. view?.reloadData()
  127. }
  128. }