Skip to content

Commit

Permalink
WebView2: Enable/disable platform default zooming shortcuts, closes #569
Browse files Browse the repository at this point in the history
 (#574)

* add with_zoom(bool) to WebViewBuilder,
but only on Windows (WebView2)

* enable/disable "pinch to zoom" as well

* add changelog

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

* Request: Change variable name to include "hotkey"

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

* Request: Change comments/documentation

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

* Request: Different coding style

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

* Request: Enable builder method for other platforms

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
  • Loading branch information
JensMertelmeyer and amrbashir committed May 15, 2022
1 parent 5e03204 commit 494a110
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/allow-zooming.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": minor
---

Add option to enable/disable zoom shortcuts for WebView2, disabled by default.
17 changes: 17 additions & 0 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ pub struct WebViewAttributes {
pub transparent: bool,
/// Whether load the provided URL to [`WebView`].
pub url: Option<Url>,
/// Whether page zooming by hotkeys is enabled
///
/// ## Platform-specific
///
/// **macOS / Linux / Android / iOS**: Unsupported
pub zoom_hotkeys_enabled: bool,
/// Whether load the provided html string to [`WebView`].
/// This will be ignored if the `url` is provided.
///
Expand Down Expand Up @@ -169,6 +175,7 @@ impl Default for WebViewAttributes {
navigation_handler: None,
clipboard: false,
devtools: false,
zoom_hotkeys_enabled: false,
}
}
}
Expand Down Expand Up @@ -333,6 +340,16 @@ impl<'a> WebViewBuilder<'a> {
self
}

/// Whether page zooming by hotkeys or gestures is enabled
///
/// ## Platform-specific
///
/// **macOS / Linux / Android / iOS**: Unsupported
pub fn with_hotkeys_zoom(mut self, zoom: bool) -> Self {
self.webview.zoom_hotkeys_enabled = zoom;
self
}

/// Set a navigation handler to decide if incoming url is allowed to navigate.
///
/// The closure takes a `String` parameter as url and return `bool` to determine the url. True is
Expand Down
5 changes: 4 additions & 1 deletion src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl InnerWebView {
.SetAreDefaultContextMenusEnabled(true)
.map_err(webview2_com::Error::WindowsError)?;
settings
.SetIsZoomControlEnabled(false)
.SetIsZoomControlEnabled(attributes.zoom_hotkeys_enabled)
.map_err(webview2_com::Error::WindowsError)?;
settings
.SetAreDevToolsEnabled(false)
Expand All @@ -222,6 +222,9 @@ impl InnerWebView {
let _ = settings.SetAreDevToolsEnabled(true);
}

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

let mut rect = RECT::default();
GetClientRect(hwnd, &mut rect);
controller
Expand Down

0 comments on commit 494a110

Please sign in to comment.