KMPDFInsertPageWindow.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // KMPDFInsertPageWindow.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/11/24.
  6. //
  7. import Cocoa
  8. enum KMPDFToolType : Int {
  9. case Insert = 0
  10. case Extract
  11. }
  12. class KMPDFInsertPageWindow: NSWindowController {
  13. @IBOutlet weak var titleLabel: NSTextField!
  14. @IBOutlet weak var mainLeftBox: NSBox!
  15. @IBOutlet weak var mainRightBox: NSBox!
  16. @IBOutlet weak var okBox: KMBox!
  17. @IBOutlet weak var okLabel: NSTextField!
  18. @IBOutlet weak var cancelBox: KMBox!
  19. @IBOutlet weak var cancelLabel: NSTextField!
  20. @IBOutlet var insertPreviewViewController: KMPDFInsertPreviewViewController!
  21. @IBOutlet var insertActionViewController: KMHomeInsertActionViewController!
  22. @IBOutlet var extractActionViewController: KMHomeExtractActionViewController!
  23. var type: KMPDFToolType!
  24. var pdfDocument: CPDFDocument!
  25. var filePath: URL?
  26. var insertScopeType: KMInsertScopeType! = .firstPage
  27. var pageScopeType: KMPageScopeType! = .previous
  28. var password: String = ""
  29. var fromFilePassword: String = ""
  30. var insertPages: [CPDFPage] = []
  31. var insertPageIndex: Int = 0
  32. var currentPageCount: Int = 1
  33. var isOneDocumentPerPage: Bool = false
  34. var isDeletePage: Bool = false
  35. override func windowDidLoad() {
  36. super.windowDidLoad()
  37. initializeUI()
  38. initLocalization()
  39. cancelBox.downCallback = { [self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  40. if downEntered {
  41. cancelAction(mouseBox)
  42. }
  43. }
  44. okBox.downCallback = { [self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in
  45. if downEntered {
  46. switch type {
  47. case .Insert:
  48. insertAction(mouseBox)
  49. case .Extract:
  50. extractAction(mouseBox)
  51. case .none:
  52. break
  53. }
  54. }
  55. }
  56. insertPreviewViewController.delete = self
  57. insertActionViewController.delete = self
  58. extractActionViewController.delete = self
  59. reloadData()
  60. if pdfDocument.isLocked {
  61. }
  62. }
  63. // MARK: Init
  64. convenience init(documentPath: URL, toolType: KMPDFToolType) {
  65. self.init(windowNibName: NSNib.Name("KMPDFInsertPageWindow"))
  66. type = toolType
  67. pdfDocument = CPDFDocument(url: documentPath)
  68. switch toolType {
  69. case .Insert:
  70. self.window?.setContentSize(NSSize(width: 480, height: 441))
  71. case .Extract:
  72. self.window?.setContentSize(NSSize(width: 480, height: 404))
  73. }
  74. }
  75. func initializeUI() -> Void {
  76. cancelBox.fillColor = .white
  77. cancelLabel.textColor = .black
  78. okBox.fillColor = .black
  79. okLabel.textColor = .white
  80. }
  81. func initLocalization() -> Void {
  82. switch type {
  83. case .Insert:
  84. titleLabel.stringValue = NSLocalizedString("插入", comment: "")
  85. okLabel.stringValue = NSLocalizedString("插入", comment: "")
  86. okBox.toolTip = NSLocalizedString("插入", comment: "")
  87. cancelLabel.stringValue = NSLocalizedString("取消", comment: "")
  88. cancelBox.toolTip = NSLocalizedString("取消", comment: "")
  89. case .some(.Extract):
  90. titleLabel.stringValue = NSLocalizedString("提取", comment: "")
  91. okLabel.stringValue = NSLocalizedString("提取", comment: "")
  92. okBox.toolTip = NSLocalizedString("提取", comment: "")
  93. cancelLabel.stringValue = NSLocalizedString("取消", comment: "")
  94. cancelBox.toolTip = NSLocalizedString("取消", comment: "")
  95. case .none:
  96. break
  97. }
  98. }
  99. // MARK: private
  100. func reloadData() -> Void {
  101. switch type {
  102. case .Insert:
  103. insertPreviewViewController.pdfDocument = pdfDocument
  104. insertActionViewController.pdfDocument = pdfDocument
  105. mainLeftBox.contentView = insertPreviewViewController.view
  106. mainRightBox.contentView = insertActionViewController.view
  107. case .Extract:
  108. insertPreviewViewController.pdfDocument = pdfDocument
  109. extractActionViewController.pdfDocument = pdfDocument
  110. mainLeftBox.contentView = insertPreviewViewController.view
  111. mainRightBox.contentView = extractActionViewController.view
  112. default:
  113. break
  114. }
  115. }
  116. func beginSheetModal(for window: NSWindow, completionHandler handler: ((_ pdfDocument: CPDFDocument, _ password: String, _ pages: [CPDFPage], _ indexPage: Int) -> Void)?) {
  117. window.beginSheet(self.window!) { [self] returnCode in
  118. if pdfDocument != nil {
  119. handler!(pdfDocument!, password, insertPages, insertPageIndex)
  120. }
  121. }
  122. }
  123. func beginSheetExtractModal(for window: NSWindow, completionHandler handler: ((_ pdfDocument: CPDFDocument, _ pages: [CPDFPage], _ oneDocumentPerPage: Bool, _ isDeletePage: Bool) -> Void)?) {
  124. window.beginSheet(self.window!) { [self] returnCode in
  125. if pdfDocument != nil {
  126. handler!(pdfDocument!, insertPages, isOneDocumentPerPage, isDeletePage)
  127. }
  128. }
  129. }
  130. // MARK: Action
  131. @IBAction func cancelAction(_ sender: Any) -> Void {
  132. pdfDocument = nil
  133. NSApp.mainWindow!.endSheet(self.window!)
  134. self.window?.orderOut(self)
  135. }
  136. @IBAction func insertAction(_ sender: Any) -> Void {
  137. if filePath?.path == "" {
  138. let alert = NSAlert()
  139. alert.alertStyle = .critical
  140. alert.messageText = NSLocalizedString("Select a File", comment: "")
  141. alert.runModal()
  142. return
  143. }
  144. if pdfDocument.isLocked {
  145. pdfDocument.unlock(withPassword: password)
  146. }
  147. let fromDocument = CPDFDocument(url: filePath)!
  148. if fromDocument.isLocked {
  149. fromDocument.unlock(withPassword: fromFilePassword)
  150. }
  151. var pages: [CPDFPage] = []
  152. let insertPDF = CPDFDocument(url: filePath)
  153. for count in 1...insertPDF!.pageCount {
  154. let page = insertPDF?.page(at: UInt(count - 1))
  155. pages.append(page!)
  156. }
  157. var index = 0
  158. switch insertScopeType {
  159. case .firstPage:
  160. index = 0
  161. case .lastPage:
  162. index = Int(pdfDocument.pageCount)
  163. case .customizePage:
  164. index = currentPageCount
  165. if 1 == pageScopeType.rawValue {
  166. index = index - 1
  167. }
  168. case .none:
  169. index = 0
  170. }
  171. insertPages = pages
  172. insertPageIndex = index
  173. NSApp.mainWindow!.endSheet(self.window!)
  174. self.window?.orderOut(self)
  175. }
  176. @IBAction func extractAction(_ sender: Any) -> Void {
  177. NSApp.mainWindow!.endSheet(self.window!)
  178. self.window?.orderOut(self)
  179. }
  180. }
  181. // MARK: - KMPDFInsertPreviewViewControllerDelegate
  182. extension KMPDFInsertPageWindow: KMPDFInsertPreviewViewControllerDelegate {
  183. func insertPreviewViewController(_ viewController: KMPDFInsertPreviewViewController, pageChanged: Int) {
  184. switch type {
  185. case .Insert:
  186. insertActionViewController.currentPageCount = pageChanged
  187. insertActionViewController.reloadData()
  188. case .Extract:
  189. extractActionViewController.currentPageCount = pageChanged
  190. extractActionViewController.reloadData()
  191. default:
  192. break
  193. }
  194. }
  195. }
  196. // MARK: - KMHomeInsertActionViewControllerDelegate
  197. extension KMPDFInsertPageWindow: KMHomeInsertActionViewControllerDelegate {
  198. func insertActionViewController(_ viewController: KMHomeInsertActionViewController, fromFilePath: URL) {
  199. self.filePath = fromFilePath
  200. }
  201. func insertActionViewController(_ viewController: KMHomeInsertActionViewController, insertScopeType: KMInsertScopeType, currentPage: Int, pageScope: KMPageScopeType) {
  202. self.insertScopeType = insertScopeType
  203. self.currentPageCount = currentPage
  204. self.pageScopeType = pageScope
  205. }
  206. }
  207. // MARK: KMHomeExtractActionViewControllerDelegate
  208. extension KMPDFInsertPageWindow: KMHomeExtractActionViewControllerDelegate {
  209. func extractActionViewController(_ viewController: KMHomeExtractActionViewController, pages: [CPDFPage]) {
  210. self.insertPages = pages
  211. }
  212. func extractActionViewController(_ viewController: KMHomeExtractActionViewController, isOneDocumentPerPage: Bool, isDeletePage: Bool) {
  213. self.isOneDocumentPerPage = isOneDocumentPerPage
  214. self.isDeletePage = isDeletePage
  215. }
  216. }