KMBaseWindowController.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 KMBaseWindowController {
  52. static func checkPassword(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. completion(true, "")
  60. return
  61. }
  62. if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
  63. completion(true, "")
  64. return
  65. }
  66. // 加密文件,尝试解锁
  67. if password.isEmpty == false {
  68. let preStatus = document.permissionsStatus
  69. document.unlock(withPassword: password)
  70. if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
  71. completion(true, password)
  72. return
  73. }
  74. }
  75. // 弹密码弹窗
  76. Task { @MainActor in
  77. KMPasswordInputWindow.openWindow(window: NSWindow.currentWindow(), type: type, url: url) { result , password in
  78. if (result == .cancel) {
  79. completion(false, "")
  80. return
  81. } else {
  82. completion(true, password ?? "")
  83. }
  84. }
  85. }
  86. }
  87. }