Skip to content

Commit ac4195d

Browse files
aboseclaude
andcommitted
fix: disable browser accelerator keys (F5 reload) in Windows webviews
Uses WebView2 ICoreWebView2Settings3 API via on_page_load to disable browser accelerator keys in all windows, preventing F5 from reloading the app. Applies to both config-defined and dynamically created windows. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent cadb0f9 commit ac4195d

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

src-tauri/Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ hex = "0.4.3"
4343

4444
[target.'cfg(target_os = "windows")'.dependencies]
4545
native-dialog = "0.7.0"
46+
webview2-com = "0.19"
47+
windows = { version = "0.39", features = ["Win32_Foundation"] }
4648

4749
[target.'cfg(target_os = "linux")'.dependencies]
4850
gtk = "0.15"

src-tauri/src/main.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,25 @@ fn main() {
819819

820820
app.emit_all("single-instance", Payload { args: argv, cwd }).unwrap();
821821
}))
822+
.on_page_load(|window, _payload| {
823+
// Disable browser accelerator keys (F5 reload, Ctrl+R, etc.) in all webviews
824+
// to prevent the window from reloading on F5 key press
825+
#[cfg(target_os = "windows")]
826+
{
827+
let _ = window.with_webview(|webview| {
828+
unsafe {
829+
use webview2_com::Microsoft::Web::WebView2::Win32::ICoreWebView2Settings3;
830+
use windows::core::Interface;
831+
let core_webview = webview.controller().CoreWebView2().unwrap();
832+
let settings = core_webview.Settings().unwrap();
833+
let settings3: ICoreWebView2Settings3 = settings.cast().unwrap();
834+
settings3.SetAreBrowserAcceleratorKeysEnabled(false).unwrap();
835+
}
836+
});
837+
}
838+
#[cfg(not(target_os = "windows"))]
839+
let _ = window; // suppress unused warning on non-windows
840+
})
822841
.on_window_event(|event| {
823842
// Get the trust state from the app handle
824843
let app_handle = event.window().app_handle();

0 commit comments

Comments
 (0)