Skip to content

Commit

Permalink
feat(windows): allow setting memory usage target level to reduce memo…
Browse files Browse the repository at this point in the history
…ry consumption of inactive application (#1053)

* feat(windows): Allow setting memory usage target level

Signed-off-by: rhysd <[email protected]>

* chore(doc): clarify `WebviewExtWindows::set_low_memory_usage` does nothing with WebView2 earlier than v1.0.1020.30

Signed-off-by: rhysd <[email protected]>

* chore(doc): apply review suggestions by @FabianLars

Co-authored-by: Fabian-Lars <[email protected]>

* feat(windows): change the memory usage level to `MemoryUsageLevel` enum

Signed-off-by: rhysd <[email protected]>

* use en-us link

---------

Signed-off-by: rhysd <[email protected]>
Co-authored-by: Fabian-Lars <[email protected]>
  • Loading branch information
rhysd and FabianLars authored Oct 28, 2023
1 parent 2939072 commit f420665
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/add-api-to-set-memory-usage-for-windows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Add `WebviewExtWindows::set_memory_usage_level` 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 consumption. Please read the [guide for WebView2](https://github.com/MicrosoftEdge/WebView2Feedback/blob/main/specs/MemoryUsageTargetLevel.md) for more details.
37 changes: 36 additions & 1 deletion src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,14 +1134,45 @@ pub fn webview_version() -> Result<String> {
platform_webview_version()
}

/// The [memory usage target level][1]. 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 reduce the application's memory consumption.
///
/// [1]: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2memoryusagetargetlevel
#[cfg(target_os = "windows")]
#[non_exhaustive]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MemoryUsageLevel {
/// The 'Normal' memory usage. Applications should set this level when they are becoming active.
#[default]
Normal,
/// The 'Low' memory usage. Applications can reduce memory comsumption by setting this level when
/// they are becoming inactive.
Low,
}

/// Additional methods on `WebView` that are specific to Windows.
#[cfg(target_os = "windows")]
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].
///
/// When to best use this mode depends on the app in question. Most commonly it's called when
/// the app's visiblity state changes.
///
/// Please read the [guide for WebView2][2] for more details.
///
/// This method uses a WebView2 API added in Runtime version 114.0.1823.32. When it is used in
/// an older Runtime version, it does nothing.
///
/// [1]: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2memoryusagetargetlevel
/// [2]: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.memoryusagetargetlevel?view=webview2-dotnet-1.0.2088.41#remarks
fn set_memory_usage_level(&self, level: MemoryUsageLevel);
}

#[cfg(target_os = "windows")]
Expand All @@ -1153,6 +1184,10 @@ impl WebviewExtWindows for WebView {
fn set_theme(&self, theme: Theme) {
self.webview.set_theme(theme)
}

fn set_memory_usage_level(&self, level: MemoryUsageLevel) {
self.webview.set_memory_usage_level(level);
}
}

/// Additional methods on `WebView` that are specific to Linux.
Expand Down
16 changes: 15 additions & 1 deletion src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ mod resize;

use crate::{
webview::{
proxy::ProxyConfig, PageLoadEvent, RequestAsyncResponder, WebContext, WebViewAttributes, RGBA,
proxy::ProxyConfig, MemoryUsageLevel, PageLoadEvent, RequestAsyncResponder, WebContext,
WebViewAttributes, RGBA,
},
Error, Result,
};
Expand Down Expand Up @@ -967,6 +968,19 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
pub fn set_theme(&self, theme: Theme) {
set_theme(&self.webview, theme);
}

pub fn set_memory_usage_level(&self, level: MemoryUsageLevel) {
let Ok(webview) = self.webview.cast::<ICoreWebView2_19>() else {
return;
};
// https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2memoryusagetargetlevel
let level = match level {
MemoryUsageLevel::Normal => 0,
MemoryUsageLevel::Low => 1,
};
let level = COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL(level);
let _ = unsafe { webview.SetMemoryUsageTargetLevel(level) };
}
}

unsafe fn prepare_web_request_response(
Expand Down

0 comments on commit f420665

Please sign in to comment.