Skip to content

Commit

Permalink
feat(windows): Allow disabling browser-specific accelerator keys (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd authored Dec 7, 2022
1 parent 4e23c0f commit 6e622ff
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/webview2_browser_accelerator_keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "patch"
---

Add `WebViewBuilderExtWindows::with_browser_accelerator_keys` method to allow disabling browser-specific accelerator keys enabled in WebView2 by default. When `false` is passed, it disables all accelerator keys that access features specific to a web browser. See [the official WebView2 document](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings#arebrowseracceleratorkeysenabled) for more details.
23 changes: 22 additions & 1 deletion src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,18 @@ impl Default for WebViewAttributes {
}

#[cfg(windows)]
#[derive(Default)]
pub(crate) struct PlatformSpecificWebViewAttributes {
additional_browser_args: Option<String>,
browser_accelerator_keys: bool,
}
#[cfg(windows)]
impl Default for PlatformSpecificWebViewAttributes {
fn default() -> Self {
Self {
additional_browser_args: None,
browser_accelerator_keys: true, // This is WebView2's default behavior
}
}
}
#[cfg(any(
target_os = "linux",
Expand Down Expand Up @@ -593,6 +602,13 @@ pub trait WebViewBuilderExtWindows {
/// By default wry passes `--disable-features=msWebOOUI,msPdfOOUI,msSmartScreenProtection`
/// so if you use this method, you also need to disable these components by yourself if you want.
fn with_additional_browser_args<S: Into<String>>(self, additional_args: S) -> Self;

/// Determines whether browser-specific accelerator keys are enabled. When this setting is set to
/// `false`, it disables all accelerator keys that access features specific to a web browser.
/// The default value is `true`. See the following link to know more details.
///
/// https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings#arebrowseracceleratorkeysenabled
fn with_browser_accelerator_keys(self, enabled: bool) -> Self;
}

#[cfg(windows)]
Expand All @@ -601,6 +617,11 @@ impl WebViewBuilderExtWindows for WebViewBuilder<'_> {
self.platform_specific.additional_browser_args = Some(additional_args.into());
self
}

fn with_browser_accelerator_keys(mut self, enabled: bool) -> Self {
self.platform_specific.browser_accelerator_keys = enabled;
self
}
}

/// The fundamental type to present a [`WebView`].
Expand Down
18 changes: 17 additions & 1 deletion src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,17 @@ impl InnerWebView {
let file_drop_handler = attributes.file_drop_handler.take();
let file_drop_window = window.clone();

let browser_accelerator_keys = pl_attrs.browser_accelerator_keys;
let env = Self::create_environment(&web_context, pl_attrs)?;
let controller = Self::create_controller(hwnd, &env)?;
let webview = Self::init_webview(window, hwnd, attributes, &env, &controller)?;
let webview = Self::init_webview(
window,
hwnd,
attributes,
&env,
&controller,
browser_accelerator_keys,
)?;

if let Some(file_drop_handler) = file_drop_handler {
let mut controller = FileDropController::new();
Expand Down Expand Up @@ -190,6 +198,7 @@ impl InnerWebView {
mut attributes: WebViewAttributes,
env: &ICoreWebView2Environment,
controller: &ICoreWebView2Controller,
browser_accelerator_keys: bool,
) -> webview2_com::Result<ICoreWebView2> {
let webview =
unsafe { controller.CoreWebView2() }.map_err(webview2_com::Error::WindowsError)?;
Expand Down Expand Up @@ -246,6 +255,13 @@ impl InnerWebView {
.SetAreDevToolsEnabled(true)
.map_err(webview2_com::Error::WindowsError)?;
}
if !browser_accelerator_keys {
if let Ok(settings3) = settings.cast::<ICoreWebView2Settings3>() {
settings3
.SetAreBrowserAcceleratorKeysEnabled(false)
.map_err(webview2_com::Error::WindowsError)?;
}
}

let settings5 = settings.cast::<ICoreWebView2Settings5>()?;
let _ = settings5.SetIsPinchZoomEnabled(attributes.zoom_hotkeys_enabled);
Expand Down

0 comments on commit 6e622ff

Please sign in to comment.