KMNBaseWindowController.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. func initDefaultValue() {
  24. self.window?.appearance = NSApp.appearance
  25. }
  26. func initNotification() {
  27. DistributedNotificationCenter.default().addObserver(self, selector: #selector(_themeChanged), name: NSApplication.interfaceThemeChangedNotification, object: nil)
  28. }
  29. func removeNotification() {
  30. DistributedNotificationCenter.default().removeObserver(self)
  31. }
  32. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  33. }
  34. func own_beginSheetModal(for window: NSWindow?, completionHandler handler: ((String?) -> Void)?) {
  35. if window != nil {
  36. parentWindow = window
  37. window!.beginSheet(self.window!) { ModalResponse in
  38. self.handler?(nil)
  39. }
  40. }
  41. self.handler = handler
  42. }
  43. func own_closeEndSheet() {
  44. parentWindow?.endSheet(self.window!)
  45. }
  46. }
  47. // MARK: - Private Methods
  48. extension KMNBaseWindowController {
  49. @objc private func _themeChanged(_ sender: Notification) {
  50. let isDarkModel = KMAdvertisementConfig.isDarkModel()
  51. if isDarkModel {
  52. self.window?.appearance = .init(named: .darkAqua)
  53. } else {
  54. self.window?.appearance = .init(named: .aqua)
  55. }
  56. Task { @MainActor in
  57. self.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  58. }
  59. }
  60. }
  61. extension NSWindowController {
  62. func knCheckPassword(url: URL, type: KMPasswordInputWindowType, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  63. // 判断路径 + document
  64. guard let document = CPDFDocument.init(url: url) else {
  65. return completion(false, "")
  66. }
  67. // 判断是否为加密文档
  68. if document.isLocked == false {
  69. if type == .open {
  70. completion(true, "")
  71. return
  72. }
  73. }
  74. if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
  75. completion(true, "")
  76. return
  77. }
  78. // 加密文件,尝试解锁
  79. if password.isEmpty == false {
  80. let preStatus = document.permissionsStatus
  81. document.unlock(withPassword: password)
  82. if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
  83. completion(true, password)
  84. return
  85. }
  86. }
  87. // 弹密码弹窗
  88. Task { @MainActor in
  89. KMPasswordInputWindow.openWindow(window: self.window!, type: type, url: url) { result , password in
  90. if (result == .cancel) {
  91. completion(false, "")
  92. return
  93. } else {
  94. completion(true, password ?? "")
  95. }
  96. }
  97. }
  98. }
  99. }
  100. extension KMNBaseWindowController {
  101. static func checkPassword(url: URL, type: KMPasswordInputWindowType, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  102. // 判断路径 + document
  103. guard let document = CPDFDocument.init(url: url) else {
  104. return completion(false, "")
  105. }
  106. // 判断是否为加密文档
  107. if document.isLocked == false {
  108. if type == .open {
  109. completion(true, "")
  110. return
  111. }
  112. }
  113. if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
  114. completion(true, "")
  115. return
  116. }
  117. // 加密文件,尝试解锁
  118. if password.isEmpty == false {
  119. let preStatus = document.permissionsStatus
  120. document.unlock(withPassword: password)
  121. if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
  122. completion(true, password)
  123. return
  124. }
  125. }
  126. // 弹密码弹窗
  127. Task { @MainActor in
  128. KMPasswordInputWindow.openWindow(window: NSWindow.currentWindow(), type: type, url: url) { result , password in
  129. if (result == .cancel) {
  130. completion(false, "")
  131. return
  132. } else {
  133. completion(true, password ?? "")
  134. }
  135. }
  136. }
  137. }
  138. }