Skip to content

Commit

Permalink
feat(windows): Allow setting memory usage target level
Browse files Browse the repository at this point in the history
Signed-off-by: rhysd <[email protected]>
  • Loading branch information
rhysd committed Oct 27, 2023
1 parent 2939072 commit 912a564
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
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_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.
22 changes: 21 additions & 1 deletion src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ICoreWebView2_19>() 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(
Expand Down

0 comments on commit 912a564

Please sign in to comment.