KMBatchWindowController.swift 8.4 KB

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