KMBatchWindowController.swift 9.4 KB

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