123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- //
- // KMEditToolbarView.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/11/5.
- //
- import Cocoa
- import KMComponentLibrary
- @objc protocol KMEditToolbarViewDelegate: AnyObject {
-
- @objc optional func kmEditToolbarViewDidUpdateMode(_ view: KMEditToolbarView)
-
- @objc optional func kmEditToolbarViewDidChooseBatch(_ view: KMEditToolbarView)
-
- @objc optional func kmEditToolbarViewDidChooseApply(_ view: KMEditToolbarView)
-
- @objc optional func kmEditToolbarViewDidChooseExit(_ view: KMEditToolbarView)
-
- }
- class KMEditToolbarView: BaseXibView {
-
- @IBOutlet var contendView: NSView!
-
- @IBOutlet var titleLabel: NSTextField!
- @IBOutlet var pageRangeSelectView: KMPageRangeSelectView!
- @IBOutlet var sepDivider: ComponentDivider!
- @IBOutlet var templateButton: ComponentButton!
- @IBOutlet var addButton: ComponentButton!
- @IBOutlet var batchButton: ComponentButton!
-
- @IBOutlet var applyButton: ComponentButton!
- @IBOutlet var exitButton: ComponentButton!
-
- @IBOutlet var templateButtonWidthConst: NSLayoutConstraint!
- @IBOutlet var addButtonWidthConst: NSLayoutConstraint!
- @IBOutlet var batchButtonWidthConst: NSLayoutConstraint!
-
- weak open var delegate: KMEditToolbarViewDelegate?
-
- var editType: KMPDFEditModeType = .none
- var editSubType: KMPDFEditSubModeType = .none
- var applyEnable: Bool = false
-
- //MARK: - func
- public override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
-
- // Drawing code here.
- }
-
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
-
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- }
-
- public override func awakeFromNib() {
- super.awakeFromNib()
-
- self.setupProperty()
- }
-
- func setupProperty() {
- contendView.wantsLayer = true
- contendView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
-
- titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
- titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
-
-
- sepDivider.properties = ComponentDividerProperty(type: .vertical, dash: false)
-
- templateButton.properties = ComponentButtonProperty(type: .text_gray_opacity, size: .xs, showLeftIcon: true, buttonText: KMLocalizedString("Templates"), icon: NSImage(named: "icon_wm_template"))
- templateButtonWidthConst.constant = templateButton.properties.propertyInfo.viewWidth
- templateButton.setTarget(self, action: #selector(buttonClicked(_:)))
-
- addButton.properties = ComponentButtonProperty(type: .text_gray_opacity, size: .xs, showLeftIcon: true, buttonText: KMLocalizedString("Add Watermark"), icon: NSImage(named: "toolbar_plus"))
- addButtonWidthConst.constant = addButton.properties.propertyInfo.viewWidth
- addButton.setTarget(self, action: #selector(buttonClicked(_:)))
-
- batchButton.properties = ComponentButtonProperty(type: .text_gray_opacity, size: .xs, showLeftIcon: true, buttonText: KMLocalizedString("Batch"), icon: NSImage(named: "tools_batch"), keepPressState: false)
- batchButtonWidthConst.constant = batchButton.properties.propertyInfo.viewWidth
- batchButton.setTarget(self, action: #selector(buttonClicked(_:)))
-
- applyButton.properties = ComponentButtonProperty(type: .primary, size: .xs, buttonText: KMLocalizedString("Apply"), keepPressState: false)
- applyButton.setTarget(self, action: #selector(buttonClicked(_:)))
-
- exitButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .xs, buttonText: KMLocalizedString("Exit"), keepPressState: false)
- exitButton.setTarget(self, action: #selector(buttonClicked(_:)))
-
- }
-
- func reloadData() {
-
- if editType == .none {
-
- } else if editType == .watermark {
- titleLabel.stringValue = KMLocalizedString("Add Watermark")
- addButton.properties.buttonText = KMLocalizedString("Add Watermark")
- } else if editType == .background {
- titleLabel.stringValue = KMLocalizedString("Add Background")
- addButton.properties.buttonText = KMLocalizedString("Add Background")
- } else if editType == .header_Footer {
- titleLabel.stringValue = KMLocalizedString("Add Header & Footer")
- addButton.properties.buttonText = KMLocalizedString("Add Header & Footer")
- } else if editType == .bates {
- titleLabel.stringValue = KMLocalizedString("Add Bates Numbers")
- addButton.properties.buttonText = KMLocalizedString("Add Bates Numbers")
- }
-
- if editSubType == .template {
- templateButton.properties.state = .pressed
- addButton.properties.state = .normal
- } else if editSubType == .add || editSubType == .edit {
- templateButton.properties.state = .normal
- addButton.properties.state = .pressed
- }
- templateButton.reloadData()
-
- addButton.properties = addButton.properties
- addButtonWidthConst.constant = addButton.properties.propertyInfo.viewWidth
- applyButton.properties.isDisabled = !applyEnable
- applyButton.reloadData()
-
- }
-
- //MARK: - Action
- @objc func buttonClicked(_ sender: ComponentButton) {
- if sender == templateButton {
- editSubType = .template
- delegate?.kmEditToolbarViewDidUpdateMode?(self)
- } else if sender == addButton {
- editSubType = .add
- delegate?.kmEditToolbarViewDidUpdateMode?(self)
- } else if sender == batchButton {
- delegate?.kmEditToolbarViewDidChooseBatch?(self)
- } else if sender == applyButton {
- delegate?.kmEditToolbarViewDidChooseApply?(self)
- } else if sender == exitButton {
- delegate?.kmEditToolbarViewDidChooseExit?(self)
- }
- reloadData()
-
- }
-
- //MARK: - Mouse
- override func mouseDown(with event: NSEvent) {
- super.mouseDown(with: event)
-
- self.window?.makeFirstResponder(nil)
- }
- }
- extension KMEditToolbarView: ComponentSelectDelegate {
- func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
- if menuItemProperty?.identifier == KMLocalizedString("e.g. 1,3-5,10") {
- view?.properties.creatable = true
- view?.properties.text = ""
- } else {
- view?.properties.creatable = false
- }
- view?.reloadData()
- }
- }
|