Compare commits

..

No commits in common. "edf51714351a92b3e3a3d02267305c76703bcce0" and "99aa9bebf6923ed73f7850203704290ca94c8656" have entirely different histories.

4 changed files with 1 additions and 73 deletions

View file

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

View file

@ -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", "dep:webview2-com"]
tauri = ["dep:tauri", "dep:windows"]
[dependencies]
serde = { version = "1", features = ["derive"] }
@ -28,8 +28,6 @@ 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"

View file

@ -184,43 +184,3 @@ 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, 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<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| {
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

@ -26,11 +26,6 @@ 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,
@ -164,23 +159,6 @@ 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.<global>.<member>.<method>(value)`
/// 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).
@ -243,13 +221,6 @@ 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();