123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- //
- // KMNBaseWindowController.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/5/11.
- //
- import Cocoa
- class KMNBaseWindowController: NSWindowController {
-
- public var parentWindow: NSWindow?
- public var handler: ((String?) -> Void)!
- deinit {
- KMPrint(self.className + " deinit.")
-
- self.removeNotification()
- }
-
- override func windowDidLoad() {
- super.windowDidLoad()
-
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
- self.initSubViews()
- self.initDefaultValue()
- self.initNotification()
- }
-
- func initSubViews() {}
- func initDefaultValue() {
- self.window?.appearance = NSApp.appearance
- }
-
- func initNotification() {
- DistributedNotificationCenter.default().addObserver(self, selector: #selector(_themeChanged), name: NSApplication.interfaceThemeChangedNotification, object: nil)
- }
-
- func removeNotification() {
- DistributedNotificationCenter.default().removeObserver(self)
- }
-
- func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
-
- }
-
- func own_beginSheetModal(for window: NSWindow?, completionHandler handler: ((String?) -> Void)?) {
- if window != nil {
- parentWindow = window
- window!.beginSheet(self.window!) { ModalResponse in
- self.handler?(nil)
- }
- }
- self.handler = handler
- }
-
- func own_closeEndSheet() {
- parentWindow?.endSheet(self.window!)
- }
- }
- // MARK: - Private Methods
- extension KMNBaseWindowController {
- @objc private func _themeChanged(_ sender: Notification) {
- let isDarkModel = KMAdvertisementConfig.isDarkModel()
- if isDarkModel {
- self.window?.appearance = .init(named: .darkAqua)
- } else {
- self.window?.appearance = .init(named: .aqua)
- }
-
- Task { @MainActor in
- self.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
- }
- }
- }
- extension NSWindowController {
- func knCheckPassword(url: URL, type: KMPasswordInputWindowType, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
- // 判断路径 + document
- guard let document = CPDFDocument.init(url: url) else {
- return completion(false, "")
- }
- // 判断是否为加密文档
- if document.isLocked == false {
- if type == .open {
- completion(true, "")
- return
- }
- }
-
- if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
- completion(true, "")
- return
- }
-
- // 加密文件,尝试解锁
- if password.isEmpty == false {
- let preStatus = document.permissionsStatus
- document.unlock(withPassword: password)
- if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
- completion(true, password)
- return
- }
- }
-
- // 弹密码弹窗
- Task { @MainActor in
- KMPasswordInputWindow.openWindow(window: self.window!, type: type, url: url) { result , password in
- if (result == .cancel) {
- completion(false, "")
- return
- } else {
- completion(true, password ?? "")
- }
- }
- }
- }
- }
- extension KMNBaseWindowController {
- static func checkPassword(url: URL, type: KMPasswordInputWindowType, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
- // 判断路径 + document
- guard let document = CPDFDocument.init(url: url) else {
- return completion(false, "")
- }
- // 判断是否为加密文档
- if document.isLocked == false {
- if type == .open {
- completion(true, "")
- return
- }
- }
-
- if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
- completion(true, "")
- return
- }
-
- // 加密文件,尝试解锁
- if password.isEmpty == false {
- let preStatus = document.permissionsStatus
- document.unlock(withPassword: password)
- if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
- completion(true, password)
- return
- }
- }
-
- // 弹密码弹窗
- Task { @MainActor in
- KMPasswordInputWindow.openWindow(window: NSWindow.currentWindow(), type: type, url: url) { result , password in
- if (result == .cancel) {
- completion(false, "")
- return
- } else {
- completion(true, password ?? "")
- }
- }
- }
- }
- }
|