Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Desktop: stop accidental reloads in the Windows WebView (#7945)

oobabooga4532df20e07b
diff --git a/.github/workflows/studio-tauri-smoke.yml b/.github/workflows/studio-tauri-smoke.yml
index 21f8574e853..f883d227fac 100644
--- a/.github/workflows/studio-tauri-smoke.yml
+++ b/.github/workflows/studio-tauri-smoke.yml
@@ -8,9 +8,11 @@
# - Tauri CLI version drift (we pin 2.10.1, matching release-desktop.yml)
# - frontend output not picked up by Tauri's distDir
#
-# Linux-only on a free `ubuntu-latest` runner. Mac and Windows desktop builds
-# stay in release-desktop.yml (manual `workflow_dispatch`) because they need
-# code-signing secrets and ~30 min of runner time each.
+# The build job stays on Linux; signed desktop builds remain in the manual
+# release workflow.
+#
+# A second job covers `#[cfg(windows)]` code, which Linux strips before type
+# checking and cannot cross-compile because `ring` requires MSVC `lib.exe`.
name: Unsloth Tauri CI
@@ -274,7 +276,8 @@ jobs:
windows-unit-tests:
name: Rust unit tests (windows)
runs-on: windows-latest
- timeout-minutes: 25
+ # A cold cache compiles both the debug and the release dependency tree.
+ timeout-minutes: 45
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
@@ -315,3 +318,10 @@ jobs:
- name: Rust unit tests (studio/src-tauri)
working-directory: studio/src-tauri
run: cargo test --no-fail-fast
+
+ # `cargo test` builds only the debug profile, and the browser guard is
+ # installed under `not(debug_assertions)`, so nothing above compiles its
+ # call site. Check the release profile so that path cannot rot.
+ - name: Rust release type check (studio/src-tauri)
+ working-directory: studio/src-tauri
+ run: cargo check --all-targets --release
diff --git a/studio/src-tauri/Cargo.lock b/studio/src-tauri/Cargo.lock
index dea047a9f85..003afdbe102 100644
--- a/studio/src-tauri/Cargo.lock
+++ b/studio/src-tauri/Cargo.lock
@@ -5759,7 +5759,9 @@ dependencies = [
"time",
"tokio",
"webkit2gtk-sys",
+ "webview2-com",
"windows 0.62.2",
+ "windows-core 0.61.2",
"windows-sys 0.61.2",
"winreg 0.55.0",
]
diff --git a/studio/src-tauri/Cargo.toml b/studio/src-tauri/Cargo.toml
index 6df5842d0a7..a3fd5b8be90 100644
--- a/studio/src-tauri/Cargo.toml
+++ b/studio/src-tauri/Cargo.toml
@@ -57,9 +57,12 @@ elevated-command = "1.1.2"
webkit2gtk-sys = "2.0.2"
[target.'cfg(windows)'.dependencies]
+# Keep these versions aligned with Tauri's WebView2 controller bindings.
+webview2-com = "0.38"
winreg = "0.55"
windows = { version = "0.62.2", features = ["Win32_System_Threading"] }
-windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Globalization", "Win32_Security", "Win32_System_Console", "Win32_System_Diagnostics_ToolHelp", "Win32_System_JobObjects", "Win32_System_Threading"] }
+windows-core = "0.61.2"
+windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Globalization", "Win32_Security", "Win32_System_Console", "Win32_System_Diagnostics_ToolHelp", "Win32_System_JobObjects", "Win32_System_Threading", "Win32_UI_Input_KeyboardAndMouse"] }
[build-dependencies]
tauri-build = { version = "2", features = [] }
diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rs
index 0dd439943bd..edae2d796a5 100644
--- a/studio/src-tauri/src/main.rs
+++ b/studio/src-tauri/src/main.rs
@@ -502,6 +502,105 @@ fn setup_custom_titlebar(app: &tauri::App) -> Result<(), Box<dyn std::error::Err
Ok(())
}
+// Compile in both Windows profiles so dependency skew fails in CI, even though
+// the guard is installed only in release builds.
+#[cfg(windows)]
+#[cfg_attr(debug_assertions, allow(dead_code))]
+fn setup_windows_browser_guards(app: &tauri::App) -> Result<(), Box<dyn std::error::Error>> {
+ use webview2_com::Microsoft::Web::WebView2::Win32::{
+ ICoreWebView2Controller, ICoreWebView2_11, COREWEBVIEW2_CONTEXT_MENU_TARGET_KIND,
+ COREWEBVIEW2_CONTEXT_MENU_TARGET_KIND_PAGE,
+ };
+ use webview2_com::{AcceleratorKeyPressedEventHandler, ContextMenuRequestedEventHandler};
+ use windows_core::Interface;
+ use windows_core::BOOL;
+ use windows_sys::Win32::UI::Input::KeyboardAndMouse::{
+ GetKeyState, VK_CONTROL, VK_F5, VK_MENU, VK_R, VK_SHIFT,
+ };
+
+ let window = app.get_webview_window("main").ok_or_else(|| {
+ std::io::Error::new(std::io::ErrorKind::NotFound, "main window not found")
+ })?;
+ window.with_webview(|webview| unsafe {
+ // Keep the standard menus and non-refresh accelerators that the WebView2
+ // settings would disable together. Tauri does not expose those settings.
+ // The explicit type makes dependency version mismatches fail here.
+ let controller: ICoreWebView2Controller = webview.controller();
+ let accelerator_handler =
+ AcceleratorKeyPressedEventHandler::create(Box::new(move |_, event_args| {
+ let Some(event_args) = event_args else {
+ return Ok(());
+ };
+ let mut virtual_key = 0;
+ event_args.VirtualKey(&mut virtual_key)?;
+ let control_down = GetKeyState(i32::from(VK_CONTROL)) < 0;
+ // AltGr reports as Ctrl+Alt, so never treat it as Ctrl+R.
+ let alt_down = GetKeyState(i32::from(VK_MENU)) < 0;
+ // Preserve Ctrl+Shift+R as recovery from a broken renderer.
+ let shift_down = GetKeyState(i32::from(VK_SHIFT)) < 0;
+ if virtual_key == u32::from(VK_F5)
+ || (virtual_key == u32::from(VK_R) && control_down && !alt_down && !shift_down)
+ {
+ event_args.SetHandled(true)?;
+ }
+ Ok(())
+ }));
+ let mut accelerator_token = 0;
+ if let Err(error) =
+ controller.add_AcceleratorKeyPressed(&accelerator_handler, &mut accelerator_token)
+ {
+ warn!("Could not block Windows refresh shortcuts: {error}");
+ }
+
+ let core_webview = match controller.CoreWebView2() {
+ Ok(core_webview) => core_webview,
+ Err(error) => {
+ warn!("Could not access the Windows WebView: {error}");
+ return;
+ }
+ };
+ let core_webview11 = match core_webview.cast::<ICoreWebView2_11>() {
+ Ok(core_webview11) => core_webview11,
+ Err(error) => {
+ warn!("Could not customize the Windows context menu: {error}");
+ return;
+ }
+ };
+ let context_menu_handler =
+ ContextMenuRequestedEventHandler::create(Box::new(move |_, event_args| {
+ let Some(event_args) = event_args else {
+ return Ok(());
+ };
+ let target = event_args.ContextMenuTarget()?;
+ let mut kind = COREWEBVIEW2_CONTEXT_MENU_TARGET_KIND::default();
+ let mut is_editable = BOOL::default();
+ let mut has_link_uri = BOOL::default();
+ let mut has_selection = BOOL::default();
+ target.Kind(&mut kind)?;
+ target.IsEditable(&mut is_editable)?;
+ target.HasLinkUri(&mut has_link_uri)?;
+ target.HasSelection(&mut has_selection)?;
+ // Suppress only bare-page browser commands. Keep media, selection,
+ // editable, and link menus; links are page targets with a link URI.
+ let is_bare_page = kind == COREWEBVIEW2_CONTEXT_MENU_TARGET_KIND_PAGE
+ && !is_editable.as_bool()
+ && !has_link_uri.as_bool()
+ && !has_selection.as_bool();
+ if is_bare_page {
+ event_args.SetHandled(true)?;
+ }
+ Ok(())
+ }));
+ let mut context_menu_token = 0;
+ if let Err(error) =
+ core_webview11.add_ContextMenuRequested(&context_menu_handler, &mut context_menu_token)
+ {
+ warn!("Could not filter the Windows context menu: {error}");
+ }
+ })?;
+ Ok(())
+}
+
/// A Tauri quit never fires beforeunload, so the frontend mirrors quit protection here.
pub type TrainingActivityState = std::sync::Arc<std::sync::Mutex<bool>>;
@@ -1216,6 +1315,8 @@ fn main() {
}
#[cfg(any(target_os = "windows", target_os = "linux"))]
setup_custom_titlebar(app)?;
+ #[cfg(all(windows, not(debug_assertions)))]
+ setup_windows_browser_guards(app)?;
setup_tray(app)?;
#[cfg(unix)]
setup_unix_termination_signals(app)?;
diff --git a/tests/studio/test_desktop_reliability_frontend_contract.py b/tests/studio/test_desktop_reliability_frontend_contract.py
index 3e668995a38..474507947b9 100644
--- a/tests/studio/test_desktop_reliability_frontend_contract.py
+++ b/tests/studio/test_desktop_reliability_frontend_contract.py
@@ -403,6 +403,16 @@ def test_mac_dock_reopens_hidden_main_window():
assert "show_main_window(app)" in reopen_handler
+def test_windows_browser_guard_runs_only_in_release_builds():
+ # WebView2 is not reachable from Python, so pin the release-only call that
+ # keeps refresh controls available during development.
+ source = TAURI_MAIN.read_text(encoding = "utf-8")
+
+ assert "fn setup_windows_browser_guards" in source
+ before_call = source.split("setup_windows_browser_guards(app)?;", 1)[0]
+ assert before_call.rstrip().endswith("#[cfg(all(windows, not(debug_assertions)))]")
+
+
def test_desktop_manages_the_remote_password_through_the_account_dialog():
section = REMOTE_ACCESS_SECTION.read_text(encoding = "utf-8")
dialog = PASSWORD_DIALOG.read_text(encoding = "utf-8")