KMBaseWindowController.swift 4.7 KB

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