KMPDFEditWindowController.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // KMPDFEditWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/12/7.
  6. //
  7. import Cocoa
  8. class KMPDFEditWindowController: NSWindowController {
  9. private var _viewController: KMPDFEditViewController?
  10. private var _modalSession: NSApplication.ModalSession?
  11. private var _isEdited = false
  12. convenience init(filepath: String, password: String?) {
  13. self.init(windowNibName: "KMPDFEditWindowController")
  14. if let document = CPDFDocument(url: URL(fileURLWithPath: filepath)) {
  15. self._viewController = KMPDFEditViewController(document)
  16. self._viewController?.documentEditedCallback = { [unowned self] _ in
  17. self._isEdited = true
  18. }
  19. }
  20. }
  21. override func windowDidLoad() {
  22. super.windowDidLoad()
  23. self.window?.title = KMLocalizedString("Page Edit", nil)
  24. if self.responds(to: NSSelectorFromString("setContentViewController:")) {
  25. self.contentViewController = self._viewController
  26. } else {
  27. if let vc = self._viewController {
  28. vc.view.frame = self.window?.contentView?.bounds ?? .zero
  29. self.window?.contentView?.addSubview(vc.view)
  30. }
  31. }
  32. }
  33. @IBAction func startModal(_ sender: AnyObject?) {
  34. NSApp.stopModal()
  35. var modalCode: NSApplication.ModalResponse?
  36. if let _win = self.window {
  37. self._modalSession = NSApp.beginModalSession(for: _win)
  38. repeat {
  39. modalCode = NSApp.runModalSession(self._modalSession!)
  40. } while (modalCode == .continue)
  41. }
  42. }
  43. @IBAction func endModal(_ sender: AnyObject?) {
  44. if let session = self._modalSession {
  45. NSApp.stopModal()
  46. NSApp.endModalSession(session)
  47. self.window?.orderOut(self)
  48. }
  49. }
  50. }
  51. extension KMPDFEditWindowController: NSWindowDelegate {
  52. func windowShouldClose(_ sender: NSWindow) -> Bool {
  53. if (self._isEdited) {
  54. self._saveCurrentPDFDocument()
  55. return false
  56. } else {
  57. self.endModal(sender)
  58. }
  59. return true
  60. }
  61. override func close() {
  62. super.close()
  63. self.endModal(nil)
  64. }
  65. }
  66. // MARK: - Private Methods
  67. extension KMPDFEditWindowController {
  68. private func _saveCurrentPDFDocument() {
  69. let savePanel = NSSavePanel()
  70. savePanel.nameFieldStringValue = self._viewController?.thumbnailView.document?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  71. savePanel.allowedFileTypes = ["pdf"]
  72. savePanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  73. if result.rawValue > 0 {
  74. let savePath = savePanel.url!
  75. var didWrite = false
  76. guard let _doc = self._viewController?.thumbnailView.document else {
  77. self.close()
  78. return
  79. }
  80. if (_doc.isEncrypted && _doc.password.isEmpty == false) {
  81. didWrite = _doc.write(to: savePath, withOptions: [.userPasswordOption : _doc.password as Any, .ownerPasswordOption : _doc.password as Any])
  82. } else {
  83. didWrite = _doc.write(to: savePath)
  84. }
  85. if (didWrite) {
  86. let workspace = NSWorkspace.shared
  87. workspace.activateFileViewerSelecting([savePath])
  88. }
  89. }
  90. self.close()
  91. }
  92. }
  93. }