Compare commits
2 commits
99aa9bebf6
...
edf5171435
| Author | SHA1 | Date | |
|---|---|---|---|
| edf5171435 | |||
| 3ef4a27a6b |
4 changed files with 73 additions and 1 deletions
1
crates/waybetter-host/Cargo.lock
generated
1
crates/waybetter-host/Cargo.lock
generated
|
|
@ -3814,6 +3814,7 @@ dependencies = [
|
||||||
"tauri",
|
"tauri",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"url",
|
"url",
|
||||||
|
"webview2-com",
|
||||||
"windows",
|
"windows",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
|
|
|
||||||
|
|
@ -184,3 +184,43 @@ 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, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
@ -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.<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 +243,13 @@ 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, 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]
|
#[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();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue