KMPDFEditWindowController.swift 4.8 KB

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