KMBatchWindowController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. print("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.SFProTextRegular(16.0)
  59. self.titleLabel.textColor = NSColor(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. print("存在相同文件")
  74. let controller: KMBatchWindowController = self.fetchSampleController()
  75. controller.inputType = .Batch
  76. controller.showWindow(window)
  77. } else {
  78. KMBatchProcessingView.openfiles(window: window) { openPanel in
  79. openPanel.title = "选择图片"
  80. openPanel.canChooseDirectories = false
  81. openPanel.canChooseFiles = true
  82. openPanel.allowsMultipleSelection = true
  83. openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
  84. } completion: { (panel ,data) in
  85. if data.count != 0 {
  86. let batchWindowController: KMBatchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
  87. batchWindowController.showWindow(window)
  88. batchWindowController.batchPrecessingView.inputData = data
  89. batchWindowController.inputType = .Batch
  90. }
  91. }
  92. }
  93. }
  94. //MARK: 打开文件
  95. static func openFile(_ url: URL?, _ type: DataNavigationViewButtonActionType, _ subType: Any = "") {
  96. if KMBatchWindowController.isSampleController() {
  97. let controller: KMBatchWindowController = self.fetchSampleController()
  98. print("存在相同文件")
  99. controller.inputSubType = subType
  100. controller.inputType = type
  101. controller.showWindow(NSApp.mainWindow)
  102. } else {
  103. let batchWindowController: KMBatchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
  104. batchWindowController.showWindow(NSApp.mainWindow)
  105. if url != nil {
  106. batchWindowController.batchPrecessingView.inputData = [url!]
  107. }
  108. batchWindowController.inputSubType = subType
  109. batchWindowController.inputType = type
  110. }
  111. }
  112. static func isSampleController() -> Bool {
  113. for window in NSApp.windows {
  114. let controller = window.windowController
  115. if controller is KMBatchWindowController {
  116. return true
  117. }
  118. }
  119. return false
  120. }
  121. static func fetchSampleController() -> KMBatchWindowController {
  122. for window in NSApp.windows {
  123. let controller = window.windowController
  124. if controller is KMBatchWindowController {
  125. return controller as! KMBatchWindowController
  126. }
  127. }
  128. return NSWindowController() as! KMBatchWindowController
  129. }
  130. }
  131. extension KMBatchWindowController: KMBatchProcessingViewDelegate {
  132. func reloadData(data: [KMBatchProcessingTableViewModel]) {
  133. self.batchData = data
  134. self.settingView.filesData = self.batchData
  135. }
  136. }
  137. extension KMBatchWindowController: KMBatchCollectionViewDelegate {
  138. func didSelect(index: IndexPath, data: KMBatchCollectionViewModel) {
  139. print(data.type.rawValue)
  140. let dataType: KMBatchCollectionViewType = data.type
  141. self.settingView.type = dataType
  142. var type: DataNavigationViewButtonActionType = self.inputType ?? .ConvertPDF
  143. switch dataType {
  144. case .batchRemove:
  145. type = .BatchRemove
  146. case .background:
  147. type = .Background
  148. case .watermark:
  149. type = .Watermark
  150. case .compress:
  151. type = .Compression
  152. case .security:
  153. type = .Security
  154. case.headerAndFooter:
  155. type = .HeaderAndFooter
  156. case .batesNumber:
  157. type = .BatesCode
  158. default:
  159. type = .ConvertPDF
  160. }
  161. self.batchPrecessingView.inputType = type
  162. }
  163. }