KMEditToolbarView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 pageSelectView: ComponentSelect!
  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. pageSelectView.properties = ComponentSelectProperties(size: .s,
  53. state: .normal,
  54. placeholder: KMLocalizedString("e.g. 1,3-5,10"),
  55. errorText: nil,
  56. creatable: false,
  57. text: KMLocalizedString("All Pages"))
  58. pageSelectView.delegate = self
  59. setUpPageSelctProperty()
  60. sepDivider.properties = ComponentDividerProperty(type: .vertical, dash: false)
  61. templateButton.properties = ComponentButtonProperty(type: .text_gray_opacity, size: .xs, showLeftIcon: true, buttonText: KMLocalizedString("Templates"), icon: NSImage(named: "icon_wm_template"))
  62. templateButtonWidthConst.constant = templateButton.properties.propertyInfo.viewWidth
  63. templateButton.setTarget(self, action: #selector(buttonClicked(_:)))
  64. addButton.properties = ComponentButtonProperty(type: .text_gray_opacity, size: .xs, showLeftIcon: true, buttonText: KMLocalizedString("Add Watermark"), icon: NSImage(named: "toolbar_plus"))
  65. addButtonWidthConst.constant = addButton.properties.propertyInfo.viewWidth
  66. addButton.setTarget(self, action: #selector(buttonClicked(_:)))
  67. batchButton.properties = ComponentButtonProperty(type: .text_gray_opacity, size: .xs, showLeftIcon: true, buttonText: KMLocalizedString("Batch"), icon: NSImage(named: "tools_batch"), keepPressState: false)
  68. batchButtonWidthConst.constant = batchButton.properties.propertyInfo.viewWidth
  69. batchButton.setTarget(self, action: #selector(buttonClicked(_:)))
  70. applyButton.properties = ComponentButtonProperty(type: .primary, size: .xs, buttonText: KMLocalizedString("Apply"), keepPressState: false)
  71. applyButton.setTarget(self, action: #selector(buttonClicked(_:)))
  72. exitButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .xs, buttonText: KMLocalizedString("Exit"), keepPressState: false)
  73. exitButton.setTarget(self, action: #selector(buttonClicked(_:)))
  74. }
  75. func setUpPageSelctProperty() {
  76. let menuItemString:[String] = [KMLocalizedString("All Pages"),
  77. KMLocalizedString("Odd Pages Only"),
  78. KMLocalizedString("Even Pages Only"),
  79. KMLocalizedString("e.g. 1,3-5,10")]
  80. var menuItemArr: [ComponentMenuitemProperty] = []
  81. for language in menuItemString {
  82. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  83. itemSelected: false,
  84. isDisabled: false,
  85. keyEquivalent: nil,
  86. text: language,
  87. identifier: language)
  88. menuItemArr.append(itemProperty)
  89. }
  90. pageSelectView.updateMenuItemsArr(menuItemArr)
  91. pageSelectView.selectItemAtIndex(0)
  92. }
  93. func reloadData() {
  94. if editType == .none {
  95. } else if editType == .watermark {
  96. titleLabel.stringValue = KMLocalizedString("Add Watermark")
  97. addButton.properties.buttonText = KMLocalizedString("Add Watermark")
  98. } else if editType == .background {
  99. titleLabel.stringValue = KMLocalizedString("Add Background")
  100. addButton.properties.buttonText = KMLocalizedString("Add Background")
  101. } else if editType == .header_Footer {
  102. titleLabel.stringValue = KMLocalizedString("Add Header & Footer")
  103. addButton.properties.buttonText = KMLocalizedString("Add Header & Footer")
  104. } else if editType == .bates {
  105. titleLabel.stringValue = KMLocalizedString("Add Bates Numbers")
  106. addButton.properties.buttonText = KMLocalizedString("Add Bates Numbers")
  107. }
  108. if editSubType == .template {
  109. templateButton.properties.state = .pressed
  110. addButton.properties.state = .normal
  111. } else if editSubType == .add || editSubType == .edit {
  112. templateButton.properties.state = .normal
  113. addButton.properties.state = .pressed
  114. }
  115. templateButton.reloadData()
  116. addButton.properties = addButton.properties
  117. addButtonWidthConst.constant = addButton.properties.propertyInfo.viewWidth
  118. applyButton.properties.isDisabled = !applyEnable
  119. applyButton.reloadData()
  120. }
  121. func getSelectedPageString(_ document: CPDFDocument, _ pageIndexs: [Int]) -> String {
  122. var pages = [Int]()
  123. if pageSelectView.indexOfSelect() == 0 {
  124. pages = Array(0..<Int(document.pageCount))
  125. } else if pageSelectView.indexOfSelect() == 1 {
  126. pages = Array(stride(from: 0, to: Int(document.pageCount), by: 2))
  127. } else if pageSelectView.indexOfSelect() == 2 {
  128. pages = Array(stride(from: 1, to: Int(document.pageCount), by: 2))
  129. } else {
  130. pages = pageIndexs.map { $0 - 1 }
  131. }
  132. let pageIndexString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
  133. return pageIndexString
  134. }
  135. func getSelectedPageIndex(_ document: CPDFDocument) -> [Int] {
  136. let pageRangeSelectIndex = pageSelectView.indexOfSelect()
  137. let fileAttribute = KMNFileAttribute()
  138. fileAttribute.pdfDocument = document
  139. fileAttribute.password = document.password ?? ""
  140. if pageRangeSelectIndex == 0 {
  141. fileAttribute.bAllPage = true
  142. fileAttribute.pagesType = .AllPages
  143. } else if pageRangeSelectIndex == 1 {
  144. fileAttribute.bAllPage = false
  145. fileAttribute.pagesType = .OnlyOdd
  146. } else {
  147. if(document.pageCount <= 1) {
  148. fileAttribute.bAllPage = false
  149. fileAttribute.pagesType = .PagesString
  150. fileAttribute.pagesString = pageSelectView.properties.text ?? ""
  151. } else {
  152. fileAttribute.bAllPage = false
  153. if(pageRangeSelectIndex == 2) {
  154. fileAttribute.pagesType = .OnlyEven
  155. } else {
  156. fileAttribute.bAllPage = false
  157. fileAttribute.pagesType = .PagesString
  158. fileAttribute.pagesString = pageSelectView.properties.text ?? ""
  159. }
  160. }
  161. }
  162. let pageIndex = fileAttribute.fetchSelectPages()
  163. return pageIndex
  164. }
  165. //MARK: - Action
  166. @objc func buttonClicked(_ sender: ComponentButton) {
  167. if sender == templateButton {
  168. editSubType = .template
  169. delegate?.kmEditToolbarViewDidUpdateMode?(self)
  170. } else if sender == addButton {
  171. editSubType = .add
  172. delegate?.kmEditToolbarViewDidUpdateMode?(self)
  173. } else if sender == batchButton {
  174. delegate?.kmEditToolbarViewDidChooseBatch?(self)
  175. } else if sender == applyButton {
  176. delegate?.kmEditToolbarViewDidChooseApply?(self)
  177. } else if sender == exitButton {
  178. delegate?.kmEditToolbarViewDidChooseExit?(self)
  179. }
  180. reloadData()
  181. }
  182. }
  183. extension KMEditToolbarView: ComponentSelectDelegate {
  184. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  185. if menuItemProperty?.identifier == KMLocalizedString("e.g. 1,3-5,10") {
  186. view?.properties.creatable = true
  187. view?.properties.text = ""
  188. } else {
  189. view?.properties.creatable = false
  190. }
  191. view?.reloadData()
  192. }
  193. }