host: low memory only for parked tabs, not for tabs hidden behind a shell overlay; skip unchanged targets

This commit is contained in:
CRTkafa 2026-09-28 19:42:34 +07:00
parent f0d44e6d9a
commit cae3d96b2a
2 changed files with 23 additions and 8 deletions

View file

@ -189,11 +189,20 @@ pub fn system_menu<R: Runtime>(webview: &Webview<R>, x: f64, y: f64) -> Result<(
/// ([`tabs::memory_target`]): hidden tabs run at `COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW` /// ([`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 /// (ICoreWebView2_19, WebView2 Runtime 114+; older runtimes just skip it). Other platforms only
/// show/hide. /// show/hide.
pub fn set_tab_visible<R: Runtime>(webview: &Webview<R>, visible: bool) { pub fn set_tab_visible<R: Runtime>(webview: &Webview<R>, visible: bool, parked: bool) {
let _ = if visible { webview.show() } else { webview.hide() }; let _ = if visible { webview.show() } else { webview.hide() };
#[cfg(windows)] #[cfg(windows)]
{ {
let target = tabs::memory_target(visible); 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<Mutex<HashMap<String, tabs::MemoryTarget>>> = 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| { let _ = webview.with_webview(move |platform| {
use webview2_com::Microsoft::Web::WebView2::Win32::{ use webview2_com::Microsoft::Web::WebView2::Win32::{
ICoreWebView2_19, COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW, COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL, ICoreWebView2_19, COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_LOW, COREWEBVIEW2_MEMORY_USAGE_TARGET_LEVEL_NORMAL,

View file

@ -26,6 +26,11 @@ pub struct TabLayout {
pub width: f64, pub width: f64,
pub height: f64, pub height: f64,
pub visible: bool, 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. /// Another site pane of the same account already shows the site's navigation rail.
#[serde(default)] #[serde(default)]
pub nav_redundant: bool, pub nav_redundant: bool,
@ -168,11 +173,11 @@ pub enum MemoryTarget {
Low, Low,
} }
pub fn memory_target(visible: bool) -> MemoryTarget { pub fn memory_target(visible: bool, parked: bool) -> MemoryTarget {
if visible { if !visible && parked {
MemoryTarget::Normal
} else {
MemoryTarget::Low MemoryTarget::Low
} else {
MemoryTarget::Normal
} }
} }
@ -240,8 +245,9 @@ mod tests {
#[test] #[test]
fn hidden_tabs_target_low_memory() { fn hidden_tabs_target_low_memory() {
assert_eq!(memory_target(false), MemoryTarget::Low); assert_eq!(memory_target(false, true), MemoryTarget::Low, "a parked background tab");
assert_eq!(memory_target(true), MemoryTarget::Normal); assert_eq!(memory_target(true, false), MemoryTarget::Normal);
assert_eq!(memory_target(false, false), MemoryTarget::Normal, "hidden only behind a shell overlay");
} }
#[test] #[test]