diff --git a/crates/waybetter-host/Cargo.lock b/crates/waybetter-host/Cargo.lock index 0c67b9c..0e05bdd 100644 --- a/crates/waybetter-host/Cargo.lock +++ b/crates/waybetter-host/Cargo.lock @@ -3814,6 +3814,7 @@ dependencies = [ "tauri", "tempfile", "url", + "webview2-com", "windows", ] diff --git a/crates/waybetter-host/Cargo.toml b/crates/waybetter-host/Cargo.toml index eb32459..365c53b 100644 --- a/crates/waybetter-host/Cargo.toml +++ b/crates/waybetter-host/Cargo.toml @@ -14,7 +14,7 @@ path = "src/lib.rs" [features] default = [] # 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] serde = { version = "1", features = ["derive"] } @@ -28,6 +28,8 @@ tauri = { version = "2", features = ["unstable"], optional = true } [target.'cfg(windows)'.dependencies] 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] tempfile = "3" diff --git a/crates/waybetter-host/src/host.rs b/crates/waybetter-host/src/host.rs index 7e85a1d..1127b03 100644 --- a/crates/waybetter-host/src/host.rs +++ b/crates/waybetter-host/src/host.rs @@ -184,3 +184,43 @@ pub fn system_menu(webview: &Webview, 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(webview: &Webview, visible: bool, parked: bool) { + let _ = if visible { webview.show() } else { webview.hide() }; + #[cfg(windows)] + { + use std::collections::HashMap; + use std::sync::{Mutex, OnceLock}; + // Last target per webview label: a sync that changes nothing costs no COM call. + static LAST: OnceLock>> = OnceLock::new(); + let target = tabs::memory_target(visible, parked); + let mut last = LAST.get_or_init(Default::default).lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + if last.insert(webview.label().to_owned(), target) == Some(target) { + return; + } + drop(last); + 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::() { + let _ = core.SetMemoryUsageTargetLevel(level); + } + } + } + }); + } +} diff --git a/crates/waybetter-host/src/tabs.rs b/crates/waybetter-host/src/tabs.rs index 00d2be7..d6e9c2f 100644 --- a/crates/waybetter-host/src/tabs.rs +++ b/crates/waybetter-host/src/tabs.rs @@ -26,6 +26,11 @@ pub struct TabLayout { pub width: f64, pub height: f64, pub visible: bool, + /// Not shown for a reason other than a shell overlay (a background tab of its group, a pane + /// with no room). Only parked tabs get the low memory target; a tab merely covered by a shell + /// menu or the command palette stays at normal. + #[serde(default)] + pub parked: bool, /// Another site pane of the same account already shows the site's navigation rail. #[serde(default)] pub nav_redundant: bool, @@ -159,6 +164,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, parked: bool) -> MemoryTarget { + if !visible && parked { + MemoryTarget::Low + } else { + MemoryTarget::Normal + } +} + /// Hands a JSON value to a member of the page runtime: `window...(value)` /// when the runtime exists, else parked in `window.` for the runtime to pick up at start. /// `json` must be serialized JSON (never page-supplied text). @@ -221,6 +243,13 @@ mod tests { 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, true), MemoryTarget::Low, "a parked background tab"); + assert_eq!(memory_target(true, false), MemoryTarget::Normal); + assert_eq!(memory_target(false, false), MemoryTarget::Normal, "hidden only behind a shell overlay"); + } + #[test] 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();