KMPDFEditWindowController.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // KMPDFEditWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/7.
  6. //
  7. import Cocoa
  8. class KMPDFEditWindowController: KMBaseWindowController {
  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 = { [weak self] _ in
  17. self?._isEdited = true
  18. }
  19. }
  20. }
  21. override func windowDidLoad() {
  22. super.windowDidLoad()
  23. self.window?.kmCurrentWindowC = self
  24. self.window?.title = KMLocalizedString("Page Edit", nil)
  25. if self.responds(to: NSSelectorFromString("setContentViewController:")) {
  26. let contentView = self.window?.contentView
  27. self.contentViewController = self._viewController
  28. if let view = contentView {
  29. self._viewController?.view.frame = view.bounds
  30. }
  31. } else {
  32. if let vc = self._viewController {
  33. vc.view.frame = self.window?.contentView?.bounds ?? .zero
  34. self.window?.contentView?.addSubview(vc.view)
  35. }
  36. }
  37. self.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  38. }
  39. @IBAction func startModal(_ sender: AnyObject?) {
  40. NSApp.stopModal()
  41. var modalCode: NSApplication.ModalResponse?
  42. if let _win = self.window {
  43. self._modalSession = NSApp.beginModalSession(for: _win)
  44. repeat {
  45. modalCode = NSApp.runModalSession(self._modalSession!)
  46. } while (modalCode == .continue)
  47. }
  48. }
  49. @IBAction func endModal(_ sender: AnyObject?) {
  50. if let session = self._modalSession {
  51. NSApp.stopModal()
  52. NSApp.endModalSession(session)
  53. self.window?.orderOut(self)
  54. }
  55. if let winC = self.window?.kmCurrentWindowC, winC.isEqual(to: self) {
  56. self.window?.kmCurrentWindowC = nil
  57. }
  58. }
  59. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  60. super.interfaceThemeDidChanged(appearance)
  61. self._viewController?.toolBar.toolBar.wantsLayer = true
  62. if appearance == .darkAqua {
  63. self._viewController?.toolBar.toolBar.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  64. } else {
  65. self._viewController?.toolBar.toolBar.layer?.backgroundColor = .clear
  66. }
  67. for item in self._viewController?.toolBar.toolBar.items ?? [] {
  68. item.wantsLayer = true
  69. if appearance == .darkAqua {
  70. item.layer?.backgroundColor = .clear
  71. } else {
  72. item.layer?.backgroundColor = .clear
  73. }
  74. }
  75. self._viewController?.interfaceThemeDidChanged(appearance)
  76. }
  77. }
  78. extension KMPDFEditWindowController: NSWindowDelegate {
  79. func windowShouldClose(_ sender: NSWindow) -> Bool {
  80. if (self._isEdited) {
  81. self._saveCurrentPDFDocument()
  82. return false
  83. } else {
  84. self.endModal(sender)
  85. }
  86. return true
  87. }
  88. override func close() {
  89. super.close()
  90. self.endModal(nil)
  91. }
  92. }
  93. // MARK: - Private Methods
  94. extension KMPDFEditWindowController {
  95. private func _saveCurrentPDFDocument() {
  96. let savePanel = NSSavePanel()
  97. savePanel.nameFieldStringValue = self._viewController?.thumbnailView.document?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  98. savePanel.allowedFileTypes = ["pdf"]
  99. savePanel.beginSheetModal(for: self.window!) { result in
  100. if result.rawValue > 0 {
  101. let savePath = savePanel.url!
  102. var didWrite = false
  103. guard let _doc = self._viewController?.thumbnailView.document else {
  104. self.close()
  105. return
  106. }
  107. if (_doc.isEncrypted && _doc.password.isEmpty == false) {
  108. didWrite = _doc.write(to: savePath, withOptions: [.userPasswordOption : _doc.password as Any, .ownerPasswordOption : _doc.password as Any])
  109. } else {
  110. didWrite = _doc.write(to: savePath)
  111. }
  112. if (didWrite) {
  113. let workspace = NSWorkspace.shared
  114. workspace.activateFileViewerSelecting([savePath])
  115. }
  116. }
  117. self.close()
  118. }
  119. }
  120. }