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.
This commit is contained in:
CRTkafa 2026-09-28 11:35:33 +07:00
parent 894a9de48f
commit 0732a33a21
4 changed files with 58 additions and 1 deletions

View file

@ -3814,6 +3814,7 @@ dependencies = [
"tauri", "tauri",
"tempfile", "tempfile",
"url", "url",
"webview2-com",
"windows", "windows",
] ]

View file

@ -14,7 +14,7 @@ path = "src/lib.rs"
[features] [features]
default = [] default = []
# Tauri glue (webview helpers, window system menu). Off for headless tests. # Tauri glue (webview helpers, window system menu). Off for headless tests.
tauri = ["dep:tauri", "dep:windows"] tauri = ["dep:tauri", "dep:windows", "dep:webview2-com"]
[dependencies] [dependencies]
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
@ -28,6 +28,8 @@ tauri = { version = "2", features = ["unstable"], optional = true }
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
windows = { version = "0.62", features = ["Win32_Foundation", "Win32_UI_WindowsAndMessaging"], optional = true } windows = { version = "0.62", features = ["Win32_Foundation", "Win32_UI_WindowsAndMessaging"], optional = true }
# The WebView2 COM interfaces wry already links (hidden tabs: low memory target).
webview2-com = { version = "0.39", optional = true }
[dev-dependencies] [dev-dependencies]
tempfile = "3" tempfile = "3"

View file

@ -184,3 +184,34 @@ pub fn system_menu<R: Runtime>(webview: &Webview<R>, x: f64, y: f64) -> Result<(
let _ = webview; let _ = webview;
Ok(()) 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);
}
}
}
});
}
}

View file

@ -159,6 +159,23 @@ impl TabAction {
} }
} }
/// WebView2 memory target for a tab webview. A hidden tab (background tab of a group, or a pane
/// clamped away) asks the browser to drop caches, decoded images and GPU tiles it can rebuild;
/// its page keeps running (timers, sockets, notifications). Shown tabs go back to normal.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MemoryTarget {
Normal,
Low,
}
pub fn memory_target(visible: bool) -> MemoryTarget {
if visible {
MemoryTarget::Normal
} else {
MemoryTarget::Low
}
}
/// Hands a JSON value to a member of the page runtime: `window.<global>.<member>.<method>(value)` /// Hands a JSON value to a member of the page runtime: `window.<global>.<member>.<method>(value)`
/// when the runtime exists, else parked in `window.<queue>` for the runtime to pick up at start. /// when the runtime exists, else parked in `window.<queue>` for the runtime to pick up at start.
/// `json` must be serialized JSON (never page-supplied text). /// `json` must be serialized JSON (never page-supplied text).
@ -221,6 +238,12 @@ mod tests {
assert_eq!(TabState::reported("tab-1".into(), None, None, Some(long_url), None).favicon, None); assert_eq!(TabState::reported("tab-1".into(), None, None, Some(long_url), None).favicon, None);
} }
#[test]
fn hidden_tabs_target_low_memory() {
assert_eq!(memory_target(false), MemoryTarget::Low);
assert_eq!(memory_target(true), MemoryTarget::Normal);
}
#[test] #[test]
fn layouts_default_to_the_default_profile() { fn layouts_default_to_the_default_profile() {
let layout: TabLayout = serde_json::from_str(r#"{"id":"a","url":"https://a.test/","x":0,"y":0,"width":1,"height":1,"visible":true}"#).unwrap(); let layout: TabLayout = serde_json::from_str(r#"{"id":"a","url":"https://a.test/","x":0,"y":0,"width":1,"height":1,"visible":true}"#).unwrap();