KMHomeInsertActionViewController.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // KMHomeInsertActionViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/11/24.
  6. //
  7. import Cocoa
  8. enum KMInsertScopeType : Int {
  9. case firstPage = 0
  10. case lastPage
  11. case customizePage
  12. }
  13. enum KMPageScopeType : Int {
  14. case previous = 0
  15. case next
  16. }
  17. protocol KMHomeInsertActionViewControllerDelegate: NSObjectProtocol {
  18. func insertActionViewController(_ viewController: KMHomeInsertActionViewController, fromFilePath: URL)
  19. func insertActionViewController(_ viewController: KMHomeInsertActionViewController, insertScopeType: KMInsertScopeType, currentPage: Int, pageScope: KMPageScopeType)
  20. }
  21. class KMHomeInsertActionViewController: NSViewController {
  22. @IBOutlet weak var insertFileLabel: NSTextField!
  23. @IBOutlet weak var selectFileBox: NSBox!
  24. @IBOutlet weak var selectFileTextField: NSTextField!
  25. @IBOutlet weak var selectBox: KMBox!
  26. @IBOutlet weak var selectLabel: NSTextField!
  27. @IBOutlet weak var positionInsertLabel: NSTextField!
  28. @IBOutlet weak var firstPageBox: KMBox!
  29. @IBOutlet weak var firstPageImageView: NSImageView!
  30. @IBOutlet weak var firstPageLabel: NSTextField!
  31. @IBOutlet weak var lastPageBox: KMBox!
  32. @IBOutlet weak var lastPageImageView: NSImageView!
  33. @IBOutlet weak var lastPageLabel: NSTextField!
  34. @IBOutlet weak var customizePageBox: KMBox!
  35. @IBOutlet weak var customizePageImageView: NSImageView!
  36. @IBOutlet weak var customizePageLabel: NSTextField!
  37. @IBOutlet weak var currentPageNumberBox: NSBox!
  38. @IBOutlet weak var currentPageNumberTextField: NSTextField!
  39. @IBOutlet weak var totalPageNumberLabel: NSTextField!
  40. @IBOutlet weak var pageScopeBox: NSBox!
  41. @IBOutlet weak var pageScopePopUpButton: NSPopUpButton!
  42. var insertScopeType: KMInsertScopeType! = .firstPage
  43. var pageScopeType: KMPageScopeType! = .previous
  44. var fromFilePath: URL!
  45. var currentPageCount: Int! = 1
  46. var pdfDocument: CPDFDocument? = nil
  47. open weak var delete: KMHomeInsertActionViewControllerDelegate?
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. // Do view setup here.
  51. initializeUI()
  52. initLocalization()
  53. selectBox.downCallback = { [weak self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in
  54. if downEntered {
  55. if self != nil {
  56. self!.selectButtonAction(self!.selectBox)
  57. }
  58. }
  59. }
  60. firstPageBox.downCallback = { [weak self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in
  61. if downEntered {
  62. if self != nil {
  63. self!.insertScopeType = .firstPage
  64. self!.reloadData()
  65. }
  66. }
  67. }
  68. lastPageBox.downCallback = { [weak self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in
  69. if downEntered {
  70. if self != nil {
  71. self!.insertScopeType = .lastPage
  72. self!.reloadData()
  73. }
  74. }
  75. }
  76. customizePageBox.downCallback = { [weak self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in
  77. if downEntered {
  78. if self != nil {
  79. self!.insertScopeType = .customizePage
  80. self!.reloadData()
  81. }
  82. }
  83. }
  84. }
  85. override func viewDidAppear() {
  86. super.viewDidAppear()
  87. reloadData()
  88. }
  89. // MARK: Init
  90. func initializeUI() -> Void {
  91. pageScopePopUpButton.menu = pageScopeMenu
  92. let menus = pageScopeMenu.items
  93. var opIndex = 0
  94. for item: NSMenuItem in menus {
  95. item.target = self
  96. item.tag = opIndex
  97. item.action = #selector(self.pageScopePopUpButtonAction(_:))
  98. opIndex += 1
  99. }
  100. selectFileTextField.textColor = .black
  101. selectFileTextField.isEnabled = false
  102. }
  103. func initLocalization() -> Void {
  104. insertFileLabel.stringValue = NSLocalizedString("插入文件", comment: "")
  105. selectFileTextField.placeholderString = String(format: " " + NSLocalizedString("选择文件", comment: ""))
  106. selectBox.toolTip = NSLocalizedString("选择", comment: "")
  107. selectLabel.stringValue = NSLocalizedString("选择", comment: "")
  108. positionInsertLabel.stringValue = NSLocalizedString("插入位置", comment: "")
  109. firstPageBox.toolTip = NSLocalizedString("第一页", comment: "")
  110. firstPageLabel.stringValue = NSLocalizedString("第一页", comment: "")
  111. lastPageBox.toolTip = NSLocalizedString("最后一页", comment: "")
  112. lastPageLabel.stringValue = NSLocalizedString("最后一页", comment: "")
  113. customizePageBox.toolTip = NSLocalizedString("页面", comment: "")
  114. customizePageLabel.stringValue = NSLocalizedString("页面", comment: "")
  115. }
  116. // MARK: private
  117. func reloadData() -> Void {
  118. firstPageImageView.image = NSImage(named: "KMImageNameUnSelect")
  119. lastPageImageView.image = NSImage(named: "KMImageNameUnSelect")
  120. customizePageImageView.image = NSImage(named: "KMImageNameUnSelect")
  121. pageScopePopUpButton.isEnabled = false
  122. pageScopeBox.borderColor = .gray
  123. switch insertScopeType {
  124. case .firstPage:
  125. firstPageImageView.image = NSImage(named: "KMImageNameSelect")
  126. case .lastPage:
  127. lastPageImageView.image = NSImage(named: "KMImageNameSelect")
  128. case .customizePage:
  129. customizePageImageView.image = NSImage(named: "KMImageNameSelect")
  130. pageScopePopUpButton.isEnabled = true
  131. pageScopeBox.borderColor = .black
  132. case .none:
  133. firstPageImageView.image = NSImage(named: "KMImageNameSelect")
  134. }
  135. currentPageNumberTextField.stringValue = String(currentPageCount)
  136. let pageCount = pdfDocument?.pageCount
  137. totalPageNumberLabel.stringValue = String(format: "/ %ld", pageCount!)
  138. self.delete?.insertActionViewController(self, insertScopeType: insertScopeType, currentPage: currentPageCount, pageScope: pageScopeType)
  139. }
  140. lazy var pageScopeMenu: NSMenu = {
  141. let pageScopeMenu = NSMenu()
  142. var item = NSMenuItem()
  143. item.title = NSLocalizedString("After", comment: "")
  144. pageScopeMenu.addItem(item)
  145. item = NSMenuItem()
  146. item.title = NSLocalizedString("Before", comment: "")
  147. pageScopeMenu.addItem(item)
  148. return pageScopeMenu
  149. }()
  150. // MARK: Action
  151. @IBAction func pageScopePopUpButtonAction(_ sender: NSMenuItem) -> Void {
  152. let item = sender
  153. let index = item.tag
  154. if index == 0 {
  155. pageScopeType = .next
  156. } else if index == 1 {
  157. pageScopeType = .previous
  158. }
  159. reloadData()
  160. }
  161. @IBAction func selectButtonAction(_ sender: KMBox) -> Void {
  162. let openPanel = NSOpenPanel()
  163. openPanel.allowedFileTypes = ["pdf"]
  164. openPanel.beginSheetModal(for: self.view.window!) { result in
  165. if result == .OK {
  166. for fileURL in openPanel.urls {
  167. let pdfDoc = CPDFDocument(url: fileURL)!
  168. if (pdfDoc.isLocked) {
  169. } else {
  170. self.fromFilePath = fileURL
  171. self.selectFileTextField.stringValue = self.fromFilePath.path
  172. self.delete?.insertActionViewController(self, fromFilePath: self.fromFilePath)
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }