From 8b3fe8a7cc5b907f537f5cac08583a3205759e20 Mon Sep 17 00:00:00 2001 From: zipg Date: Sat, 27 Jun 2026 23:28:39 +0800 Subject: [PATCH] =?UTF-8?q?Fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=20macOS=20?= =?UTF-8?q?=E5=85=A8=E5=B1=8F=E5=85=B3=E9=97=AD=E7=AA=97=E5=8F=A3=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/lib.rs | 60 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index cb6240297..e6ec6473c 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -177,6 +177,62 @@ fn show_main_window(app: &tauri::AppHandle) { } } +fn clear_main_webview_focus(app: &tauri::AppHandle) { + if let Some(window) = app.get_webview_window("main") { + let _ = window.eval( + r#" + (() => { + const active = document.activeElement; + if (active instanceof HTMLElement) active.blur(); + if (document.body) { + if (!document.body.hasAttribute("tabindex")) { + document.body.setAttribute("tabindex", "-1"); + } + document.body.focus({ preventScroll: true }); + } + })(); + "#, + ); + } +} + +fn hide_main_window_for_close(app: &tauri::AppHandle, window: &tauri::Window) { + clear_main_webview_focus(app); + + #[cfg(target_os = "macos")] + { + if window.is_fullscreen().unwrap_or(false) { + let app = app.clone(); + let window = window.clone(); + let _ = window.set_fullscreen(false); + tauri::async_runtime::spawn(async move { + for _ in 0..40 { + tokio::time::sleep(std::time::Duration::from_millis(50)).await; + if !window.is_fullscreen().unwrap_or(false) { + tokio::time::sleep(std::time::Duration::from_millis(600)).await; + let app_to_hide = app.clone(); + let window_to_hide = window.clone(); + let _ = app.run_on_main_thread(move || { + let _ = window_to_hide.hide(); + let _ = app_to_hide.hide(); + }); + return; + } + } + let app_to_hide = app.clone(); + let window_to_hide = window.clone(); + let _ = app.run_on_main_thread(move || { + let _ = window_to_hide.hide(); + let _ = app_to_hide.hide(); + }); + }); + return; + } + } + + let _ = window.hide(); +} + fn open_connection_deep_links(app: &tauri::AppHandle, links: Vec) { if links.is_empty() { return; @@ -548,8 +604,8 @@ pub fn run() { } let app = window.app_handle(); let Some(state) = app.try_state::() else { - let _ = window.hide(); api.prevent_close(); + hide_main_window_for_close(&app, window); return; }; if !state.prompted() { @@ -561,8 +617,8 @@ pub fn run() { app.exit(0); return; } - let _ = window.hide(); api.prevent_close(); + hide_main_window_for_close(&app, window); } }) .invoke_handler(tauri::generate_handler![