KMPDFEditWindowController.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. let contentView = self.window?.contentView
  26. self.contentViewController = self._viewController
  27. if let view = contentView {
  28. self._viewController?.view.frame = view.bounds
  29. }
  30. } else {
  31. if let vc = self._viewController {
  32. vc.view.frame = self.window?.contentView?.bounds ?? .zero
  33. self.window?.contentView?.addSubview(vc.view)
  34. }
  35. }
  36. }
  37. @IBAction func startModal(_ sender: AnyObject?) {
  38. NSApp.stopModal()
  39. var modalCode: NSApplication.ModalResponse?
  40. if let _win = self.window {
  41. self._modalSession = NSApp.beginModalSession(for: _win)
  42. repeat {
  43. modalCode = NSApp.runModalSession(self._modalSession!)
  44. } while (modalCode == .continue)
  45. }
  46. }
  47. @IBAction func endModal(_ sender: AnyObject?) {
  48. if let session = self._modalSession {
  49. NSApp.stopModal()
  50. NSApp.endModalSession(session)
  51. self.window?.orderOut(self)
  52. }
  53. }
  54. }
  55. extension KMPDFEditWindowController: NSWindowDelegate {
  56. func windowShouldClose(_ sender: NSWindow) -> Bool {
  57. if (self._isEdited) {
  58. self._saveCurrentPDFDocument()
  59. return false
  60. } else {
  61. self.endModal(sender)
  62. }
  63. return true
  64. }
  65. override func close() {
  66. super.close()
  67. self.endModal(nil)
  68. }
  69. }
  70. // MARK: - Private Methods
  71. extension KMPDFEditWindowController {
  72. private func _saveCurrentPDFDocument() {
  73. let savePanel = NSSavePanel()
  74. savePanel.nameFieldStringValue = self._viewController?.thumbnailView.document?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  75. savePanel.allowedFileTypes = ["pdf"]
  76. savePanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  77. if result.rawValue > 0 {
  78. let savePath = savePanel.url!
  79. var didWrite = false
  80. guard let _doc = self._viewController?.thumbnailView.document else {
  81. self.close()
  82. return
  83. }
  84. if (_doc.isEncrypted && _doc.password.isEmpty == false) {
  85. didWrite = _doc.write(to: savePath, withOptions: [.userPasswordOption : _doc.password as Any, .ownerPasswordOption : _doc.password as Any])
  86. } else {
  87. didWrite = _doc.write(to: savePath)
  88. }
  89. if (didWrite) {
  90. let workspace = NSWorkspace.shared
  91. workspace.activateFileViewerSelecting([savePath])
  92. }
  93. }
  94. self.close()
  95. }
  96. }
  97. }