// // KMCompareView.swift // PDF Reader Pro // // Created by lizhe on 2023/11/14. // import Cocoa typealias KMCompareViewCancelAction = (_ view: KMCompareView) -> Void typealias KMCompareViewDoneAction = (_ view: KMCompareView, _ config: KMCompareFilesConfig) -> Void enum KMCompareFilesType { case content case coverting } let CPDFOldFileQKSelectedPathsKey = "CPDFOldFileQKSelectedPathsKey" let CPDFNewFileQKSelectedPathsKey = "CPDFNewFileQKSelectedPathsKey" let CPDFMaxQKSelectedPathsCount = 5 class KMCompareView: KMBaseXibView { @IBOutlet weak var cancelButton: NSButton! @IBOutlet weak var doneButton: NSButton! @IBOutlet weak var compareTypeSegment: NSSegmentedControl! @IBOutlet weak var typeSegWidthConst: NSLayoutConstraint! @IBOutlet weak var comparePreviewBox: NSBox! @IBOutlet weak var oldDocumentsTitle: NSTextField! @IBOutlet weak var compareOldPreviewView: KMCompareFilesView! @IBOutlet weak var oldFileQKSelectedBox: NSComboBox! @IBOutlet weak var oldFileSelectBtn: NSButton! @IBOutlet weak var oldPDFView: PDFView! @IBOutlet weak var currentOldPageLabel: NSTextField! @IBOutlet weak var totalPaOldgeLabel: NSTextField! @IBOutlet weak var compareNewPreviewView: KMCompareFilesView! @IBOutlet weak var documentsNewTitle: NSTextField! @IBOutlet weak var fileQKNewSelectedBox: NSComboBox! @IBOutlet weak var fileSelectNewBtn: NSButton! @IBOutlet weak var pdfNewView: PDFView! @IBOutlet weak var addFileContentView: KMCompareFilesView! @IBOutlet weak var addFileImageView: NSImageView! @IBOutlet weak var addFileAddImageFramView: KMCompareFilesView! @IBOutlet weak var currentNewPageLabel: NSTextField! @IBOutlet weak var totalPaNewgeLabel: NSTextField! @IBOutlet weak var addFileTitle: NSTextField! @IBOutlet weak var addFileMag: NSTextField! @IBOutlet weak var pageRangeField: NSTextField! @IBOutlet weak var oldFileRangeField: NSTextField! @IBOutlet weak var pageRangeOldComboBox: NSComboBox! @IBOutlet weak var fileNewRangeField: NSTextField! @IBOutlet weak var pageRangeNewComboBox: NSComboBox! @IBOutlet weak var compareTextTypeBtn: NSButton! @IBOutlet weak var compareImageTypeBtn: NSButton! @IBOutlet weak var compareSettingsBtn: NSButton! @IBOutlet weak var settingBtnTopLayout: NSLayoutConstraint! var contentSettingWindowController: KMCompareContentSettingWindowController? var coveringSettingWindowController: KMCompareCoveringSettingWindowController? var pdfCompareContent: CPDFCompareContent? var password: String = "" { didSet { self.oldPDFView.document?.unlock(withPassword: password) } } var filePath: String? { didSet { if filePath?.count != 0 { self.addFilePath(filePath: filePath!, password: password) } } } private var pdfOldDocument: PDFDocument? var oldFileQKSelectedPaths: [String] = [] var oldFilePaths: [String] = [] private var pdfNewDocument: PDFDocument? var fileQKNewSelectedPaths: [String] = [] var newFilePaths: [String] = [] var fileType: KMCompareFilesType = .content { didSet { self.updateFileCompareType(fileType: fileType) } } var cancelAction: KMCompareViewCancelAction? var doneAction: KMCompareViewDoneAction? convenience init(pdfDocument: PDFDocument) { self.init() self.pdfOldDocument = pdfDocument let document: PDFDocument = PDFDocument.init(url: pdfDocument.documentURL!)! let cDocument: CPDFDocument = CPDFDocument.init(url: pdfDocument.documentURL!) let file: KMFileAttribute = KMFileAttribute() file.pdfDocument = cDocument let config: KMCompareFilesConfig = KMCompareFilesConfig.init() config.fileOldAttribute = file } convenience init(filePath: String, password: String) { self.init() self.pdfOldDocument = PDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL) let pdfDocument = PDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL) let cDocument = CPDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL) if pdfDocument!.isLocked { pdfDocument!.unlock(withPassword: password) } if self.pdfOldDocument!.isLocked { self.pdfOldDocument!.unlock(withPassword: password) } let file: KMFileAttribute = KMFileAttribute() file.pdfDocument = cDocument let config = KMCompareFilesConfig.defaultConfig config.fileOldAttribute = file config.fileOldAttribute.password = password } override func addNotification() { NotificationCenter.default.addObserver(self, selector: #selector(pageChangeNotification), name: NSNotification.Name.PDFViewPageChanged, object: nil) } override func setup() { fileQKNewSelectedBox.delegate = self oldFileQKSelectedBox.delegate = self compareTypeSegment.action = #selector(segmentedControlClicked) compareTypeSegment.target = self compareTypeSegment.wantsLayer = true addFileContentView.wantsLayer = true addFileImageView.ignoresMultiClick = true fileQKNewSelectedBox.isEnabled = false oldFileQKSelectedBox.isEnabled = false fileQKNewSelectedBox.cell!.isEnabled = false oldFileQKSelectedBox.cell!.isEnabled = false currentOldPageLabel.delegate = self currentNewPageLabel.delegate = self oldPDFView.wantsLayer = true pdfNewView.wantsLayer = true // vsMaskView.wantsLayer = true; oldPDFView.layer?.backgroundColor = NSColor.clear.cgColor pdfNewView.layer?.backgroundColor = NSColor.clear.cgColor // vsMaskView.layer.backgroundColor = NSColor.clear.CGColor compareTypeSegment.wantsLayer = true compareTypeSegment.layer?.cornerRadius = 5.0 compareTypeSegment.layer?.masksToBounds = true compareTypeSegment.layer?.backgroundColor = NSColor.clear.cgColor addFileAddImageFramView.wantsLayer = true addFileAddImageFramView.layer?.backgroundColor = NSColor.clear.cgColor self.compareOldPreviewView.dragSuccessBlock = { [weak self] filePath in self?.updateDocument(filePath: filePath) { fileAttitude in } } self.compareNewPreviewView.dragSuccessBlock = { [weak self] filePath in self?.updateDocument(filePath: filePath, isNew: true) { fileAttitude in } } self.addFileContentView.dragSuccessBlock = { [weak self] filePath in self?.updateDocument(filePath: filePath, isNew: true) { fileAttitude in } } self.addFileAddImageFramView.dragSuccessBlock = { [weak self] filePath in self?.updateDocument(filePath: filePath, isNew: true) { fileAttitude in } } self.addFileContentView.mouseUpBack = { [weak self] view in self?.chooseFileAction(isNew: true) } KMCompareFilesConfig.defaultConfig.fileNewAttribute.pdfDocument = nil } override func updateLanguage() { compareTypeSegment.setLabel(NSLocalizedString("Side-by-Side View", comment: ""), forSegment: 0) compareTypeSegment.setLabel(NSLocalizedString("Compare by File Overlay", comment: ""), forSegment: 1) compareTypeSegment.setToolTip(NSLocalizedString("Side-by-Side View", comment: ""), forSegment: 0) compareTypeSegment.setToolTip(NSLocalizedString("Compare by File Overlay", comment: ""), forSegment: 1) oldDocumentsTitle.stringValue = NSLocalizedString("Old File", comment: "") documentsNewTitle.stringValue = NSLocalizedString("New File", comment: "") oldFileSelectBtn.title = NSLocalizedString("Choose...", comment: "") fileSelectNewBtn.title = NSLocalizedString("Choose...", comment: "") oldFileSelectBtn.toolTip = NSLocalizedString("Choose...", comment: "") fileSelectNewBtn.toolTip = NSLocalizedString("Choose...", comment: "") addFileTitle.stringValue = NSLocalizedString("Select File", comment: "") addFileMag.stringValue = NSLocalizedString("Click to add", comment: "") oldFileRangeField.stringValue = NSLocalizedString("Old File:", comment: "") fileNewRangeField.stringValue = NSLocalizedString("New File:", comment: "") pageRangeField.stringValue = NSLocalizedString("Page Range", comment: "") pageRangeOldComboBox.removeAllItems() pageRangeOldComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""), NSLocalizedString("Odd Pages Only", comment: ""), NSLocalizedString("Even Pages Only",comment: ""), NSLocalizedString("e.g. 1,3-5,10",comment: "")]) pageRangeOldComboBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "") pageRangeNewComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""), NSLocalizedString("Odd Pages Only", comment: ""), NSLocalizedString("Even Pages Only",comment: ""), NSLocalizedString("e.g. 1,3-5,10",comment: "")]) self.pageRangeNewComboBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "") self.compareTextTypeBtn.title = NSLocalizedString("Compare text", comment: "") self.compareImageTypeBtn.title = NSLocalizedString("Compare image", comment: "") let settingsString = " " + NSLocalizedString("Settings", comment: "") compareSettingsBtn.title = settingsString compareSettingsBtn.state = .off compareSettingsBtn.setTitleColor(KMAppearance.Layout.h1Color()) cancelButton.title = NSLocalizedString("Cancel", comment: "") doneButton.title = NSLocalizedString("Compare", comment: "") } override func updateUI() { if KMAppearance.isDarkMode() { self.compareTypeSegment.layer?.backgroundColor = NSColor(red: 50.0/255.0, green: 53.0/255.0, blue: 54.0/255.0, alpha: 1).cgColor self.addFileContentView.layer?.backgroundColor = NSColor.black.cgColor } else { self.compareTypeSegment.layer?.backgroundColor = NSColor(red: 247.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1).cgColor self.addFileContentView.layer?.backgroundColor = NSColor.white.cgColor } } override func reloadData() { if(pageRangeOldComboBox.indexOfSelectedItem == 3) { self.window?.makeFirstResponder(pageRangeOldComboBox) pageRangeOldComboBox.stringValue = "" pageRangeOldComboBox.isSelectable = true } self.updatePageRangeData(view: pageRangeNewComboBox, file: KMCompareFilesConfig.defaultConfig.fileNewAttribute, isNew: true) self.updatePageRangeData(view: pageRangeOldComboBox, file: KMCompareFilesConfig.defaultConfig.fileOldAttribute) if (KMCompareFilesConfig.defaultConfig.isCompareText)() { self.compareTextTypeBtn.state = .on; } else { self.compareTextTypeBtn.state = .off; } if (KMCompareFilesConfig.defaultConfig.isCompareImage)() { self.compareImageTypeBtn.state = .on; } else { self.compareImageTypeBtn.state = .off; } self.updateSelectBoxData() self.updatePageState() self.updatePageState(isNew: true) } @objc func pageChangeNotification(notification: Notification) { self.updatePageState() self.updatePageState(isNew: true) } } extension KMCompareView { func addFilePath(filePath: String, isNew: Bool = false, password: String = "") { DispatchQueue.main.async { let document: PDFDocument = PDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL) ?? PDFDocument() let cDocument: CPDFDocument = CPDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)! let file: KMFileAttribute = KMFileAttribute() // file.pdfDocument = cDocument // file.myPDFDocument = document file.password = password file.filePath = filePath document.unlock(withPassword: password) cDocument.unlock(withPassword: password) if isNew { KMCompareFilesConfig.defaultConfig.fileNewAttribute = file self.pdfNewDocument = document self.pdfNewView.document = document self.pdfNewView.autoScales = true self.pdfNewView.delegate = self } else { KMCompareFilesConfig.defaultConfig.fileOldAttribute = file self.pdfOldDocument = document self.oldPDFView.document = document self.oldPDFView.autoScales = true self.oldPDFView.delegate = self } self.addFileQKSelectPath(filePath: filePath, isNew: isNew) self.reloadData() } } func addFileQKSelectPath(filePath: String, isNew: Bool = false) { let pdfdocument = PDFDocument(url: NSURL(fileURLWithPath: filePath) as URL) guard let pdfdocument = pdfdocument else { return } var key = "" if isNew { key = CPDFNewFileQKSelectedPathsKey } else { key = CPDFOldFileQKSelectedPathsKey } //取出 var filePaths: [String] = UserDefaults.standard.object(forKey: key) as? [String] ?? [] if filePaths.count > CPDFMaxQKSelectedPathsCount { filePaths.removeLast() } if !filePaths.contains(filePath) { filePaths.insert(filePath, at: 0) } //存储 UserDefaults.standard.setValue(filePaths, forKey: key) UserDefaults.standard.synchronize() let fileName = filePath.lastPathComponent self.updateSelectBoxData() if isNew { self.fileQKNewSelectedBox.selectItem(withObjectValue: fileName) self.fileQKNewSelectedBox.toolTip = fileName } else { self.oldFileQKSelectedBox.selectItem(withObjectValue: fileName) self.oldFileQKSelectedBox.toolTip = fileName } } func updateSelectBoxData() { self.oldFileQKSelectedPaths = self.updateSelectBoxItemData() self.oldFileQKSelectedBox.removeAllItems() self.oldFileQKSelectedBox.addItems(withObjectValues: self.oldFileQKSelectedPaths) self.fileQKNewSelectedPaths = self.updateSelectBoxItemData(isNew: true) self.fileQKNewSelectedBox.removeAllItems() self.fileQKNewSelectedBox.addItems(withObjectValues: self.fileQKNewSelectedPaths) if self.fileQKNewSelectedPaths.count > 0 { self.fileQKNewSelectedBox.isEnabled = true } else { self.fileQKNewSelectedBox.isEnabled = false } if self.oldFileQKSelectedPaths.count > 0 { self.oldFileQKSelectedBox.isEnabled = true } else { self.oldFileQKSelectedBox.isEnabled = false } self.oldFileQKSelectedBox.isEditable = false self.fileQKNewSelectedBox.isEditable = false } func updateSelectBoxItemData(isNew: Bool = false) -> [String] { let defaults = UserDefaults.standard var fileSelectedCachePaths: [String] = [] if isNew { fileSelectedCachePaths = defaults.value(forKey: CPDFNewFileQKSelectedPathsKey) as? [String] ?? [] self.newFilePaths = fileSelectedCachePaths } else { fileSelectedCachePaths = defaults.value(forKey: CPDFOldFileQKSelectedPathsKey) as? [String] ?? [] self.oldFilePaths = fileSelectedCachePaths } var fileSelectedPaths: [String] = [] var filePaths: [String] = [] let fileManager = FileManager.default for path in fileSelectedCachePaths { if fileManager.fileExists(atPath: path) { // #if VERSION_DMG fileSelectedPaths.append(path.lastPathComponent) filePaths.append(path) // #else // let url = URL(fileURLWithPath: path) // // if let bookmarkData = try? url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil) { // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: url) // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: URL(fileURLWithPath: url.path ?? url.absoluteString)) // // fileOldSelectedPaths.append(path) // } else { // AppSandboxFileAccess.fileAccess().accessFileURL(url, persistPermission: yearMask) { // if let bookmarkData = try? url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil) { // fileOldSelectedPaths.append(path) // } // } // } // #endif } } if isNew { self.newFilePaths = filePaths } else { self.oldFilePaths = filePaths } return fileSelectedPaths } func updatePageState(isNew: Bool = false) { var pageDocument = PDFDocument() var pdfView = PDFView() var currentPageLabel = NSTextField() var totalPageLabel = NSTextField() if isNew { addFileContentView.isHidden = false guard let document = KMCompareFilesConfig.defaultConfig.fileNewAttribute.pdfDocument else { return } pdfView = pdfNewView pageDocument = pdfView.document ?? PDFDocument() currentPageLabel = currentNewPageLabel totalPageLabel = totalPaNewgeLabel addFileContentView.isHidden = true } else { guard let document = KMCompareFilesConfig.defaultConfig.fileOldAttribute.pdfDocument else { return } pdfView = oldPDFView pageDocument = pdfView.document ?? PDFDocument() currentPageLabel = currentOldPageLabel totalPageLabel = totalPaOldgeLabel } // 隐藏PDFView滑动条 pdfView.documentView?.enclosingScrollView?.hasVerticalScroller = false pdfView.documentView?.enclosingScrollView?.hasHorizontalScroller = false let pageCount = pageDocument.pageCount var currentPageIndex = 0 if(pdfView.currentPage != nil) { currentPageIndex = Int(pageDocument.index(for: pdfView.currentPage!)) } if(pageCount > 0) { currentPageLabel.stringValue = "\(currentPageIndex + 1)" } else { currentPageLabel.stringValue = "\(currentPageIndex)" } totalPageLabel.stringValue = "/ \(pageCount)" } func updateDocument(filePath: String, isNew: Bool = false, completion: @escaping (_ fileAttitude: KMFileAttribute?) -> Void) { let isAccessFilePath = AppSandboxFileAccess().accessFileURL(NSURL(fileURLWithPath: filePath) as URL, persistPermission: true) { } if isAccessFilePath { print("有权限") var pdfDocument = CPDFDocument() if isNew { pdfDocument = KMCompareFilesConfig.defaultConfig.fileNewAttribute.pdfDocument } else { pdfDocument = KMCompareFilesConfig.defaultConfig.fileOldAttribute.pdfDocument } // guard let pdfDocument = pdfDocument else { // completion(nil) // return // } if (pdfDocument?.documentURL.path == filePath) { let alert = NSAlert() alert.alertStyle = NSAlert.Style.critical alert.messageText = NSLocalizedString("There is no difference between the two documents.", comment: "") alert.runModal() completion(nil) return } else { KMBaseWindowController.checkPassword(url: NSURL(fileURLWithPath: filePath) as URL, type: .open) { success, resultPassword in if success { let file = KMFileAttribute() file.pdfDocument = pdfDocument file.password = resultPassword completion(file) } else { completion(nil) } } } } else { print("无权限") } } func updatePageRangeData(view: NSComboBox, file: KMFileAttribute, isNew: Bool = false) { switch (file.pagesType) { case .all: view.selectItem(at: 0) view.isEditable = false view.delegate = nil break; case .odd: view.selectItem(at: 1) view.isEditable = false view.delegate = nil break; case .even: view.selectItem(at: 2) view.isEditable = false view.delegate = nil break; case .custom: view.delegate = self view.stringValue = file.pagesString view.isEditable = true break; default: break } } func updateFileCompareType(fileType: KMCompareFilesType) { if fileType == .content { compareTypeSegment.selectedSegment = 0 compareTextTypeBtn.isHidden = false compareImageTypeBtn.isHidden = false settingBtnTopLayout.constant = 81 } else { compareTypeSegment.selectedSegment = 1 compareTextTypeBtn.isHidden = true compareImageTypeBtn.isHidden = true settingBtnTopLayout.constant = 20 } } func updatePageRange(view: NSComboBox, isNew: Bool = false) { let config = KMCompareFilesConfig.defaultConfig var index = pageRangeOldComboBox.indexOfSelectedItem var file = config.fileOldAttribute if isNew { index = pageRangeNewComboBox.indexOfSelectedItem file = config.fileNewAttribute } switch index { case 0: file.bAllPage = true file.pagesType = .all view.delegate = nil view.isEditable = false case 1: file.bAllPage = false file.pagesType = .odd view.delegate = nil view.isEditable = false case 2: file.bAllPage = false file.pagesType = .even view.delegate = nil view.isEditable = false case 3: file.bAllPage = false file.pagesType = .custom view.delegate = self view.stringValue = "" view.isEditable = true view.isSelectable = true self.window?.makeFirstResponder(view) default: break } } } extension KMCompareView { @IBAction func cancelButtonAction(_ sender: Any) { guard let callBack = cancelAction else { return } callBack(self) } @IBAction func convertButtonAction(_ sender: Any) { guard let callBack = doneAction else { return } if KMCompareFilesConfig.defaultConfig.fileOldAttribute.pagesType == .custom { KMCompareFilesConfig.defaultConfig.fileOldAttribute.pagesString = self.pageRangeOldComboBox.stringValue } if KMCompareFilesConfig.defaultConfig.fileNewAttribute.pagesType == .custom { KMCompareFilesConfig.defaultConfig.fileNewAttribute.pagesString = self.pageRangeNewComboBox.stringValue } callBack(self, KMCompareFilesConfig.defaultConfig) } @IBAction func oldFilesSelectBoxAction(_ sender: Any) { let selectIndex = self.oldFileQKSelectedBox.indexOfSelectedItem if selectIndex >= 0 { let selectItem = self.oldFileQKSelectedBox.itemObjectValue(at: selectIndex) let filePath = self.oldFilePaths[selectIndex] self.updateDocument(filePath: filePath) { fileAttitude in if fileAttitude != nil { self.addFilePath(filePath: filePath , isNew: false, password: fileAttitude?.password ?? "") } else { self.oldFileQKSelectedBox.selectItem(withObjectValue: self.oldPDFView.document?.documentURL?.path.lastPathComponent) } } } } @IBAction func newFilesSelectBoxAction(_ sender: Any) { let selectIndex = self.fileQKNewSelectedBox.indexOfSelectedItem if selectIndex >= 0 { let selectItem = self.fileQKNewSelectedBox.itemObjectValue(at: selectIndex) let filePath = self.newFilePaths[selectIndex] self.updateDocument(filePath: filePath) { fileAttitude in if fileAttitude != nil { self.addFilePath(filePath: filePath , isNew: true, password: fileAttitude?.password ?? "") } else { self.fileQKNewSelectedBox.selectItem(withObjectValue: self.pdfNewView.document?.documentURL?.path.lastPathComponent) } } } } @IBAction func oldChooseButtonAction(_ sender: Any) { self.chooseFileAction() } @IBAction func newChooseButtonAction(_ sender: Any) { self.chooseFileAction(isNew: true) } @IBAction func oldNextButtonAction(_ sender: Any) { self.oldPDFView.goToNextPage(nil) self.updatePageState() } @IBAction func oldPreviousButtonAction(_ sender: Any) { self.oldPDFView.goToPreviousPage(nil) self.updatePageState() } @IBAction func newNextButtonAction(_ sender: Any) { self.pdfNewView.goToNextPage(nil) self.updatePageState(isNew: true) } @IBAction func newPreviousButtonAction(_ sender: Any) { self.pdfNewView.goToPreviousPage(nil) self.updatePageState(isNew: true) } @IBAction func oldPageRangeBoxAction(_ sender: Any) { self.updatePageRange(view: self.pageRangeOldComboBox) } @IBAction func newPageRangeBoxAction(_ sender: Any) { self.updatePageRange(view: self.pageRangeNewComboBox, isNew: true) } @IBAction func compareTextButtonAction(_ sender: NSButton) { let isCompareText = sender.state == .on ?true:false KMCompareFilesConfig.defaultConfig.setIsCompareText(isCompareText) } @IBAction func compareImageButtonAction(_ sender: NSButton) { let isCompareImage = sender.state == .on ?true:false KMCompareFilesConfig.defaultConfig.setIsCompareImage(isCompareImage) } @IBAction func settingButtonAction(_ sender: Any) { if fileType == .content { if contentSettingWindowController == nil { let controller = KMCompareContentSettingWindowController(windowNibName: "KMCompareContentSettingWindowController") contentSettingWindowController = controller } NSWindow.currentWindow().addChildWindow(contentSettingWindowController!.window!, ordered: .above) contentSettingWindowController!.window?.center() } else { if coveringSettingWindowController == nil { let controller = KMCompareCoveringSettingWindowController(windowNibName: "KMCompareCoveringSettingWindowController") coveringSettingWindowController = controller } NSWindow.currentWindow().addChildWindow(coveringSettingWindowController!.window!, ordered: .above) coveringSettingWindowController!.window?.center() } } @objc func segmentedControlClicked(sender: NSSegmentedControl) { let selectedType = self.compareTypeSegment.selectedSegment var type: KMCompareFilesType = .content if (selectedType == 0) { type = .content; } else { type = .coverting; } self.fileType = type } func chooseFileAction(isNew: Bool = false) { // fileQKNewSelectedBox.resignFirstResponder() // self.window?.makeFirstResponder(nil) let openPanel = NSOpenPanel() openPanel.allowedFileTypes = ["pdf"] openPanel.allowsMultipleSelection = false openPanel.beginSheetModal(for: self.window!) { (result) in if result == NSApplication.ModalResponse.OK { #if VERSION_DMG // Add code for VERSION_DMG #else // if let url = openPanel.url { // do { // let bookmarkData = try url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil) // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: url) // AppSandboxFileAccess.fileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, forURL: URL(fileURLWithPath: url.path ?? url.absoluteString)) // } catch { // // Handle error // print("Error creating bookmark data: \(error)") // } // } #endif if let filePath = openPanel.url?.path { let url = NSURL(fileURLWithPath: filePath) AppSandboxFileAccess().persistPermissionURL(url as URL) let bookmarkData = try?url.bookmarkData(options: NSURL.BookmarkCreationOptions.withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil) if bookmarkData != nil { AppSandboxFileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData! as Data, for: url as URL) AppSandboxFileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData! as Data, for: NSURL(fileURLWithPath: url.path!) as URL) } // let isAccessFilePath = AppSandboxFileAccess().accessFileURL(url as URL, persistPermission: true) { // // } // // if (isAccessFilePath) { // print("获取权限成功") // let url = NSURL(fileURLWithPath: filePath) // let bookmarkData = try?url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil) // if bookmarkData != nil { // AppSandboxFileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, for: url as URL) // AppSandboxFileAccess().bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, for: NSURL(fileURLWithPath: url.path!) as URL) // } // } else { // print("获取权限失败") // } // NSURL *url = [NSURL fileURLWithPath:folderPath]; // [[AppSandboxFileAccess fileAccess] persistPermissionURL:url]; // NSData *bookmarkData = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:NULL]; // if (bookmarkData) { // [[AppSandboxFileAccess fileAccess].bookmarkPersistanceDelegate setBookmarkData:bookmarkData forURL:url]; // [[AppSandboxFileAccess fileAccess].bookmarkPersistanceDelegate setBookmarkData:bookmarkData forURL:[NSURL fileURLWithPath:(url.path?:url.absoluteString)]]; // } self.updateDocument(filePath: filePath, isNew: isNew) { [weak self] file in if let _ = file { self?.addFilePath(filePath: filePath, isNew: isNew, password: file?.password ?? "") } } } } } } } extension KMCompareView: NSComboBoxDelegate { } extension KMCompareView: PDFViewDelegate { }