Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(windows): Allow disabling browser-specific accelerator keys (closes #791) #792

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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