// // NSWindowController+KMExtension.swift // PDF Reader Pro // // Created by tangchao on 2023/7/27. // import Foundation @objc protocol KMForceProtocol: NSObjectProtocol { @objc optional func setForceOnTop(_ flag: Bool) } extension NSWindowController { override func km_window() -> NSWindow? { return self.window } func forceSubwindowsOnTop(_ flag: Bool) { guard let doc = self.document as? NSDocument else { NSSound.beep() return } for wc in doc.windowControllers { if wc.responds(to: #selector(setForceOnTop)) { wc.setForceOnTop(flag) } } } } extension NSWindowController: KMForceProtocol { func setForceOnTop(_ flag: Bool) { // no things. } } // MARK: - 互动模式 @objc enum KMInteractionMode: Int { case normal = 0 // 常规模式 case fullScreen case presentation // 幻灯片模式 case legacyFullScreen } @objc protocol KMInteractionModeProtocol: NSObjectProtocol { @objc optional var interactionMode: KMInteractionMode { get } @objc optional var isSwitchingFullScreen: Bool { get } @objc optional func canEnterPresentation() -> Bool @objc optional func canExitPresentation() -> Bool @objc optional func enterPresentation() @objc optional func exitPresentation() } @objc protocol KMInteractionProviderProtocol: NSObjectProtocol { @objc optional func providerContentView(fullScreenWindow: NSWindow, inset: CGFloat) -> NSView? } extension NSWindowController: KMInteractionModeProtocol { var interactionMode: KMInteractionMode { get { if let win = self.window?.interactionParent { return win.interactionMode } return self.window?.interactionMode ?? .normal } } var isSwitchingFullScreen: Bool { get { if let win = self.window?.interactionParent { return win.isSwitchingFullScreen } return self.window?.isSwitchingFullScreen ?? false } } func canEnterPresentation() -> Bool { if let win = self.window?.interactionParent { return win.canEnterPresentation() } return self.window?.canEnterPresentation() ?? false } func canExitPresentation() -> Bool { if let win = self.window?.interactionParent { return win.canExitPresentation() } return self.window?.canExitPresentation() ?? false } } @objc extension NSView: KMInteractionModeProtocol { var interactionMode: KMInteractionMode { get { if let win = self.window?.interactionParent { return win.interactionMode } return self.window?.interactionMode ?? .normal } } var isSwitchingFullScreen: Bool { get { if let win = self.window?.interactionParent { return win.isSwitchingFullScreen } return self.window?.isSwitchingFullScreen ?? false } } func canEnterPresentation() -> Bool { if let win = self.window?.interactionParent { return win.canEnterPresentation() } return self.window?.canEnterPresentation() ?? false } func canExitPresentation() -> Bool { if let win = self.window?.interactionParent { return win.canExitPresentation() } return self.window?.canExitPresentation() ?? false } } @objc extension NSViewController: KMInteractionModeProtocol { var interactionMode: KMInteractionMode { get { if let win = self.view.window?.interactionParent { return win.interactionMode } return self.view.window?.interactionMode ?? .normal } } var isSwitchingFullScreen: Bool { get { if let win = self.view.window?.interactionParent { return win.isSwitchingFullScreen } return self.view.window?.isSwitchingFullScreen ?? false } } func canEnterPresentation() -> Bool { if let win = self.view.window?.interactionParent { return win.canEnterPresentation() } return self.view.window?.canEnterPresentation() ?? false } func canExitPresentation() -> Bool { if let win = self.view.window?.interactionParent { return win.canExitPresentation() } return self.view.window?.canExitPresentation() ?? false } }