KMBatchWindowController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // KMBatchWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/1/12.
  6. //
  7. import Cocoa
  8. class KMBatchWindowController: NSWindowController {
  9. @IBOutlet weak var batchPrecessingView: KMBatchProcessingView!
  10. @IBOutlet weak var batchCollectionView: KMBatchCollectionView!
  11. @IBOutlet weak var batchHandleView: NSView!
  12. @IBOutlet weak var settingView: KMBatchSettingView!
  13. @IBOutlet weak var titleLabel: NSTextField!
  14. var batchData: [KMBatchProcessingTableViewModel]?
  15. var inputType: DataNavigationViewButtonActionType? {
  16. didSet {
  17. var type: KMBatchCollectionViewType = .convertPDF
  18. switch inputType {
  19. case .BatchRemove:
  20. type = .batchRemove
  21. case .Background:
  22. type = .background
  23. case .Watermark:
  24. type = .watermark
  25. case .Compression:
  26. type = .compress
  27. case .Security:
  28. type = .security
  29. case.HeaderAndFooter:
  30. type = .headerAndFooter
  31. case .BatesCode:
  32. type = .batesNumber
  33. default:
  34. type = .convertPDF
  35. }
  36. self.type = type
  37. }
  38. }
  39. var inputSubType: Any?
  40. var type: KMBatchCollectionViewType = .convertPDF {
  41. didSet {
  42. self.reloadData()
  43. }
  44. }
  45. deinit {
  46. KMPrint("KMImageToPDFWindowController 释放")
  47. }
  48. override func windowDidLoad() {
  49. super.windowDidLoad()
  50. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  51. self.window?.title = NSLocalizedString("Batch", comment: "")
  52. self.setup()
  53. self.reloadData()
  54. }
  55. func setup() {
  56. self.window?.contentView?.wantsLayer = true
  57. self.window?.contentView?.layer?.backgroundColor = NSColor.white.cgColor
  58. self.titleLabel.font = NSFont.SFProTextRegularFont(16.0)
  59. self.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  60. self.batchCollectionView.delegate = self
  61. self.batchPrecessingView.delegate = self
  62. self.settingView.type = .convertPDF
  63. }
  64. func reloadData() {
  65. self.batchPrecessingView.inputType = self.inputType
  66. self.batchCollectionView.inputType = self.type
  67. self.settingView.type = self.type
  68. self.settingView.subType = self.inputSubType
  69. }
  70. //MARK: 打开文件
  71. static func openFiles(window: NSWindow) {
  72. if KMBatchWindowController.isSampleController() {
  73. KMPrint("存在相同文件")
  74. if let controller: KMBatchWindowController = self.fetchSampleController() {
  75. controller.inputType = .Batch
  76. controller.showWindow(window)
  77. }
  78. } else {
  79. KMBatchProcessingView.openfiles(window: window) { openPanel in
  80. openPanel.title = "选择图片"
  81. openPanel.canChooseDirectories = false
  82. openPanel.canChooseFiles = true
  83. openPanel.allowsMultipleSelection = true
  84. openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
  85. } completion: { (panel ,data) in
  86. if data.count != 0 {
  87. let batchWindowController: KMBatchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
  88. batchWindowController.showWindow(window)
  89. batchWindowController.batchPrecessingView.inputData = data
  90. batchWindowController.inputType = .Batch
  91. }
  92. }
  93. }
  94. }
  95. //MARK: 打开文件
  96. static func openFile(_ url: URL?, _ type: DataNavigationViewButtonActionType, _ subType: Any = "") {
  97. if KMBatchWindowController.isSampleController() {
  98. if let controller: KMBatchWindowController = self.fetchSampleController() {
  99. KMPrint("存在相同文件")
  100. controller.inputSubType = subType
  101. controller.inputType = type
  102. controller.showWindow(NSApp.mainWindow)
  103. }
  104. } else {
  105. let batchWindowController: KMBatchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
  106. batchWindowController.showWindow(NSApp.mainWindow)
  107. if url != nil {
  108. batchWindowController.batchPrecessingView.inputData = [url!]
  109. }
  110. batchWindowController.inputSubType = subType
  111. batchWindowController.inputType = type
  112. }
  113. }
  114. static func isSampleController() -> Bool {
  115. for window in NSApp.windows {
  116. let controller = window.windowController
  117. if controller is KMBatchWindowController {
  118. return true
  119. }
  120. }
  121. return false
  122. }
  123. static func fetchSampleController() -> KMBatchWindowController? {
  124. for window in NSApp.windows {
  125. let controller = window.windowController
  126. if controller is KMBatchWindowController {
  127. return controller as! KMBatchWindowController
  128. }
  129. }
  130. return nil
  131. }
  132. }
  133. extension KMBatchWindowController: KMBatchProcessingViewDelegate {
  134. func reloadData(data: [KMBatchProcessingTableViewModel]) {
  135. self.batchData = data
  136. self.settingView.filesData = self.batchData
  137. }
  138. }
  139. extension KMBatchWindowController: KMBatchCollectionViewDelegate {
  140. func didSelect(index: IndexPath, data: KMBatchCollectionViewModel) {
  141. KMPrint(data.type.rawValue)
  142. let dataType: KMBatchCollectionViewType = data.type
  143. self.settingView.type = dataType
  144. var type: DataNavigationViewButtonActionType = self.inputType ?? .ConvertPDF
  145. switch dataType {
  146. case .batchRemove:
  147. type = .BatchRemove
  148. case .background:
  149. type = .Background
  150. case .watermark:
  151. type = .Watermark
  152. case .compress:
  153. type = .Compression
  154. case .security:
  155. type = .Security
  156. case.headerAndFooter:
  157. type = .HeaderAndFooter
  158. case .batesNumber:
  159. type = .BatesCode
  160. default:
  161. type = .ConvertPDF
  162. }
  163. self.batchPrecessingView.inputType = type
  164. }
  165. }