Skip to content

Commit

Permalink
Windows: Use CoreWebView2EnvironmentOptions API.
Browse files Browse the repository at this point in the history
Use the CoreWebView2EnvironmentOptions API wrapper when creating a
WebView2 environment on Windows, as opposed to using the
ICoreWebView2EnvironmentOptions* COM interfaces. The wrapper exposes
the interface methods with a more Rust-like API.

It may be possible to do this with ICoreWebView2Controller as well, but
I don't feel like experimenting with that right now. This is just an
improvement I realised I could make while implementing tauri-apps#1339.

Signed-off-by: Jamie Ridding <[email protected]>
  • Loading branch information
Themayu committed Aug 19, 2024
1 parent 266c6d6 commit b27eb5b
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,26 +287,23 @@ impl InnerWebView {
arguments
});

let additional_browser_args = HSTRING::from(additional_browser_args);

let (tx, rx) = mpsc::channel();
CreateCoreWebView2EnvironmentCompletedHandler::wait_for_async_operation(
Box::new(move |environmentcreatedhandler| unsafe {
let options: ICoreWebView2EnvironmentOptions =
CoreWebView2EnvironmentOptions::default().into();
let options = CoreWebView2EnvironmentOptions::default();

let _ = options.SetAdditionalBrowserArguments(&additional_browser_args);
options.set_additional_browser_arguments(additional_browser_args);

// Get user's system language
let lcid = GetUserDefaultUILanguage();
let mut lang = [0; MAX_LOCALE_NAME as usize];
LCIDToLocaleName(lcid as u32, Some(&mut lang), LOCALE_ALLOW_NEUTRAL_NAMES);
options.SetLanguage(PCWSTR::from_raw(lang.as_ptr()))?;
options.set_language(String::from_utf16_lossy(&lang));

CreateCoreWebView2EnvironmentWithOptions(
PCWSTR::null(),
&data_directory.unwrap_or_default(),
&options,
&ICoreWebView2EnvironmentOptions::from(options),
&environmentcreatedhandler,
)
.map_err(Into::into)
Expand Down

0 comments on commit b27eb5b

Please sign in to comment.