From 912a56447580ac5180bb0e9a6109addcd68d7c51 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 27 Oct 2023 17:11:05 +0900 Subject: [PATCH] feat(windows): Allow setting memory usage target level Signed-off-by: rhysd --- ...add-api-to-set-memory-usage-for-windows.md | 5 +++++ src/webview/mod.rs | 22 ++++++++++++++++++- src/webview/webview2/mod.rs | 10 +++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 .changes/add-api-to-set-memory-usage-for-windows.md diff --git a/.changes/add-api-to-set-memory-usage-for-windows.md b/.changes/add-api-to-set-memory-usage-for-windows.md new file mode 100644 index 000000000..9f61d6184 --- /dev/null +++ b/.changes/add-api-to-set-memory-usage-for-windows.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +Add `WebviewExtWindows::set_low_memory_usage` API to set [the memory usage target level](https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2memoryusagetargetlevel) on Windows. Setting 'Low' memory usage target level when an application is going to inactive can significantly reduce the memory usage by the application. Please read [the guide for WebView2](https://github.com/MicrosoftEdge/WebView2Feedback/blob/main/specs/MemoryUsageTargetLevel.md) for more details. diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 7fdd1438d..4f11650c4 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -1140,8 +1140,24 @@ pub trait WebviewExtWindows { /// Returns WebView2 Controller fn controller(&self) -> ICoreWebView2Controller; - // Changes the webview2 theme. + /// Changes the webview2 theme. fn set_theme(&self, theme: Theme); + + /// Sets [the memory usage target level][1] to 'Low'. There are two levels 'Low' and 'Normal' and + /// the default level is 'Normal'. When the application is going inactive, setting the level to + /// 'Low' can significantly save the memory used by the application. + /// + /// Passing `true` to `enabled` argument means the WebView will switch the level to 'Low' and + /// passing `false` will switch to 'Normal'. + /// + /// The logic when application goes to inactive/active is specific to the application. + /// [`tao::event::WindowEvent::Focused`] event may be useful. + /// + /// Please read [the guide for WebView2][2] for more details. + /// + /// [1]: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2memoryusagetargetlevel + /// [2]: https://github.com/MicrosoftEdge/WebView2Feedback/blob/main/specs/MemoryUsageTargetLevel.md + fn set_low_memory_usage(&self, enabled: bool); } #[cfg(target_os = "windows")] @@ -1153,6 +1169,10 @@ impl WebviewExtWindows for WebView { fn set_theme(&self, theme: Theme) { self.webview.set_theme(theme) } + + fn set_low_memory_usage(&self, enabled: bool) { + self.webview.set_low_memory_usage(enabled); + } } /// Additional methods on `WebView` that are specific to Linux. diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index a141b29bb..8e2a743f2 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -967,6 +967,16 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ pub fn set_theme(&self, theme: Theme) { set_theme(&self.webview, theme); } + + pub fn set_low_memory_usage(&self, enabled: bool) { + let Ok(webview) = self.webview.cast::() else { + return; + }; + // https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2memoryusagetargetlevel + let level = if enabled { 1 } else { 0 }; + let level = COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL(level); + let _ = unsafe { webview.SetMemoryUsageTargetLevel(level) }; + } } unsafe fn prepare_web_request_response(