KMBatchWindowController.swift 9.6 KB

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