KMNBaseWindowController.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // KMNBaseWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/5/11.
  6. //
  7. import Cocoa
  8. class KMNBaseWindowController: NSWindowController {
  9. public var parentWindow: NSWindow?
  10. public var handler: ((String?) -> Void)!
  11. deinit {
  12. KMPrint(self.className + " deinit.")
  13. self.removeNotification()
  14. }
  15. override func windowDidLoad() {
  16. super.windowDidLoad()
  17. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  18. self.initSubViews()
  19. self.initDefaultValue()
  20. self.initNotification()
  21. }
  22. func initSubViews() {
  23. }
  24. func beginSheetFinish() {
  25. }
  26. func initDefaultValue() {
  27. self.window?.appearance = NSApp.appearance
  28. }
  29. func initNotification() {
  30. DistributedNotificationCenter.default().addObserver(self, selector: #selector(_themeChanged), name: NSApplication.interfaceThemeChangedNotification, object: nil)
  31. }
  32. func removeNotification() {
  33. DistributedNotificationCenter.default().removeObserver(self)
  34. }
  35. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  36. }
  37. func own_beginSheetModal(for window: NSWindow?, completionHandler handler: ((String?) -> Void)?) {
  38. if window != nil {
  39. parentWindow = window
  40. window!.beginSheet(self.window!) { ModalResponse in
  41. self.handler?(nil)
  42. }
  43. }
  44. self.handler = handler
  45. beginSheetFinish()
  46. }
  47. func own_closeEndSheet() {
  48. parentWindow?.endSheet(self.window!)
  49. }
  50. }
  51. // MARK: - Private Methods
  52. extension KMNBaseWindowController {
  53. @objc private func _themeChanged(_ sender: Notification) {
  54. let isDarkModel = KMAdvertisementConfig.isDarkModel()
  55. if isDarkModel {
  56. self.window?.appearance = .init(named: .darkAqua)
  57. } else {
  58. self.window?.appearance = .init(named: .aqua)
  59. }
  60. Task { @MainActor in
  61. self.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  62. }
  63. }
  64. }
  65. extension NSWindowController {
  66. func knCheckPassword(url: URL, type: KMPasswordInputWindowType, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  67. // 判断路径 + document
  68. guard let document = CPDFDocument.init(url: url) else {
  69. return completion(false, "")
  70. }
  71. // 判断是否为加密文档
  72. if document.isLocked == false {
  73. if type == .open {
  74. completion(true, "")
  75. return
  76. }
  77. }
  78. if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
  79. completion(true, "")
  80. return
  81. }
  82. // 加密文件,尝试解锁
  83. if password.isEmpty == false {
  84. let preStatus = document.permissionsStatus
  85. document.unlock(withPassword: password)
  86. if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
  87. completion(true, password)
  88. return
  89. }
  90. }
  91. // 弹密码弹窗
  92. Task { @MainActor in
  93. KMPasswordInputWindow.openWindow(window: self.window!, type: type, url: url) { result , password in
  94. if (result == .cancel) {
  95. completion(false, "")
  96. return
  97. } else {
  98. completion(true, password ?? "")
  99. }
  100. }
  101. }
  102. }
  103. }
  104. extension KMNBaseWindowController {
  105. static func checkPassword(url: URL, type: KMPasswordInputWindowType, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  106. // 判断路径 + document
  107. guard let document = CPDFDocument.init(url: url) else {
  108. return completion(false, "")
  109. }
  110. // 判断是否为加密文档
  111. if document.isLocked == false {
  112. if type == .open {
  113. completion(true, "")
  114. return
  115. }
  116. }
  117. if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
  118. completion(true, "")
  119. return
  120. }
  121. // 加密文件,尝试解锁
  122. if password.isEmpty == false {
  123. let preStatus = document.permissionsStatus
  124. document.unlock(withPassword: password)
  125. if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
  126. completion(true, password)
  127. return
  128. }
  129. }
  130. // 弹密码弹窗
  131. Task { @MainActor in
  132. KMPasswordInputWindow.openWindow(window: NSWindow.currentWindow(), type: type, url: url) { result , password in
  133. if (result == .cancel) {
  134. completion(false, "")
  135. return
  136. } else {
  137. completion(true, password ?? "")
  138. }
  139. }
  140. }
  141. }
  142. }