KMBatchProcessingView.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // KMBatchProcessingView.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2022/11/16.
  6. //
  7. import Cocoa
  8. class KMBatchProcessingView: NSView {
  9. @IBOutlet var contentView: NSView!
  10. @IBOutlet weak var addBox: NSBox!
  11. @IBOutlet weak var tableView: KMBatchProcessingTableView!
  12. @IBOutlet weak var selectedFilesView: KMBatchSelectedFilesView!
  13. @IBOutlet weak var cleanButton: NSButton!
  14. var createFilePopover: NSPopover?
  15. var addVC: KMDesignSelect!
  16. weak var delegate: KMBatchProcessingViewDelegate?
  17. var inputType: DataNavigationViewButtonActionType? {
  18. didSet {
  19. self.selectedFilesView.inputType = inputType
  20. self.tableView.inputType = inputType
  21. if inputType == .ImageToPDF {
  22. addVC.removeAllItems()
  23. addVC.addItems(withObjectValues: ["Add Files", "Add Folder"])
  24. }
  25. }
  26. }
  27. /**
  28. @abstract 外部传入数据
  29. @param inputData 文件路劲
  30. */
  31. var inputData: [URL]! {
  32. didSet {
  33. self.tableView.inputData = inputData
  34. self.reloadData()
  35. }
  36. }
  37. override func draw(_ dirtyRect: NSRect) {
  38. super.draw(dirtyRect)
  39. // Drawing code here.
  40. }
  41. // MARK: 初始化
  42. public required init?(coder decoder: NSCoder) {
  43. super.init(coder: decoder)
  44. initContentView()
  45. setup()
  46. }
  47. override init(frame frameRect: NSRect) {
  48. super.init(frame: frameRect)
  49. initContentView()
  50. setup()
  51. }
  52. private func initContentView() {
  53. //绑定xib
  54. let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
  55. bundle: Bundle(for: self.classForCoder.self))!
  56. resource.instantiate(withOwner: self, topLevelObjects: nil)
  57. addSubview(contentView)
  58. contentView.translatesAutoresizingMaskIntoConstraints = false
  59. NSLayoutConstraint.activate([
  60. contentView.topAnchor.constraint(equalTo: topAnchor),
  61. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  62. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  63. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  64. contentView.updateConstraintsForSubtreeIfNeeded()
  65. }
  66. func setup() {
  67. addVC = KMDesignSelect.init(withType: .PopButton)
  68. addBox.fillColor = .clear
  69. addBox.contentView = addVC.view
  70. addVC.removeAllItems()
  71. addVC.addItems(withObjectValues: ["Add Files", "Add Folder", "Add Open Files"])
  72. addVC.selectItem(at: 0)
  73. addVC.popoverBehavior = .transient
  74. addVC.delete = self
  75. self.cleanButton.wantsLayer = true
  76. self.cleanButton.border()
  77. self.selectedFilesView.dragEnd = {[unowned self] (data) in
  78. self.inputData = data
  79. }
  80. self.selectedFilesView.addAction = {[unowned self] (sender) in
  81. self.addFiles()
  82. }
  83. self.tableView.delegate = self
  84. self.reloadData()
  85. }
  86. func reloadData() {
  87. if (self.tableView.data != nil) && self.tableView.data!.count != 0 {
  88. self.selectedFilesView.isHidden = true
  89. self.tableView.isHidden = false
  90. } else {
  91. self.selectedFilesView.isHidden = false
  92. self.tableView.isHidden = true
  93. }
  94. if self.tableView.data?.count != 0 {
  95. self.cleanButton.isEnabled = true
  96. } else {
  97. self.cleanButton.isEnabled = false
  98. }
  99. self.tableView.reloadData()
  100. }
  101. }
  102. protocol KMBatchProcessingViewAction{}
  103. extension KMBatchProcessingView: KMBatchProcessingViewAction {
  104. @IBAction func cleanAll(_ sender: Any) {
  105. if self.tableView.selectModels.count != 0 {
  106. self.tableView.presenter.deleteData(model: (self.tableView.selectModels.first)!)
  107. self.tableView.selectModels.removeObject(self.tableView.selectModels.first!)
  108. } else {
  109. self.tableView.presenter.cleanAll()
  110. self.tableView.selectModels.removeAll()
  111. }
  112. self.reloadData()
  113. }
  114. func addFiles() {
  115. KMBatchProcessingView.openfiles(window: window!) { [unowned self] openPanel in
  116. if self.inputType == .OCR {
  117. openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
  118. } else if self.inputType == .ImageToPDF {
  119. openPanel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes()
  120. } else {
  121. openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
  122. }
  123. openPanel.prompt = "Open"
  124. openPanel.canChooseDirectories = false //是否允许选择目录
  125. openPanel.canChooseFiles = true //是否可以选择文件
  126. openPanel.allowsMultipleSelection = true //是否允许多选
  127. if openPanel.allowsMultipleSelection == true {
  128. openPanel.message = NSLocalizedString("Press the keyboard ⌘/⇧ to select multiple files", comment: "")
  129. }
  130. } completion: { [unowned self] (panel ,data) in
  131. if data.count != 0 {
  132. self.tableView.presenter.addData(data: data)
  133. self.reloadData()
  134. }
  135. }
  136. }
  137. func addFolder() {
  138. KMPrint("addFolder")
  139. KMBatchProcessingView.openfiles(window: window!) { [unowned self] openPanel in
  140. if self.inputType == .OCR {
  141. openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
  142. } else if self.inputType == .ImageToPDF {
  143. openPanel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes()
  144. } else {
  145. openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
  146. }
  147. openPanel.prompt = "Open"
  148. openPanel.canChooseDirectories = true //是否允许选择目录
  149. openPanel.canChooseFiles = false //是否可以选择文件
  150. openPanel.allowsMultipleSelection = false //是否允许多选
  151. } completion: { [unowned self] (panel ,data) in
  152. if data.count != 0 {
  153. var filePaths: [URL] = []
  154. var types: [String] = []
  155. if self.inputType == .OCR {
  156. types = KMOCRModel.supportedTypes()
  157. } else if self.inputType == .ImageToPDF {
  158. types = KMBatchProcessingTableViewModel.supportedImageTypes()
  159. } else {
  160. types = KMOCRModel.supportedTypes()
  161. }
  162. KMMergeTools.findAllFiles(folder: data.first!, result: &filePaths, types: types)
  163. self.tableView.presenter.addData(data: filePaths)
  164. self.reloadData()
  165. }
  166. }
  167. }
  168. func addOpenFiles() {
  169. let array = ImageToPDFTools.fetchOpenDocumentFiles()
  170. self.tableView.presenter.addData(data: array)
  171. self.reloadData()
  172. }
  173. func canAddOpenFile() -> Bool {
  174. var result: Bool = false
  175. let array = ImageToPDFTools.fetchOpenDocumentFiles()
  176. if array.count != 0 {
  177. result = true
  178. }
  179. return result
  180. }
  181. }
  182. protocol KMBatchProcessingViewOpenPanel {}
  183. extension KMBatchProcessingView: KMBatchProcessingViewOpenPanel {
  184. /**
  185. @abstract 打开文件
  186. @param window 显示window
  187. @param title 标题
  188. */
  189. static func openfiles(window:NSWindow, openPanel:((_ openPanel: NSOpenPanel) -> Void)?, completion:@escaping/**逃逸闭包*/ (_ panel: NSOpenPanel ,_ data:[URL]) -> Void) {
  190. let panel = NSOpenPanel()
  191. if openPanel != nil {
  192. openPanel!(panel)
  193. } else {
  194. panel.prompt = ""
  195. panel.canChooseDirectories = false //是否允许选择目录
  196. panel.canChooseFiles = true //是否可以选择文件
  197. panel.allowsMultipleSelection = true //是否允许多选
  198. panel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes() //支持类型
  199. // let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
  200. // button.state = .off
  201. // panel.accessoryView = button
  202. }
  203. panel.beginSheetModal(for: window, completionHandler: { result in
  204. if result != .OK {
  205. KMPrint("取消")
  206. completion(panel, [])
  207. } else {
  208. completion(panel, panel.urls)
  209. }
  210. })
  211. }
  212. }
  213. extension KMBatchProcessingView: KMBatchProcessingTableViewDelegate {
  214. func reloadData(data: [KMBatchProcessingTableViewModel]) {
  215. self.reloadData()
  216. if (self.delegate != nil) {
  217. self.delegate?.reloadData(data: data)
  218. }
  219. }
  220. }
  221. protocol KMBatchProcessingViewDelegate: NSObject {
  222. /**
  223. 数据刷新
  224. */
  225. func reloadData(data: [KMBatchProcessingTableViewModel])
  226. }
  227. extension KMBatchProcessingView: KMSelectPopButtonDelegate {
  228. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  229. if (self.addVC == obj) {
  230. let index = self.addVC.indexOfSelectedItem
  231. if index < 0 {
  232. return
  233. }
  234. let familyString = self.addVC.stringValue
  235. if familyString == NSLocalizedString("Add Files", comment: "") {
  236. self.addFiles()
  237. } else if familyString == NSLocalizedString("Add Folder", comment: "") {
  238. self.addFolder()
  239. } else if familyString == NSLocalizedString("Add Open Files", comment: "") {
  240. self.addOpenFiles()
  241. }
  242. }
  243. }
  244. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  245. }
  246. }