host: hidden tab webviews use the WebView2 low memory target

set_tab_visible shows/hides a tab webview and sets
ICoreWebView2_19::SetMemoryUsageTargetLevel (LOW when hidden, NORMAL when
shown). Hidden tabs keep running; the browser trims their working set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
CRTkafa 2026-09-28 11:35:33 +07:00
parent 99aa9bebf6
commit 3ef4a27a6b
4 changed files with 58 additions and 1 deletions

View file

@ -184,3 +184,34 @@ pub fn system_menu<R: Runtime>(webview: &Webview<R>, x: f64, y: f64) -> Result<(
let _ = webview;
Ok(())
}
/// Shows or hides a tab webview and sets its WebView2 memory target to match
/// ([`tabs::memory_target`]): hidden tabs run at `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW`
/// (ICoreWebView2_19, WebView2 Runtime 114+; older runtimes just skip it). Other platforms only
/// show/hide.
pub fn set_tab_visible<R: Runtime>(webview: &Webview<R>, visible: bool) {
let _ = if visible { webview.show() } else { webview.hide() };
#[cfg(windows)]
{
let target = tabs::memory_target(visible);
let _ = webview.with_webview(move |platform| {
use webview2_com::Microsoft::Web::WebView2::Win32::{
ICoreWebView2_19, COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW, COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL,
};
use windows::core::Interface;
let level = match target {
tabs::MemoryTarget::Low => COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW,
tabs::MemoryTarget::Normal => COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL,
};
// SAFETY: COM calls on the live controller of this webview, on the main thread
// (with_webview runs the closure there).
unsafe {
if let Ok(core) = platform.controller().CoreWebView2() {
if let Ok(core) = core.cast::<ICoreWebView2_19>() {
let _ = core.SetMemoryUsageTargetLevel(level);
}
}
}
});
}
}