1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- //
- // NSWindowController+Extension.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/10/17.
- //
- import Foundation
- extension NSWindowController {
-
- //MARK: - show方法,确保居中显示
- func showKMWindow(_ sender: Any?) {
- self.window?.center()
-
- self.showWindow(sender)
- }
- }
- // MARK: - Sheet
- extension NSWindowController {
- static var _sheetParentResponderKey = "KMSheetParentResponderKey"
- unowned var sheetParentResponder: NSResponder? {
- get {
- return objc_getAssociatedObject(self, &Self._sheetParentResponderKey) as? NSResponder
- }
- }
-
- static var _sheetFlagKey = "KMSheetFlagKey"
- var sheetFlag: Bool {
- get {
- return objc_getAssociatedObject(self, &Self._sheetFlagKey) as? Bool ?? false
- }
- set {
- objc_setAssociatedObject(self, &Self._sheetFlagKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
- }
- }
-
- // MARK: - Private Methods
- func _setSheetParentResponder(_ newValue: NSResponder?) {
- objc_setAssociatedObject(self, &Self._sheetParentResponderKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
- }
- }
- extension NSWindowController {
- func handleRightMouseDown(theEvent: NSEvent) -> Bool {
- return false
- }
- }
- //MARK: - Password
- extension NSWindowController {
- 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 ?? "")
- }
- }
- }
- }
- }
|