KMBatchWindowController.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // KMBatchWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/1/12.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class CustomWindowController: NSWindowController {
  10. override func windowDidLoad() {
  11. super.windowDidLoad()
  12. // 创建一个工具栏
  13. let toolbar = NSToolbar(identifier: "CustomToolbar")
  14. toolbar.delegate = self
  15. toolbar.allowsUserCustomization = false
  16. toolbar.displayMode = .iconAndLabel
  17. // 设置窗口的工具栏
  18. self.window?.toolbar = toolbar
  19. }
  20. }
  21. extension CustomWindowController: NSToolbarDelegate {
  22. func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
  23. let item = NSToolbarItem(itemIdentifier: itemIdentifier)
  24. item.label = "Action"
  25. item.target = self
  26. item.action = #selector(toolbarAction)
  27. return item
  28. }
  29. func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
  30. return [.flexibleSpace, NSToolbarItem.Identifier("CustomItem")]
  31. }
  32. @objc func toolbarAction() {
  33. print("Toolbar action triggered!")
  34. }
  35. }
  36. class KMBatchWindowController: NSWindowController {
  37. @IBOutlet weak var batchPrecessingView: KMBatchProcessingView!
  38. @IBOutlet weak var batchCollectionView: KMBatchCollectionView!
  39. @IBOutlet weak var batchCollectionViewWidthConstraint: NSLayoutConstraint!
  40. @IBOutlet weak var batchHandleView: NSView!
  41. @IBOutlet weak var settingView: KMBatchSettingView!
  42. @IBOutlet weak var processView: NSView!
  43. static let manager = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
  44. var isDisable: Bool = false {
  45. didSet {
  46. self.batchCollectionView.isDisable = isDisable
  47. self.settingView.isDisable = isDisable
  48. }
  49. }
  50. private var batchData: [KMBatchProcessingTableViewModel]?
  51. @objc var inputData:[URL] = [] {
  52. didSet {
  53. self.reloadData()
  54. }
  55. }
  56. @objc var inputDataModel:[KMBatchProcessingTableViewModel] = [] {
  57. didSet {
  58. self.reloadData()
  59. }
  60. }
  61. var inputSubType: Any? {
  62. didSet {
  63. print( "inputSubType = \(inputSubType)")
  64. self.reloadData()
  65. }
  66. }
  67. var type: KMBatchCollectionViewType = .convertPDF {
  68. didSet {
  69. var size = CGSizeMake(1080, 600)
  70. if type == .imageToPDF {
  71. size = CGSizeMake(880, 600)
  72. }
  73. var rect = self.window!.frame
  74. rect.size.width = size.width
  75. rect.size.height = size.height
  76. self.window?.setFrame(rect, display: true, animate: false)
  77. self.window?.minSize = CGSize(width: size.width, height: size.height)
  78. self.reloadData()
  79. }
  80. }
  81. deinit {
  82. KMPrint("KMImageToPDFWindowController 释放")
  83. NotificationCenter.default.removeObserver(self)
  84. }
  85. override func windowDidLoad() {
  86. super.windowDidLoad()
  87. guard let window = self.window else { return }
  88. // 隐藏标题栏
  89. window.titleVisibility = .hidden
  90. window.titlebarAppearsTransparent = true
  91. // 通过确保窗口的代理是当前的窗口控制器来设置监听
  92. self.window?.delegate = self
  93. self.setup()
  94. self.reloadData()
  95. self.addNotification()
  96. }
  97. func addNotification() {
  98. NotificationCenter.default.addObserver(self, selector: #selector(bacthProcessingNotification), name: NSNotification.Name(kBacthProcessNotification), object: nil)
  99. }
  100. func setup() {
  101. self.window?.contentView?.wantsLayer = true
  102. self.window?.contentView?.layer?.backgroundColor = ComponentLibrary.shared.backgroundColor(forToken: "colorBg/layout-middle")?.cgColor
  103. // 修改窗口背景颜色
  104. self.window?.backgroundColor = ComponentLibrary.shared.backgroundColor(forToken: "colorBg/layout-middle")
  105. self.batchCollectionView.delegate = self
  106. self.batchPrecessingView.delegate = self
  107. self.settingView.type = .convertPDF
  108. }
  109. func reloadData() {
  110. if self.type == .imageToPDF {
  111. batchCollectionViewWidthConstraint.constant = 0
  112. } else {
  113. batchCollectionViewWidthConstraint.constant = 240
  114. }
  115. self.batchPrecessingView.inputType = self.type
  116. self.batchCollectionView.inputType = self.type
  117. self.settingView.type = self.type
  118. self.settingView.subType = self.inputSubType
  119. if inputData.count != 0 {
  120. self.batchPrecessingView.inputData = inputData
  121. }
  122. if inputDataModel.count != 0 {
  123. self.batchPrecessingView.inputDataModel = inputDataModel
  124. }
  125. }
  126. //MARK: 打开文件
  127. static func openFiles(window: NSWindow) {
  128. if KMBatchWindowController.isSampleController() {
  129. KMPrint("存在相同文件")
  130. if let controller: KMBatchWindowController = self.fetchSampleController() {
  131. controller.showWindow(window)
  132. }
  133. } else {
  134. KMBatchProcessingView.openfiles(window: window) { openPanel in
  135. openPanel.title = "选择图片"
  136. openPanel.canChooseDirectories = false
  137. openPanel.canChooseFiles = true
  138. openPanel.allowsMultipleSelection = true
  139. openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
  140. } completion: { (panel ,data) in
  141. if data.count != 0 {
  142. let batchWindowController: KMBatchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
  143. batchWindowController.showWindow(window)
  144. batchWindowController.batchPrecessingView.inputData = data
  145. }
  146. }
  147. }
  148. }
  149. //MARK: 打开文件
  150. static func openFile(_ url: URL?, _ type: KMBatchCollectionViewType, _ subType: Any = "") {
  151. if KMBatchWindowController.isSampleController() {
  152. if let controller: KMBatchWindowController = self.fetchSampleController() {
  153. KMPrint("存在相同文件")
  154. controller.inputSubType = subType
  155. controller.type = type
  156. controller.showWindow(NSApp.mainWindow)
  157. }
  158. } else {
  159. let batchWindowController: KMBatchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
  160. batchWindowController.showWindow(NSApp.mainWindow)
  161. if url != nil {
  162. batchWindowController.batchPrecessingView.inputData = [url!]
  163. }
  164. batchWindowController.inputSubType = subType
  165. batchWindowController.type = type
  166. }
  167. }
  168. static func isSampleController() -> Bool {
  169. for window in NSApp.windows {
  170. let controller = window.windowController
  171. if controller is KMBatchWindowController {
  172. return true
  173. }
  174. }
  175. return false
  176. }
  177. static func fetchSampleController() -> KMBatchWindowController? {
  178. for window in NSApp.windows {
  179. let controller = window.windowController
  180. if controller is KMBatchWindowController {
  181. return controller as? KMBatchWindowController
  182. }
  183. }
  184. return nil
  185. }
  186. }
  187. extension KMBatchWindowController: NSWindowDelegate {
  188. func windowWillClose(_ notification: Notification) {
  189. //清除数据
  190. self.batchPrecessingView._cleanAll()
  191. }
  192. }
  193. extension KMBatchWindowController: KMBatchProcessingViewDelegate {
  194. func reloadData(data: [KMBatchProcessingTableViewModel]) {
  195. self.batchData = data
  196. KMBatchManager.manager.filesData = data
  197. self.settingView.filesData = data
  198. }
  199. }
  200. extension KMBatchWindowController: KMBatchCollectionViewDelegate {
  201. func didSelect(index: IndexPath, data: KMBatchCollectionViewModel) {
  202. KMPrint(data.type.rawValue)
  203. let dataType: KMBatchCollectionViewType = data.type
  204. self.settingView.type = dataType
  205. self.batchPrecessingView.inputType = dataType
  206. KMBatchManager.manager.batchUnkown()
  207. }
  208. }
  209. extension KMBatchWindowController {
  210. @objc func bacthProcessingNotification() {
  211. if KMBatchManager.manager.state == .processing ||
  212. KMBatchManager.manager.state == .begin {
  213. self.isDisable = true
  214. } else {
  215. self.isDisable = false
  216. }
  217. }
  218. }