KMBatchWindowController.swift 9.3 KB

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