// // KMPDFEditWindowController.swift // PDF Reader Pro // // Created by tangchao on 2023/12/7. // import Cocoa class KMPDFEditWindowController: KMBaseWindowController { private var _viewController: KMPDFEditViewController? private var _modalSession: NSApplication.ModalSession? private var _isEdited = false convenience init(filepath: String, password: String?) { self.init(windowNibName: "KMPDFEditWindowController") if let document = CPDFDocument(url: URL(fileURLWithPath: filepath)) { self._viewController = KMPDFEditViewController(document) self._viewController?.kEventTag = 1 self._viewController?.documentEditedCallback = { [weak self] _ in self?._isEdited = true } } } override func windowDidLoad() { super.windowDidLoad() self.window?.kmCurrentWindowC = self self.window?.title = KMLocalizedString("Page Edit", nil) if self.responds(to: NSSelectorFromString("setContentViewController:")) { let contentView = self.window?.contentView self.contentViewController = self._viewController if let view = contentView { self._viewController?.view.frame = view.bounds } } else { if let vc = self._viewController { vc.view.frame = self.window?.contentView?.bounds ?? .zero self.window?.contentView?.addSubview(vc.view) } } self.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua) } @IBAction func startModal(_ sender: AnyObject?) { NSApp.stopModal() var modalCode: NSApplication.ModalResponse? if let _win = self.window { self._modalSession = NSApp.beginModalSession(for: _win) repeat { modalCode = NSApp.runModalSession(self._modalSession!) } while (modalCode == .continue) } } @IBAction func endModal(_ sender: AnyObject?) { if let session = self._modalSession { NSApp.stopModal() NSApp.endModalSession(session) self.window?.orderOut(self) } if let winC = self.window?.kmCurrentWindowC, winC.isEqual(to: self) { self.window?.kmCurrentWindowC = nil } } override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) { super.interfaceThemeDidChanged(appearance) self._viewController?.toolBar.toolBar.wantsLayer = true if appearance == .darkAqua { self._viewController?.toolBar.toolBar.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor } else { self._viewController?.toolBar.toolBar.layer?.backgroundColor = .clear } for item in self._viewController?.toolBar.toolBar.items ?? [] { item.wantsLayer = true if appearance == .darkAqua { item.layer?.backgroundColor = .clear } else { item.layer?.backgroundColor = .clear } } self._viewController?.interfaceThemeDidChanged(appearance) } } extension KMPDFEditWindowController: NSWindowDelegate { func windowShouldClose(_ sender: NSWindow) -> Bool { if (self._isEdited) { self._saveCurrentPDFDocument() return false } else { self.endModal(sender) } return true } override func close() { super.close() self.endModal(nil) } } // MARK: - Private Methods extension KMPDFEditWindowController { private func _saveCurrentPDFDocument() { let savePanel = NSSavePanel() savePanel.nameFieldStringValue = self._viewController?.thumbnailView.document?.documentURL.deletingPathExtension().lastPathComponent ?? "" savePanel.allowedFileTypes = ["pdf"] savePanel.beginSheetModal(for: self.window!) { result in if result.rawValue > 0 { let savePath = savePanel.url! var didWrite = false guard let _doc = self._viewController?.thumbnailView.document else { self.close() return } if (_doc.isEncrypted && _doc.password.isEmpty == false) { didWrite = _doc.write(to: savePath, withOptions: [.userPasswordOption : _doc.password as Any, .ownerPasswordOption : _doc.password as Any]) } else { didWrite = _doc.write(to: savePath) } if (didWrite) { let workspace = NSWorkspace.shared workspace.activateFileViewerSelecting([savePath]) } } self.close() } } }