KMBaseWindowController.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. if let data = self.window?.appearance?.name, data == .darkAqua {
  41. self.window?.appearance = .init(named: .aqua)
  42. } else {
  43. self.window?.appearance = .init(named: .darkAqua)
  44. }
  45. Task { @MainActor in
  46. self.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  47. }
  48. }
  49. }
  50. extension KMBaseWindowController {
  51. static func checkPassword(url: URL, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  52. // 判断路径 + document
  53. guard let document = CPDFDocument.init(url: url) else {
  54. return completion(false, "")
  55. }
  56. // 判断是否为加密文档
  57. if document.isLocked == false {
  58. completion(true, "")
  59. return
  60. }
  61. if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
  62. completion(true, "")
  63. return
  64. }
  65. // 加密文件,尝试解锁
  66. if password.isEmpty == false {
  67. let preStatus = document.permissionsStatus
  68. document.unlock(withPassword: password)
  69. if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
  70. completion(true, password)
  71. return
  72. }
  73. }
  74. // 弹密码弹窗
  75. Task { @MainActor in
  76. let passwordWindowController = PasswordWindowController(windowNibName: "PasswordWindowController")
  77. passwordWindowController.fileURL = url
  78. let window = NSWindow.currentWindow()
  79. window.km_beginSheet(windowC: passwordWindowController)
  80. passwordWindowController.closeCallBack = { passwordString in
  81. window.km_quick_endSheet()
  82. if passwordString.count != 0 {
  83. document.unlock(withPassword: passwordString)
  84. completion(true, passwordString)
  85. } else {
  86. completion(false, "")
  87. }
  88. }
  89. }
  90. }
  91. }