// // KMBatchOperateConvertViewController.swift // PDF Reader Pro // // Created by kdanmobile on 2023/11/1. // import Cocoa typealias detailInfoViewMouseDownCallback = (_ mouseDown: Bool) -> () class KMDetailInfoView: NSView{ var mouseDownCallback: detailInfoViewMouseDownCallback? override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) guard let callBack = mouseDownCallback else { return } callBack(true) } } class KMBatchOperateConvertViewController: KMBatchOperateBaseViewController,NSCollectionViewDelegate,NSCollectionViewDataSource{ var dataSourcesArray: [KMConvertWithPDFType]? @IBOutlet var collectionView: NSCollectionView! @IBOutlet var bottomView: NSView! @IBOutlet var convertEveryPageButton: NSButton! @IBOutlet var convertButton: NSButton! @IBOutlet var detailInfoView: KMDetailInfoView! @IBOutlet var detailInfoLabel: NSTextField! @IBOutlet var checkBoxTopConstraint: NSLayoutConstraint! @IBOutlet var containerViewTopConstraint: NSLayoutConstraint! @IBOutlet var checkBoxBottomConstaint: NSLayoutConstraint! @IBOutlet var convertEveryPageButtonLabel: NSTextField! @IBOutlet var extractButton: NSButton! @IBOutlet var wordButtonOne: NSButton! @IBOutlet var wordButtonTwo: NSButton! @IBOutlet var csvExtractButton: NSButton! @IBOutlet var onlyTextBtn: NSButton! @IBOutlet var onlyTableBtn: NSButton! @IBOutlet var allContentBtn: NSButton! @IBOutlet var tableMenu1: NSMenuItem! @IBOutlet var tableMenu2: NSMenuItem! @IBOutlet var tableMenu3: NSMenuItem! @IBOutlet var allContentMenu2: NSMenuItem! @IBOutlet var allContentMenu3: NSMenuItem! @IBOutlet var tableMenu: NSPopUpButton! @IBOutlet var allContentMenu: NSPopUpButton! var excelContentOption: CPDFConvertExcelContentOptions? var excelWorksheetOption: CPDFConvertExcelWorksheetOptions? var haveFiles: Bool = false deinit { NotificationCenter.default.removeObserver(self) } override var interfaceStatus: KMBatchOperateInterfaceStatus?{ set{ super.interfaceStatus = newValue self.convertButton.isEnabled = true if newValue == .Processing { self.collectionView.isSelectable = false self.convertButton.title = NSLocalizedString("Cancel", comment: "") self.convertButton.tag = 2 self.convertButton.setTitleColor(KMAppearance.Layout.w0Color()) self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor if self.convertType == .WordStandard { self.convertButton.title = NSLocalizedString("Convert", comment: "") self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor self.convertButton.isEnabled = false } } else { DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) { var files: [URL] = [] for url in self.successFilePathURLArray! { if FileManager.default.fileExists(atPath: url.path) { files.append(url) } } if files.count > 0 { let workspace = NSWorkspace.shared workspace.activateFileViewerSelecting(files) } } self.collectionView.isSelectable = true self.convertButton.title = NSLocalizedString("Convert", comment: "") self.convertButton.tag = 1 self.convertButton.setTitleColor(KMAppearance.Layout.w0Color()) self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor } } get{ return super.interfaceStatus } } override var convertType: KMConvertWithPDFType?{ set{ super.convertType = newValue changeAllFilesToConvertType(convertType!) fetchFileListViewController()?.reloadConvertInterface(withType: convertType!) } get{ return super.convertType } } func changeAllFilesToConvertType(_ type: KMConvertWithPDFType) { for i in 0..() var index = -1 for i in 0.. 0 { self.detailInfoView.isHidden = false self.containerViewTopConstraint.constant = 16 hasInfo = true } else { self.detailInfoView.isHidden = true self.containerViewTopConstraint.constant = 11 } if self.convertType == .WordAdvance { isExcel = true self.convertEveryPageButton.isHidden = true self.wordButtonOne.isHidden = false self.wordButtonOne.title = NSLocalizedString("Retain Flowing Text", comment: "") self.convertEveryPageButtonLabel.isHidden = true self.extractButton.isHidden = true self.csvExtractButton.isHidden = true self.wordButtonTwo.isHidden = false self.wordButtonTwo.title = NSLocalizedString("Retain Page Layout", comment: "") self.checkBoxBottomConstaint.constant = 15 + self.wordButtonTwo.frame.size.height + self.wordButtonOne.frame.size.height self.checkBoxTopConstraint.constant = 5 } else if self.convertType == .Excel { isExcel = true self.convertEveryPageButtonLabel.stringValue = NSLocalizedString("Convert each page to a separate worksheet", comment: "") self.onlyTextBtn.isHidden = false self.onlyTableBtn.isHidden = false self.allContentBtn.isHidden = false self.tableMenu.isHidden = false self.allContentMenu.isHidden = false self.convertEveryPageButtonLabel.isHidden = true self.convertEveryPageButton.isHidden = true self.wordButtonOne.isHidden = true self.extractButton.isHidden = true self.wordButtonTwo.isHidden = true self.csvExtractButton.isHidden = true self.extractButton.title = NSLocalizedString("Extract Tables Only", comment: "") var oneHeight = self.convertEveryPageButtonLabel.frame.size.height let language = Bundle.main.preferredLocalizations[0] if language != "zh_CN" && language != "zh_TW" { oneHeight = 34.0 } self.checkBoxBottomConstaint.constant = 83 self.checkBoxTopConstraint.constant = 5 } else if self.convertType == .CSV { isExcel = true self.convertEveryPageButtonLabel.isHidden = true self.convertEveryPageButton.isHidden = true self.extractButton.isHidden = true self.wordButtonTwo.isHidden = true self.wordButtonOne.isHidden = true self.csvExtractButton.isHidden = false self.csvExtractButton.title = NSLocalizedString("Extract Tables Only", comment: "") self.checkBoxBottomConstaint.constant = 15 + self.csvExtractButton.frame.size.height self.checkBoxTopConstraint.constant = 5 } else { self.convertEveryPageButton.isHidden = true self.extractButton.isHidden = true self.wordButtonTwo.isHidden = true self.wordButtonOne.isHidden = true self.csvExtractButton.isHidden = true self.convertEveryPageButtonLabel.stringValue = NSLocalizedString("", comment: "") self.checkBoxTopConstraint.constant = -10 self.checkBoxBottomConstaint.constant = 5 } if !hasInfo && !isExcel { self.containerViewTopConstraint.constant = 0 self.checkBoxTopConstraint.constant = 0 self.checkBoxBottomConstaint.constant = 1 } } override func viewDidLoad() { super.viewDidLoad() self.prepareData() self.view.wantsLayer = true collectionView.allowsMultipleSelection = false collectionView.enclosingScrollView?.borderType = .noBorder collectionView.allowsEmptySelection = false convertButton.wantsLayer = true convertButton.font = NSFont.systemFont(ofSize: 13) convertButton.layer?.cornerRadius = 1.0 interfaceStatus = .PrepareProcess convertEveryPageButton.title = NSLocalizedString("", comment: "") // Convert each page to a separate worksheet convertEveryPageButtonLabel.stringValue = NSLocalizedString("Convert each page to a separate worksheet", comment: "") self.collectionView.register(KMConvertCollectionViewHeader.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAdvertisementCollectionViewItem")) self.collectionView.register(KMBatchoperateConvertCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchoperateConvertCollectionViewItem")) self.collectionView.register(KMConvertCollectionViewHeader.self, forSupplementaryViewOfKind: NSCollectionView.elementKindSectionHeader, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "convertHeader")) collectionView.enclosingScrollView?.drawsBackground = false let v = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: 100)) v.wantsLayer = true v.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor collectionView.backgroundView = v collectionView.enclosingScrollView?.horizontalScrollElasticity = .none collectionView.enclosingScrollView?.verticalScrollElasticity = .none allContentBtn.state = .on allContentMenu.isEnabled = true tableMenu.isEnabled = false excelContentOption = .allContent excelWorksheetOption = .forEachPage onlyTextBtn.title = NSLocalizedString("Only Text", comment: "") onlyTableBtn.title = NSLocalizedString("Only Table", comment: "") allContentBtn.title = NSLocalizedString("All Content", comment: "") onlyTextBtn.toolTip = NSLocalizedString("Only Text", comment: "") onlyTableBtn.toolTip = NSLocalizedString("Only Table", comment: "") allContentBtn.toolTip = NSLocalizedString("All Content", comment: "") tableMenu1.title = NSLocalizedString("Create Sheet for each Table", comment: "") tableMenu2.title = NSLocalizedString("Create Sheet for each Page", comment: "") allContentMenu2.title = NSLocalizedString("Create Sheet for each Page", comment: "") tableMenu3.title = NSLocalizedString("Create single Sheet for File", comment: "") allContentMenu3.title = NSLocalizedString("Create single Sheet for File", comment: "") self.detailInfoView.mouseDownCallback = { [weak self] (downEntered: Bool) in if downEntered { self?.moreConvertInfoAction() } } self.updateViewColor() NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: Notification.Name(rawValue: "KMBatchFilesCountNotification"), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(IAPProductPurchasedNotification(notification:)), name: NSNotification.Name("KMIAPProductPurchasedNotification"), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFinishedNotification(notification:)), name: NSNotification.Name("KMIAPProductRestoreFinishedNotification"), object: nil) // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil) DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(deviceActivateStatusChanged(notification:)), name: NSNotification.Name("kDeviceActivateNotification"), object: nil) } @objc func batchFilesCountNotification(notification: NSNotification) { let arr: Array? = notification.object as? [KMBatchOperateFile] self.files? = arr ?? [] if files!.count > 0 { self.convertButton.setTitleColor(NSColor.white) self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor self.haveFiles = true } else { self.convertButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6)) self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor self.haveFiles = false } } @objc func themeChanged(notification: NSNotification) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { self.updateViewColor() } } @objc func IAPProductRestoreFinishedNotification(notification: NSNotification) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { self.updateBottomView() self.prepareData() self.collectionView.reloadData() } } @objc func IAPProductPurchasedNotification(notification: NSNotification) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { self.updateBottomView() self.prepareData() self.collectionView.reloadData() } } @objc func deviceActivateStatusChanged(notification: NSNotification) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { self.updateBottomView() self.prepareData() self.collectionView.reloadData() } } func updateViewColor() { self.view.wantsLayer = true if KMAppearance.isDarkMode() { self.view.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor self.collectionView.backgroundView?.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor } else { self.view.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor self.collectionView.backgroundView?.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor } } func convertActionVC() { var vc: KMToolCompareWindowController? = nil if(.WordStandard == self.convertType || .WordAdvance == self.convertType) { vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1) } else if (.Excel == self.convertType) { vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 2) } else if (.PowerPoint == self.convertType) { vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 3) } else if (self.convertType == .JPG || self.convertType == .PNG || self.convertType == .GIF || self.convertType == .TIFF || self.convertType == .TGA || self.convertType == .BMP){ vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 4) } else { vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 0) } vc?.showWindow(nil) } func prepareData() { let arr: [KMConvertWithPDFType] = [.Excel, .PowerPoint, .RTF, .CSV, .HTML, .Text, .JPEG, .JPG, .PNG, .GIF, .TIFF, .TGA, .BMP, .JPEG2000] self.dataSourcesArray = arr var needShowAdvance = true var needShowDefault = true #if VERSION_FREE // 桌机版 if IAPProductsManager.default().isAvailableAllFunction() { if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() { needShowAdvance = true needShowDefault = false } } #else if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() { needShowAdvance = true needShowDefault = false } #endif if needShowDefault { var wordType: KMConvertWithPDFType = .WordStandard self.dataSourcesArray?.insert(wordType, at: 0) } if needShowAdvance { var wordType: KMConvertWithPDFType = .WordAdvance self.dataSourcesArray?.insert(wordType, at: 0) } } @IBAction func buttonClicked_Convert(_ sender: NSButton) { if !self.haveFiles { return } self.view.window?.makeFirstResponder(nil) for i in 0.. Int { return dataSourcesArray!.count } func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set) { if let indexPath = indexPaths.first { convertType = dataSourcesArray![indexPath.item] updateBottomView() } } func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize { return NSSize(width: 10000, height: 50) } func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView { let view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier:NSUserInterfaceItemIdentifier(rawValue: "convertHeader") , for: indexPath) as? KMConvertCollectionViewHeader view?.refreshData() view?.convertHeaderClickedCallBack = { [weak self] in self?.moreConvertInfoAction() } return view ?? NSView() } func intConvertType(num: Int) -> KMConvertWithPDFType { var type: KMConvertWithPDFType? switch num { case 0: type = .WordAdvance case 1: type = .WordStandard case 2: type = .Excel case 3: type = .PowerPoint case 4: type = .RTF case 5: type = .CSV case 6: type = .HTML case 7: type = .Text case 8: type = .JPEG case 9: type = .JPG case 10: type = .PNG case 11: type = .GIF case 12: type = .TIFF case 13: type = .TGA case 14: type = .BMP case 15: type = .JPEG2000 default: type = .WordAdvance } return type! } func moreConvertInfoAction() { if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() == false { #if VERSION_DMG if IAPProductsManager.default().isAvailableAllFunction() && IAPProductsManager.default().isAvailableAdvancedPDFToOffice() == false { let limitWC = KMPurchaseLimitWindowController.currentLimitWC() limitWC.continueBlock = { windowController in } limitWC.window?.center() limitWC.showWindow(nil) }else{ KMPurchaseCompareWindowController.sharedInstance().showWindow(nil) } #else if IAPProductsManager.default().isAvailableAllFunction() { self.convertActionVC() } else { KMPurchaseCompareWindowController.sharedInstance().showWindow(nil) } #endif } } } extension KMBatchOperateConvertViewController: NSCollectionViewDelegateFlowLayout { func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchoperateConvertCollectionViewItem"), for: indexPath) as! KMBatchoperateConvertCollectionViewItem item.updateInterface(dataSourcesArray![indexPath.item]) return item } func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize { return NSSize(width: 74, height: 80) } func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets { return NSEdgeInsets(top: 0, left: 10, bottom: 0, right: 0) } }