diff --git a/.changes/support-webview2-before-101.md b/.changes/support-webview2-before-101.md new file mode 100644 index 000000000..3e0d973e5 --- /dev/null +++ b/.changes/support-webview2-before-101.md @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 244e514ec..d39b111b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, @@ -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; @@ -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://.path/to/page` instead of the default `http://.path/to/page`. @@ -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]. diff --git a/src/webview2/mod.rs b/src/webview2/mod.rs index 7f73d1719..661cdd97c 100644 --- a/src/webview2/mod.rs +++ b/src/webview2/mod.rs @@ -328,17 +328,29 @@ impl InnerWebView { incognito: bool, ) -> Result { let (tx, rx) = mpsc::channel(); - let env = env.clone().cast::()?; - let controller_opts = unsafe { env.CreateCoreWebView2ControllerOptions()? }; - - unsafe { controller_opts.SetIsInPrivateModeEnabled(incognito)? } + let env = env.clone(); + let env10 = env.cast::(); 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))) @@ -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