From 834e85ce3100cd784faf2ab561e048845793c538 Mon Sep 17 00:00:00 2001 From: weykon <635883092@qq.com> Date: Sat, 31 Dec 2022 20:21:15 +0800 Subject: [PATCH] fix window layout level problem(can not see keyboard) --- NewMan/AppDelegate.swift | 19 +++++++++++++++---- NewMan/PopoverViewController.swift | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/NewMan/AppDelegate.swift b/NewMan/AppDelegate.swift index 91f261c..4879b96 100644 --- a/NewMan/AppDelegate.swift +++ b/NewMan/AppDelegate.swift @@ -15,6 +15,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength) let toggleHotKey = HotKey(key: .space, modifiers: [.option]) var refreshHotKey : HotKey? + var escHotKey: HotKey? func applicationDidFinishLaunching(_ aNotification: Notification) { @@ -27,6 +28,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { button.sendAction(on: [.leftMouseUp, .rightMouseUp]) } popover.contentViewController = PopoverViewController.freshController() + eventMonitor = EventMonitor(mask: [.leftMouseDown, .rightMouseDown]) { [weak self] event in if let strongSelf = self, strongSelf.popover.isShown { strongSelf.closePopover(event!) @@ -61,30 +63,39 @@ class AppDelegate: NSObject, NSApplicationDelegate { } // 设置应用内快捷键,启用 - func onListenRefreshHotkey () { + func onListenOpeningHotkey () { refreshHotKey = HotKey(key: .r, modifiers: [.command]) refreshHotKey?.keyDownHandler = { let popoverViewController = self.popover.contentViewController as! PopoverViewController popoverViewController.WebView?.reload() } + escHotKey = HotKey(key: .escape,modifiers:[]) + escHotKey?.keyDownHandler = { + self.closePopover(self.popover) + } } - func unregisterRefreshHotkey(){ + func unregisterClosingHotkey(){ refreshHotKey?.keyDownHandler = nil refreshHotKey?.keyUpHandler = nil refreshHotKey = nil + escHotKey?.keyDownHandler = nil + escHotKey?.keyUpHandler = nil + escHotKey = nil } // 显示Popover @objc func showPopover(_ sender: AnyObject) { if let button = statusItem.button { popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY) - onListenRefreshHotkey() + onListenOpeningHotkey() + // 降低显示层级 + popover.contentViewController?.view.window?.level=NSWindow.Level.submenu; } eventMonitor?.start() } // 隐藏Popover @objc func closePopover(_ sender: AnyObject) { popover.performClose(sender) - unregisterRefreshHotkey() + unregisterClosingHotkey() eventMonitor?.stop() } // 接管togglePopover diff --git a/NewMan/PopoverViewController.swift b/NewMan/PopoverViewController.swift index 82970ae..25ec103 100644 --- a/NewMan/PopoverViewController.swift +++ b/NewMan/PopoverViewController.swift @@ -17,7 +17,7 @@ class PopoverViewController: NSViewController { // Do view setup here. let url = URL(string: "https://chat.openai.com/chat")! - WebView.load(URLRequest(url: url)) + WebView.load(URLRequest(url: url)) }