KMHomeExtractActionViewController.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // KMHomeExtractActionViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/11/28.
  6. //
  7. import Cocoa
  8. enum KMComboBoxPageChoice : Int {
  9. case all = 0
  10. case current
  11. case odd
  12. case even
  13. case custom
  14. }
  15. protocol KMHomeExtractActionViewControllerDelegate: NSObjectProtocol {
  16. func extractActionViewController(_ viewController: KMHomeExtractActionViewController, pages: [CPDFPage])
  17. func extractActionViewController(_ viewController: KMHomeExtractActionViewController, isOneDocumentPerPage: Bool, isDeletePage: Bool)
  18. }
  19. class KMExtractCombobox: KMComboBox {
  20. typealias mouseDownCallback = (_ mouseDown: Bool) -> Void
  21. var downCallback: mouseDownCallback?
  22. override func mouseDown(with event: NSEvent) {
  23. super.mouseDown(with: event)
  24. if let callback = downCallback {
  25. callback(true)
  26. }
  27. }
  28. }
  29. class KMHomeExtractActionViewController: NSViewController {
  30. @IBOutlet weak var pageScopeLabel: NSTextField!
  31. @IBOutlet weak var pageScopeBox: NSBox!
  32. @IBOutlet weak var extractionWayLabel: NSTextField!
  33. @IBOutlet weak var oneDocumentPerPageBox: KMBox!
  34. @IBOutlet weak var oneDocumentPerPageImageView: NSImageView!
  35. @IBOutlet weak var oneDocumentPerPageLabel: NSTextField!
  36. @IBOutlet weak var isDeletePageBox: KMBox!
  37. @IBOutlet weak var isDeletePageImageView: NSImageView!
  38. @IBOutlet weak var isDeletePageLabel: NSTextField!
  39. var pageRangeCombobox: KMExtractCombobox!
  40. var pdfDocument: CPDFDocument? = nil
  41. var currentPageCount: Int! = 1
  42. var choiceArray: [String] = []
  43. var pageChoice: KMComboBoxPageChoice = .all
  44. var comboBoxContent: String = ""
  45. var isOneDocumentPerPage: Bool = false
  46. var isDeletePage: Bool = false
  47. var customPageNumbers: [String] = []
  48. open weak var delete: KMHomeExtractActionViewControllerDelegate?
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. // Do view setup here.
  52. choiceArray = [NSLocalizedString("All Pages", comment: ""), NSLocalizedString("Current Page", comment: ""), NSLocalizedString("Odd Pages", comment: ""), NSLocalizedString("Even Pages", comment: ""), NSLocalizedString("Customized", comment: "")]
  53. oneDocumentPerPageBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  54. if downEntered {
  55. if self != nil {
  56. self!.oneDocumentPerPageAction(mouseBox)
  57. }
  58. }
  59. }
  60. isDeletePageBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  61. if downEntered {
  62. if self != nil {
  63. self!.isDeletePageAction(mouseBox)
  64. }
  65. }
  66. }
  67. }
  68. override func viewDidAppear() {
  69. super.viewDidAppear()
  70. initializeUI()
  71. initLocalization()
  72. reloadData()
  73. }
  74. // MARK: Init
  75. func initializeUI() -> Void {
  76. pageRangeCombobox = KMExtractCombobox(frame: pageScopeBox.bounds)
  77. pageRangeCombobox.comboxRect = pageRangeCombobox.bounds
  78. pageRangeCombobox.type = .none
  79. pageRangeCombobox.focusRingType = .none
  80. pageRangeCombobox.wantsLayer = true
  81. pageRangeCombobox.layer?.backgroundColor = .white
  82. pageRangeCombobox.layer?.borderWidth = 1.0
  83. pageRangeCombobox.layer?.borderColor = .black
  84. pageRangeCombobox.layer?.cornerRadius = 1.0
  85. pageRangeCombobox.backgroundColor = .white
  86. pageScopeBox.addSubview(pageRangeCombobox)
  87. pageRangeCombobox.addItems(withObjectValues: choiceArray)
  88. let cell = pageRangeCombobox.cell as! NSTextFieldCell
  89. cell.placeholderString = "e.g. 1,3-5,10"
  90. pageRangeCombobox.selectItem(at: 0)
  91. pageRangeCombobox.isEditable = false
  92. pageRangeCombobox.delegate = self
  93. comboBoxContent = "e.g. 1,3-5,10"
  94. pageRangeCombobox.downCallback = { [weak self](mouseDown: Bool) -> Void in
  95. if mouseDown {
  96. if self != nil {
  97. self!.pageRangeComboboxAction(self!.pageRangeCombobox as Any)
  98. }
  99. }
  100. }
  101. }
  102. func initLocalization() -> Void {
  103. pageScopeLabel.stringValue = NSLocalizedString("页面范围", comment: "")
  104. extractionWayLabel.stringValue = NSLocalizedString("提取方式", comment: "")
  105. oneDocumentPerPageBox.toolTip = NSLocalizedString("每个页面作为单独页面", comment: "")
  106. oneDocumentPerPageLabel.stringValue = NSLocalizedString("每个页面作为单独页面", comment: "")
  107. isDeletePageBox.toolTip = NSLocalizedString("提取后删除页面", comment: "")
  108. isDeletePageLabel.stringValue = NSLocalizedString("提取后删除页面", comment: "")
  109. }
  110. // MARK: private
  111. func reloadData() -> Void {
  112. var pages: [CPDFPage] = []
  113. switch pageChoice {
  114. case .all:
  115. for count in 1...pdfDocument!.pageCount {
  116. let page = pdfDocument?.page(at: UInt(count - 1))
  117. pages.append(page!)
  118. }
  119. case .current:
  120. let page = pdfDocument?.page(at: UInt(currentPageCount - 1))
  121. pages.append(page!)
  122. case .odd:
  123. for count in 1...pdfDocument!.pageCount {
  124. if count % 2 != 0 {
  125. let page = pdfDocument?.page(at: UInt(count - 1))
  126. pages.append(page!)
  127. }
  128. }
  129. case .even:
  130. for count in 1...pdfDocument!.pageCount {
  131. if count % 2 == 0 {
  132. let page = pdfDocument?.page(at: UInt(count - 1))
  133. pages.append(page!)
  134. }
  135. }
  136. case .custom:
  137. for string in customPageNumbers {
  138. let page = pdfDocument?.page(at: UInt(string)!)
  139. pages.append(page!)
  140. }
  141. }
  142. self.delete?.extractActionViewController(self, pages: pages)
  143. if isOneDocumentPerPage {
  144. oneDocumentPerPageImageView.image = NSImage(named: "KMImageNameSelect")
  145. } else {
  146. oneDocumentPerPageImageView.image = NSImage(named: "KMImageNameUnSelect")
  147. }
  148. if isDeletePage {
  149. isDeletePageImageView.image = NSImage(named: "KMImageNameSelect")
  150. } else {
  151. isDeletePageImageView.image = NSImage(named: "KMImageNameUnSelect")
  152. }
  153. self.delete?.extractActionViewController(self, isOneDocumentPerPage: isOneDocumentPerPage, isDeletePage: isDeletePage)
  154. }
  155. // MARK: Acton
  156. @IBAction func pageRangeComboboxAction(_ sender: Any) -> Void {
  157. if pageRangeCombobox.indexOfSelectedItem == KMComboBoxPageChoice.custom.rawValue {
  158. if comboBoxContent == "e.g. 1,3-5,10" {
  159. pageRangeCombobox.stringValue = ""
  160. } else {
  161. pageRangeCombobox.stringValue = comboBoxContent
  162. }
  163. }
  164. }
  165. @IBAction func oneDocumentPerPageAction(_ sender: Any) -> Void {
  166. isOneDocumentPerPage = !isOneDocumentPerPage
  167. reloadData()
  168. }
  169. @IBAction func isDeletePageAction(_ sender: Any) -> Void {
  170. isDeletePage = !isDeletePage
  171. reloadData()
  172. }
  173. }
  174. // MARK: NSComboBoxDelegate
  175. extension KMHomeExtractActionViewController: NSComboBoxDelegate {
  176. func comboBoxSelectionDidChange(_ notification: Notification) {
  177. let combobox = notification.object as! KMExtractCombobox
  178. if combobox.isEqual(pageRangeCombobox) {
  179. if pageRangeCombobox.indexOfSelectedItem == KMComboBoxPageChoice.custom.rawValue {
  180. pageRangeCombobox.isEditable = true
  181. pageRangeCombobox.isSelectable = true
  182. self.view.window?.makeFirstResponder(pageRangeCombobox)
  183. } else {
  184. pageRangeCombobox.resignFirstResponder()
  185. pageRangeCombobox.isEditable = false
  186. pageRangeCombobox.isSelectable = false
  187. }
  188. let index = pageRangeCombobox.indexOfSelectedItem
  189. pageChoice = KMComboBoxPageChoice(rawValue: index) ?? .all
  190. reloadData()
  191. }
  192. }
  193. func controlTextDidChange(_ obj: Notification) {
  194. let combobox = obj.object as! KMExtractCombobox
  195. if combobox.isEqual(pageRangeCombobox) {
  196. comboBoxContent = combobox.stringValue
  197. }
  198. }
  199. func controlTextDidEndEditing(_ obj: Notification) {
  200. let combobox = obj.object as! KMExtractCombobox
  201. if combobox.isEqual(pageRangeCombobox) {
  202. reloadData()
  203. }
  204. }
  205. }