Skip to content

Commit

Permalink
enhance(windows): support webview2 version before 86 (#1322)
Browse files Browse the repository at this point in the history
* Support webview2 version < 86

* Add change file

* Wrong name
  • Loading branch information
Legend-Master authored Jul 22, 2024
1 parent 86faa70 commit 5231a37
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changes/support-webview2-before-86.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Support WebView2 version older than 86.0.616.0 and document version requirements for `back_forward_navigation_gestures`, `with_user_agent`, `with_hotkeys_zoom`, `with_browser_accelerator_keys`
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ pub struct WebViewAttributes {
///
/// ## Platform-specific:
///
/// - Windows: Setting to `false` does nothing on WebView2 Runtime version before 92.0.902.0,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10902-prerelease
///
/// - **Android / iOS:** Unsupported.
pub back_forward_navigation_gestures: bool,

Expand Down Expand Up @@ -843,6 +846,11 @@ impl<'a> WebViewBuilder<'a> {
}

/// Set a custom [user-agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) for the WebView.
///
/// ## Platform-specific
///
/// - Windows: Requires WebView2 Runtime version 86.0.616.0 or higher, does nothing on older versions,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10790-prerelease
pub fn with_user_agent(mut self, user_agent: &str) -> Self {
self.attrs.user_agent = Some(user_agent.to_string());
self
Expand All @@ -868,7 +876,10 @@ impl<'a> WebViewBuilder<'a> {
///
/// ## Platform-specific
///
/// **macOS / Linux / Android / iOS**: Unsupported
/// - Windows: Setting to `false` can't disable pinch zoom on WebView2 Runtime version before 91.0.865.0,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10865-prerelease
///
/// - **macOS / Linux / Android / iOS**: Unsupported
pub fn with_hotkeys_zoom(mut self, zoom: bool) -> Self {
self.attrs.zoom_hotkeys_enabled = zoom;
self
Expand Down Expand Up @@ -1108,6 +1119,9 @@ pub trait WebViewBuilderExtWindows {
/// `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.
///
/// Setting to `false` does nothing on WebView2 Runtime version before 92.0.902.0,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10824-prerelease
///
/// <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;

Expand Down
21 changes: 12 additions & 9 deletions src/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,21 +509,24 @@ impl InnerWebView {
settings.SetAreDevToolsEnabled(attributes.devtools)?;

if let Some(user_agent) = &attributes.user_agent {
let settings2: ICoreWebView2Settings2 = webview.Settings()?.cast()?;
let user_agent = HSTRING::from(user_agent);
settings2.SetUserAgent(&user_agent)?;
if let Ok(settings2) = settings.cast::<ICoreWebView2Settings2>() {
settings2.SetUserAgent(&HSTRING::from(user_agent))?;
}
}

if !pl_attrs.browser_accelerator_keys {
let settings3 = settings.cast::<ICoreWebView2Settings5>()?;
settings3.SetAreBrowserAcceleratorKeysEnabled(false)?;
if let Ok(settings3) = settings.cast::<ICoreWebView2Settings3>() {
settings3.SetAreBrowserAcceleratorKeysEnabled(false)?;
}
}

let settings5 = settings.cast::<ICoreWebView2Settings5>()?;
settings5.SetIsPinchZoomEnabled(attributes.zoom_hotkeys_enabled)?;
if let Ok(settings5) = settings.cast::<ICoreWebView2Settings5>() {
settings5.SetIsPinchZoomEnabled(attributes.zoom_hotkeys_enabled)?;
}

let settings6 = settings.cast::<ICoreWebView2Settings6>()?;
settings6.SetIsSwipeNavigationEnabled(attributes.back_forward_navigation_gestures)?;
if let Ok(settings6) = settings.cast::<ICoreWebView2Settings6>() {
settings6.SetIsSwipeNavigationEnabled(attributes.back_forward_navigation_gestures)?;
}

if let Ok(settings9) = settings.cast::<ICoreWebView2Settings9>() {
settings9.SetIsNonClientRegionSupportEnabled(true)?;
Expand Down

0 comments on commit 5231a37

Please sign in to comment.