12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- //
- // 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)
- }
-
- func showCheckPassword(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 ?? "")
- }
- }
- }
- }
-
- }
- // 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
- }
- }
|