123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- //
- // 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()
- }
- }
- }
|