From a5cecb377756217d8655ee79de13e97af2f8f734 Mon Sep 17 00:00:00 2001 From: NimBold Date: Thu, 27 Aug 2026 17:34:04 +0330 Subject: [PATCH] fix(startup): block Windows activation during WebView2 init (#37) - Issue #37: keep the Windows host hidden, unfocused, and non-activatable while WebView2 is constructed.\n- Restore focusability and reveal the main window only after Tauri reports Ready.\n- Preserve queued startup restores on non-Windows instead of clearing them before servicing.\n\nFixes #37. --- src-tauri/src/lib.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 8b0b572..9edbd8f 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -3993,6 +3993,18 @@ fn mark_main_window_startup_complete(app_handle: &tauri::AppHandle) { } } +#[cfg(target_os = "windows")] +fn reveal_main_window(app_handle: &tauri::AppHandle) { + if let Some(window) = app_handle.get_webview_window("main") { + if let Err(error) = window.set_focusable(true) { + log::warn!("Could not make the main window focusable: {error}"); + } + if let Err(error) = window.show() { + eprintln!("Failed to reveal the main window: {error}"); + } + } +} + fn restore_pending_main_window(app_handle: &tauri::AppHandle) { if app_handle .try_state::() @@ -18541,10 +18553,13 @@ pub fn run() { #[cfg(target_os = "windows")] { // Wry installs its parent WM_SETFOCUS handler while WebView2 - // is still being initialized. Keep the host unfocused and - // hidden until RunEvent::Ready so Windows cannot re-enter that - // handler during native construction. - main_window_builder = main_window_builder.visible(false).focused(false); + // is still being initialized. Keep the host hidden and + // non-activatable until RunEvent::Ready so Windows cannot + // re-enter that handler during native construction. + main_window_builder = main_window_builder + .visible(false) + .focused(false) + .focusable(false); } main_window_builder .build() @@ -19883,10 +19898,10 @@ pub fn run() { .run(|app_handle, event| match event { tauri::RunEvent::Ready => { mark_main_window_startup_complete(app_handle); - if let Some(state) = app_handle.try_state::() { - state.requested.swap(false, Ordering::AcqRel); - } - restore_main_window(app_handle); + #[cfg(target_os = "windows")] + reveal_main_window(app_handle); + #[cfg(not(target_os = "windows"))] + restore_pending_main_window(app_handle); } #[cfg(target_os = "macos")] tauri::RunEvent::Opened { urls } => {