Skip to content

Commit

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

* Add change file

* Use "returns error" instead of fails

* Add docs to `with_incognito` as well
  • Loading branch information
Legend-Master authored Jul 2, 2024
1 parent f9e6bcc commit 39fc82c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changes/support-webview2-before-101.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Support WebView2 version older than 101.0.1210.39 and document `incognito` and `theme` will not work for versions before it
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ pub struct WebViewAttributes {
///
/// ## Platform-specific:
///
/// - Windows: Requires WebView2 Runtime version 101.0.1210.39 or higher, does nothing on older versions,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10121039
/// - **Android:** Unsupported yet.
pub incognito: bool,

Expand Down Expand Up @@ -961,6 +963,8 @@ impl<'a> WebViewBuilder<'a> {
///
/// ## Platform-specific:
///
/// - Windows: Requires WebView2 Runtime version 101.0.1210.39 or higher, does nothing on older versions,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10121039
/// - **Android:** Unsupported yet.
pub fn with_incognito(mut self, incognito: bool) -> Self {
self.attrs.incognito = incognito;
Expand Down Expand Up @@ -1110,6 +1114,9 @@ pub trait WebViewBuilderExtWindows {
/// Specifies the theme of webview2. This affects things like `prefers-color-scheme`.
///
/// Defaults to [`Theme::Auto`] which will follow the OS defaults.
///
/// Requires WebView2 Runtime version 101.0.1210.39 or higher, does nothing on older versions,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10121039
fn with_theme(self, theme: Theme) -> Self;

/// Determines whether the custom protocols should use `https://<scheme>.path/to/page` instead of the default `http://<scheme>.path/to/page`.
Expand Down Expand Up @@ -1503,6 +1510,9 @@ pub trait WebViewExtWindows {
fn controller(&self) -> ICoreWebView2Controller;

/// Changes the webview2 theme.
///
/// Requires WebView2 Runtime version 101.0.1210.39 or higher, returns error on older versions,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10121039
fn set_theme(&self, theme: Theme) -> Result<()>;

/// Sets the [memory usage target level][1].
Expand Down
40 changes: 30 additions & 10 deletions src/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,29 @@ impl InnerWebView {
incognito: bool,
) -> Result<ICoreWebView2Controller> {
let (tx, rx) = mpsc::channel();
let env = env.clone().cast::<ICoreWebView2Environment10>()?;
let controller_opts = unsafe { env.CreateCoreWebView2ControllerOptions()? };

unsafe { controller_opts.SetIsInPrivateModeEnabled(incognito)? }
let env = env.clone();
let env10 = env.cast::<ICoreWebView2Environment10>();

CreateCoreWebView2ControllerCompletedHandler::wait_for_async_operation(
Box::new(move |handler| unsafe {
env
.CreateCoreWebView2ControllerWithOptions(hwnd, &controller_opts, &handler)
.map_err(Into::into)
}),
if let Ok(env10) = env10 {
let controller_opts = unsafe { env10.CreateCoreWebView2ControllerOptions()? };
unsafe { controller_opts.SetIsInPrivateModeEnabled(incognito)? }
Box::new(
move |handler: ICoreWebView2CreateCoreWebView2ControllerCompletedHandler| unsafe {
env10
.CreateCoreWebView2ControllerWithOptions(hwnd, &controller_opts, &handler)
.map_err(Into::into)
},
)
} else {
Box::new(
move |handler: ICoreWebView2CreateCoreWebView2ControllerCompletedHandler| unsafe {
env
.CreateCoreWebView2Controller(hwnd, &handler)
.map_err(Into::into)
},
)
},
Box::new(move |error_code, controller| {
error_code?;
tx.send(controller.ok_or_else(|| windows::core::Error::from(E_POINTER)))
Expand All @@ -363,7 +375,15 @@ impl InnerWebView {

// Theme
if let Some(theme) = pl_attrs.theme {
unsafe { set_theme(&webview, theme)? };
if let Err(error) = unsafe { set_theme(&webview, theme) } {
match error {
// Ignore cast error
Error::WebView2Error(webview2_com::Error::WindowsError(windows_error))
if windows_error.code() == E_NOINTERFACE => {}
// Return error if other things went wrong
_ => return Err(error),
};
}
}

// Background color
Expand Down

0 comments on commit 39fc82c

Please sign in to comment.