123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- //
- // 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 pageSelectView: ComponentSelect!
- @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")
-
- pageSelectView.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- placeholder: KMLocalizedString("e.g. 1,3-5,10"),
- errorText: nil,
- creatable: false,
- text: KMLocalizedString("All Pages"))
- pageSelectView.delegate = self
- setUpPageSelctProperty()
-
- 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 setUpPageSelctProperty() {
- let menuItemString:[String] = [KMLocalizedString("All Pages"),
- KMLocalizedString("Odd Pages Only"),
- KMLocalizedString("Even Pages Only"),
- KMLocalizedString("e.g. 1,3-5,10")]
- var menuItemArr: [ComponentMenuitemProperty] = []
- for language in menuItemString {
- let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
- itemSelected: false,
- isDisabled: false,
- keyEquivalent: nil,
- text: language,
- identifier: language)
-
- menuItemArr.append(itemProperty)
- }
- pageSelectView.updateMenuItemsArr(menuItemArr)
- pageSelectView.selectItemAtIndex(0)
-
- }
-
- 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()
-
- }
-
- func getSelectedPageString(_ document: CPDFDocument, _ pageIndexs: [Int]) -> String {
-
- var pages = [Int]()
- if pageSelectView.indexOfSelect() == 0 {
- pages = Array(0..<Int(document.pageCount))
- } else if pageSelectView.indexOfSelect() == 1 {
- pages = Array(stride(from: 0, to: Int(document.pageCount), by: 2))
- } else if pageSelectView.indexOfSelect() == 2 {
- pages = Array(stride(from: 1, to: Int(document.pageCount), by: 2))
- } else {
- pages = pageIndexs.map { $0 - 1 }
- }
-
- let pageIndexString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
-
- return pageIndexString
- }
-
- func getSelectedPageIndex(_ document: CPDFDocument) -> [Int] {
- let pageRangeSelectIndex = pageSelectView.indexOfSelect()
-
- let fileAttribute = KMNFileAttribute()
- fileAttribute.pdfDocument = document
- fileAttribute.password = document.password ?? ""
-
- if pageRangeSelectIndex == 0 {
- fileAttribute.bAllPage = true
- fileAttribute.pagesType = .AllPages
- } else if pageRangeSelectIndex == 1 {
- fileAttribute.bAllPage = false
- fileAttribute.pagesType = .OnlyOdd
- } else {
- if(document.pageCount <= 1) {
- fileAttribute.bAllPage = false
- fileAttribute.pagesType = .PagesString
- fileAttribute.pagesString = pageSelectView.properties.text ?? ""
- } else {
- fileAttribute.bAllPage = false
- if(pageRangeSelectIndex == 2) {
- fileAttribute.pagesType = .OnlyEven
- } else {
- fileAttribute.bAllPage = false
- fileAttribute.pagesType = .PagesString
- fileAttribute.pagesString = pageSelectView.properties.text ?? ""
- }
- }
- }
-
- let pageIndex = fileAttribute.fetchSelectPages()
- return pageIndex
- }
-
- //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()
-
- }
-
- }
- 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()
- }
- }
|