KMBatchWindowController.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 batchHandleView: NSView!
  40. @IBOutlet weak var settingView: KMBatchSettingView!
  41. @IBOutlet weak var processView: NSView!
  42. var isDisable: Bool = false {
  43. didSet {
  44. self.batchCollectionView.isDisable = isDisable
  45. self.settingView.isDisable = isDisable
  46. }
  47. }
  48. private var batchData: [KMBatchProcessingTableViewModel]?
  49. var inputData:[URL] = [] {
  50. didSet {
  51. self.reloadData()
  52. }
  53. }
  54. var inputSubType: Any?
  55. var type: KMBatchCollectionViewType = .convertPDF {
  56. didSet {
  57. self.reloadData()
  58. }
  59. }
  60. deinit {
  61. KMPrint("KMImageToPDFWindowController 释放")
  62. NotificationCenter.default.removeObserver(self)
  63. }
  64. override func windowDidLoad() {
  65. super.windowDidLoad()
  66. guard let window = self.window else { return }
  67. // 隐藏标题栏
  68. window.titleVisibility = .hidden
  69. window.titlebarAppearsTransparent = true
  70. // // 添加自定义导航栏
  71. // let navBar = NSView(frame: NSRect(x: 0, y: window.frame.height - 10, width: window.frame.width, height: 10))
  72. // navBar.wantsLayer = true
  73. // navBar.layer?.backgroundColor = ComponentLibrary.shared.backgroundColor(forToken: "colorBg/layout-middle")?.cgColor // 自定义颜色
  74. //
  75. // // 添加到窗口内容视图中
  76. // window.contentView?.addSubview(navBar)
  77. //
  78. // // 添加约束
  79. // navBar.translatesAutoresizingMaskIntoConstraints = false
  80. // NSLayoutConstraint.activate([
  81. // navBar.topAnchor.constraint(equalTo: window.contentView!.topAnchor),
  82. // navBar.leadingAnchor.constraint(equalTo: window.contentView!.leadingAnchor),
  83. // navBar.trailingAnchor.constraint(equalTo: window.contentView!.trailingAnchor),
  84. // navBar.heightAnchor.constraint(equalToConstant: 10)
  85. // ])
  86. self.setup()
  87. self.reloadData()
  88. self.addNotification()
  89. }
  90. func addNotification() {
  91. NotificationCenter.default.addObserver(self, selector: #selector(bacthProcessingNotification), name: NSNotification.Name(kBacthProcessNotification), object: nil)
  92. }
  93. func setup() {
  94. self.window?.contentView?.wantsLayer = true
  95. self.window?.contentView?.layer?.backgroundColor = ComponentLibrary.shared.backgroundColor(forToken: "colorBg/layout-middle")?.cgColor
  96. // 修改窗口背景颜色
  97. self.window?.backgroundColor = ComponentLibrary.shared.backgroundColor(forToken: "colorBg/layout-middle")
  98. // self.window?.contentView?.border(ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider") , 0.5, 0)
  99. // self.processView.wantsLayer = true
  100. // self.processView.border(ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider"), 0.5)
  101. self.batchCollectionView.delegate = self
  102. self.batchPrecessingView.delegate = self
  103. self.settingView.type = .convertPDF
  104. }
  105. func reloadData() {
  106. self.batchPrecessingView.inputType = self.type
  107. self.batchCollectionView.inputType = self.type
  108. self.settingView.type = self.type
  109. self.settingView.subType = self.inputSubType
  110. self.batchPrecessingView.inputData = inputData
  111. }
  112. //MARK: 打开文件
  113. static func openFiles(window: NSWindow) {
  114. if KMBatchWindowController.isSampleController() {
  115. KMPrint("存在相同文件")
  116. if let controller: KMBatchWindowController = self.fetchSampleController() {
  117. controller.showWindow(window)
  118. }
  119. } else {
  120. KMBatchProcessingView.openfiles(window: window) { openPanel in
  121. openPanel.title = "选择图片"
  122. openPanel.canChooseDirectories = false
  123. openPanel.canChooseFiles = true
  124. openPanel.allowsMultipleSelection = true
  125. openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
  126. } completion: { (panel ,data) in
  127. if data.count != 0 {
  128. let batchWindowController: KMBatchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
  129. batchWindowController.showWindow(window)
  130. batchWindowController.batchPrecessingView.inputData = data
  131. }
  132. }
  133. }
  134. }
  135. //MARK: 打开文件
  136. static func openFile(_ url: URL?, _ type: KMBatchCollectionViewType, _ subType: Any = "") {
  137. if KMBatchWindowController.isSampleController() {
  138. if let controller: KMBatchWindowController = self.fetchSampleController() {
  139. KMPrint("存在相同文件")
  140. controller.inputSubType = subType
  141. controller.type = type
  142. controller.showWindow(NSApp.mainWindow)
  143. }
  144. } else {
  145. let batchWindowController: KMBatchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
  146. batchWindowController.showWindow(NSApp.mainWindow)
  147. if url != nil {
  148. batchWindowController.batchPrecessingView.inputData = [url!]
  149. }
  150. batchWindowController.inputSubType = subType
  151. batchWindowController.type = type
  152. }
  153. }
  154. static func isSampleController() -> Bool {
  155. for window in NSApp.windows {
  156. let controller = window.windowController
  157. if controller is KMBatchWindowController {
  158. return true
  159. }
  160. }
  161. return false
  162. }
  163. static func fetchSampleController() -> KMBatchWindowController? {
  164. for window in NSApp.windows {
  165. let controller = window.windowController
  166. if controller is KMBatchWindowController {
  167. return controller as? KMBatchWindowController
  168. }
  169. }
  170. return nil
  171. }
  172. }
  173. extension KMBatchWindowController: KMBatchProcessingViewDelegate {
  174. func reloadData(data: [KMBatchProcessingTableViewModel]) {
  175. self.batchData = data
  176. KMBatchManager.manager.filesData = data
  177. self.settingView.filesData = data
  178. }
  179. }
  180. extension KMBatchWindowController: KMBatchCollectionViewDelegate {
  181. func didSelect(index: IndexPath, data: KMBatchCollectionViewModel) {
  182. KMPrint(data.type.rawValue)
  183. let dataType: KMBatchCollectionViewType = data.type
  184. self.settingView.type = dataType
  185. self.batchPrecessingView.inputType = dataType
  186. }
  187. }
  188. extension KMBatchWindowController {
  189. @objc func bacthProcessingNotification() {
  190. if KMBatchManager.manager.state == .processing {
  191. self.isDisable = true
  192. } else {
  193. self.isDisable = false
  194. }
  195. }
  196. }