// // KMBatchProcessingView.swift // PDF Master // // Created by lizhe on 2022/11/16. // import Cocoa import KMComponentLibrary class KMBatchProcessingView: BaseXibView { @IBOutlet weak var tableView: KMBatchProcessingTableView! @IBOutlet weak var selectedFilesView: KMBatchSelectedFilesView! // @IBOutlet weak var addFilesButton: ComponentDropdownTool! @IBOutlet weak var deleteButton: ComponentButton! @IBOutlet weak var addFilesButton: ComponentButton! @IBOutlet weak var addFilesMoreButton: ComponentButton! @IBOutlet var addFilesSepline: ComponentDivider! @IBOutlet weak var lineView: NSView! var groupView: ComponentGroup? weak var delegate: KMBatchProcessingViewDelegate? var inputType: KMBatchCollectionViewType = .convertPDF { didSet { self.selectedFilesView.inputType = inputType self.tableView.inputType = inputType } } /** @abstract 外部传入数据 @param inputData 文件路劲 */ var inputData: [URL]! { didSet { self.tableView.inputData = inputData self.reloadData() } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } deinit { NotificationCenter.default.removeObserver(self) } override func setup() { self.backgroundColor(ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-low")) self.border(ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider"), 0.5, 0) self.lineView.backgroundColor(ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider")) addFilesSepline.properties = ComponentDividerProperty(type: .vertical) let properties = ComponentButtonProperty() properties.type = .gray properties.size = .s properties.buttonText = KMLocalizedString("Add Files...") addFilesButton.properties = properties properties.propertyInfo.cornerRadius_topRight = 0 properties.propertyInfo.cornerRadius_bottomRight = 0 addFilesButton.reloadData() addFilesButton.setTarget(self, action: #selector(addFileAction)) let properties2 = ComponentButtonProperty() properties2.type = .gray properties2.size = .s properties2.onlyIcon = true properties2.icon = NSImage(named: "KMImageNameImageToPDFDropdown") addFilesMoreButton.properties = properties2 properties2.propertyInfo.cornerRadius_topLeft = 0 properties2.propertyInfo.cornerRadius_bottomLeft = 0 addFilesMoreButton.reloadData() addFilesMoreButton.setTarget(self, action: #selector(addFileAction)) self.deleteButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, state: .normal, onlyIcon: true, icon:NSImage(named: "icon_btn_clear_norm_false"), keepPressState: false) self.deleteButton.setTarget(self, action: #selector(cleanAll(_:))) self.deleteButton.reloadData() self.selectedFilesView.dragEnd = {[unowned self] (data) in self.inputData = data } self.selectedFilesView.addAction = {[unowned self] (sender) in self.addFiles() } self.tableView.delegate = self self.reloadData() NotificationCenter.default.addObserver(self, selector: #selector(bacthFilesProcessingNotification), name: NSNotification.Name(kBacthFilesProcessNotification), object: nil) } func reloadData() { if (self.tableView.data != nil) && self.tableView.data!.count != 0 { self.selectedFilesView.isHidden = true self.tableView.isHidden = false self.lineView.isHidden = false } else { self.selectedFilesView.isHidden = false self.tableView.isHidden = true self.lineView.isHidden = true } self.tableView.reloadData() } func showGroupView() { var viewHeight: CGFloat = 8 var menuItemArr: [ComponentMenuitemProperty] = [] for i in ["Add Files", "Add Folder"] { let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: KMLocalizedString(i)) menuItemArr.append(properties_Menuitem) viewHeight += 36 } if groupView == nil { groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle()) } groupView?.groupDelegate = self groupView?.frame = CGRectMake(310, 0, 200, viewHeight) groupView?.updateGroupInfo(menuItemArr) var point = addFilesButton.convert(addFilesButton.frame.origin, to: nil) point.y -= viewHeight groupView?.showWithPoint(point, relativeTo: addFilesButton) // addFileButton_?.properties.state = .pressed // addFileButton_?.reloadData() } } protocol KMBatchProcessingViewAction{} extension KMBatchProcessingView: KMBatchProcessingViewAction { @IBAction func cleanAll(_ sender: Any) { if self.tableView.selectModels.count != 0 { self.tableView.presenter.deleteData(model: (self.tableView.selectModels.first)!) self.tableView.selectModels.removeObject(self.tableView.selectModels.first!) } else { self.tableView.presenter.cleanAll() self.tableView.selectModels.removeAll() } self.reloadData() } func addFileAction() { self.showGroupView() } func addFiles() { KMBatchProcessingView.openfiles(window: window!) { [unowned self] openPanel in if self.inputType == .OCR { openPanel.allowedFileTypes = KMOCRModel.supportedTypes() } else if self.inputType == .imageToPDF { openPanel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes() } else { openPanel.allowedFileTypes = ["pdf","PDF"] } openPanel.prompt = "Open" openPanel.canChooseDirectories = false //是否允许选择目录 openPanel.canChooseFiles = true //是否可以选择文件 openPanel.allowsMultipleSelection = true //是否允许多选 if openPanel.allowsMultipleSelection == true { openPanel.message = NSLocalizedString("Press the keyboard ⌘/⇧ to select multiple files", comment: "") } } completion: { [unowned self] (panel ,data) in if data.count != 0 { self.tableView.presenter.addData(data: data) self.reloadData() } } } func addFolder() { KMPrint("addFolder") KMBatchProcessingView.openfiles(window: window!) { [unowned self] openPanel in if self.inputType == .OCR { openPanel.allowedFileTypes = KMOCRModel.supportedTypes() } else if self.inputType == .imageToPDF { openPanel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes() } else { openPanel.allowedFileTypes = ["pdf","PDF"] } openPanel.prompt = "Open" openPanel.canChooseDirectories = true //是否允许选择目录 openPanel.canChooseFiles = false //是否可以选择文件 openPanel.allowsMultipleSelection = false //是否允许多选 } completion: { [unowned self] (panel ,data) in if data.count != 0 { var filePaths: [URL] = [] var types: [String] = [] if self.inputType == .OCR { types = KMOCRModel.supportedTypes() } else if self.inputType == .imageToPDF { types = KMBatchProcessingTableViewModel.supportedImageTypes() } else { types = ["pdf","PDF"] } KMMergeTools.findAllFiles(folder: data.first!, result: &filePaths, types: types) self.tableView.presenter.addData(data: filePaths) self.reloadData() } } } func addOpenFiles() { // let array = ImageToPDFTools.fetchOpenDocumentFiles() // self.tableView.presenter.addData(data: array) // self.reloadData() } func canAddOpenFile() -> Bool { var result: Bool = false // let array = ImageToPDFTools.fetchOpenDocumentFiles() // if array.count != 0 { // result = true // } return result } } protocol KMBatchProcessingViewOpenPanel {} extension KMBatchProcessingView: KMBatchProcessingViewOpenPanel { /** @abstract 打开文件 @param window 显示window @param title 标题 */ static func openfiles(window:NSWindow, openPanel:((_ openPanel: NSOpenPanel) -> Void)?, completion:@escaping/**逃逸闭包*/ (_ panel: NSOpenPanel ,_ data:[URL]) -> Void) { let panel = NSOpenPanel() if openPanel != nil { openPanel!(panel) } else { panel.prompt = "" panel.canChooseDirectories = false //是否允许选择目录 panel.canChooseFiles = true //是否可以选择文件 panel.allowsMultipleSelection = true //是否允许多选 panel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes() //支持类型 // let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil) // button.state = .off // panel.accessoryView = button } panel.beginSheetModal(for: window, completionHandler: { result in if result != .OK { KMPrint("取消") completion(panel, []) } else { completion(panel, panel.urls) } }) } } extension KMBatchProcessingView: KMBatchProcessingTableViewDelegate { func reloadData(data: [KMBatchProcessingTableViewModel]) { self.reloadData() if (self.delegate != nil) { self.delegate?.reloadData(data: data) } } } protocol KMBatchProcessingViewDelegate: NSObject { /** 数据刷新 */ func reloadData(data: [KMBatchProcessingTableViewModel]) } extension KMBatchProcessingView: ComponentGroupDelegate { func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) { let familyString = menuItemProperty?.text if familyString == KMLocalizedString("Add Files", comment: "") { self.addFiles() } else if familyString == KMLocalizedString("Add Folder", comment: "") { self.addFolder() } else if familyString == KMLocalizedString("Add Open Files", comment: "") { self.addOpenFiles() } } func componentGroupDidDismiss(group: ComponentGroup?) { self.addFilesButton.properties.state = .normal self.addFilesMoreButton.properties.state = .normal self.addFilesButton.reloadData() self.addFilesMoreButton.reloadData() } } extension KMBatchProcessingView { @objc func bacthFilesProcessingNotification() { self.reloadData() } }