S062: memory saver sliders, resting tab icons, last_shown stamped when a tab is left

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-18 05:00:21 -06:00
commit 2f53bdec8f
5 changed files with 122 additions and 20 deletions

View file

@ -1058,6 +1058,10 @@ impl App {
if let Some(win) = self.windows.get_mut(&window)
&& index < win.tabs.len()
{
// The tab being left starts resting now, not when it was last drawn.
if let Some(old) = win.tabs.get_mut(win.active) {
old.last_shown = std::time::Instant::now();
}
win.active = index;
win.ui.omnibox_edit = win.tabs[index].draft.clone();
win.ui.close_popups();

View file

@ -1209,7 +1209,7 @@ impl App {
self.save_session();
self.flush_stores();
}
if now.duration_since(self.last_slow) >= Duration::from_secs(30) {
if now.duration_since(self.last_slow) >= Duration::from_secs((crate::perf::rest_unit_secs() / 2).clamp(1, 30)) {
self.last_slow = now;
self.slow_tick();
}
@ -1281,7 +1281,7 @@ impl App {
if i == active || tab.web.is_none() || tab.audible() || tab.pip_size.is_some() || settings.keeps_active(&tab.host()) || tab.pinned && discard == 0 {
continue;
}
let idle = now.duration_since(tab.last_shown).as_secs() / 60;
let idle = now.duration_since(tab.last_shown).as_secs() / crate::perf::rest_unit_secs();
if discard > 0 && idle >= discard as u64 && !tab.pinned {
// Unload: forget the web view, keep the address to load it again when shown.
tab.pending_url = Some(tab.url.clone());

View file

@ -131,7 +131,33 @@ pub fn tab_rest_minutes(settings: &Settings) -> (u32, u32) {
(freeze, discard)
}
/// Seconds in one rest minute: 60, or `NACRE_REST_UNIT_SECS` (smoke tests count a minute in a few seconds).
pub fn rest_unit_secs() -> u64 {
std::env::var("NACRE_REST_UNIT_SECS").ok().and_then(|v| v.parse().ok()).filter(|&v| v > 0).unwrap_or(60)
}
/// Whether animations (new tab background, ad removal, artwork spin) should stand still.
pub fn still(settings: &Settings) -> bool {
settings.perf.still_ntp || lite(settings) || energy_saver(settings)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rest_minutes_follow_the_sliders_and_low_memory_caps_them() {
let mut s = Settings::default();
s.perf.lite = AutoOnOff::Off;
s.perf.energy = EnergySaver::Off;
s.perf.freeze_min = 60;
s.perf.discard_min = 0;
assert_eq!(tab_rest_minutes(&s), (60, 0));
s.perf.lite = AutoOnOff::On;
s.perf.discard_min = 240;
assert_eq!(tab_rest_minutes(&s), (1, 15));
s.perf.freeze_min = 0;
s.perf.discard_min = 0;
assert_eq!(tab_rest_minutes(&s), (0, 0));
}
}

View file

@ -269,6 +269,9 @@ fn tab(ui: &mut Ui, f: &mut Frame, index: usize, rect: Rect, dragged: bool, drop
let favicon = t.favicon.as_ref().map(|t| t.id());
let internal = t.page.is_some();
let active = index == *f.active;
// Resting tabs look dimmer, as in Chromium: unloaded (no web view) more than frozen.
let (unloaded, frozen) = (!internal && t.web.is_none(), t.frozen);
let rest_dim = if unloaded { 0.45 } else if frozen { 0.7 } else { 1.0 };
let response = ui.interact(rect, Id::new(("tab", tab_id)), Sense::click_and_drag());
let (amount, press) = state_layer(ui, &response);
let selected = theme::effect(&ctx, Id::new(("tab-on", tab_id)), active);
@ -300,11 +303,11 @@ fn tab(ui: &mut Ui, f: &mut Frame, index: usize, rect: Rect, dragged: bool, drop
if load < 1.0 {
let size = 16.0 * (1.0 - 0.4 * load);
let mut painter = ui.painter().clone();
painter.set_opacity(1.0 - load);
painter.set_opacity((1.0 - load) * rest_dim);
if let Some(texture) = favicon {
painter.image(texture, Rect::from_center_size(icon_center, Vec2::splat(size)), Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE);
} else {
icon(ui, icon_center, if internal { icons::TAB } else { icons::PUBLIC }, size, active, content.gamma_multiply(1.0 - load));
icon(ui, icon_center, if internal { icons::TAB } else { icons::PUBLIC }, size, active, content.gamma_multiply((1.0 - load) * rest_dim));
}
}
let hovered = response.hovered() || ui.rect_contains_pointer(rect);
@ -331,7 +334,8 @@ fn tab(ui: &mut Ui, f: &mut Frame, index: usize, rect: Rect, dragged: bool, drop
ui.painter().galley(pos2(left, cy - galley.size().y / 2.0), galley, content);
}
}
let response = response.on_hover_text(format!("{title}\n{url}"));
let note = if unloaded { "\nUnloaded to save memory" } else if frozen { "\nResting in the background" } else { "" };
let response = response.on_hover_text(format!("{title}\n{url}{note}"));
if command.is_some() {
return command;
}

View file

@ -674,6 +674,27 @@ fn toolbar(ui: &mut Ui, f: &mut Frame, s: &mut Settings) {
});
}
/// Snap points of the memory saver and freeze sliders, in minutes (0 = off).
const UNLOAD_STEPS: &[u32] = &[0, 5, 15, 30, 60, 120, 240];
const FREEZE_STEPS: &[u32] = &[0, 1, 2, 5, 10, 15, 30, 60];
fn rest_label(minutes: u32, off: &str) -> String {
match minutes {
0 => off.to_owned(),
m if m % 60 == 0 => format!("{} h", m / 60),
m => format!("{m} min"),
}
}
/// A slider over `steps` that holds the nearest step (a stored value between two steps shows at the nearer one).
fn rest_slider(ui: &mut Ui, minutes: &mut u32, steps: &[u32]) {
let at = steps.iter().enumerate().min_by_key(|(_, s)| s.abs_diff(*minutes)).map_or(0, |(i, _)| i);
let mut v = at as f32;
if settled_slider(ui, &mut v, 0.0..=(steps.len() - 1) as f32, 200.0, &|v| rest_label(steps[v.round() as usize], "Off")) {
*minutes = steps[v.round() as usize];
}
}
fn performance(ui: &mut Ui, f: &mut Frame, s: &mut Settings) {
section(ui, "Memory");
card(ui, |ui| {
@ -690,14 +711,10 @@ fn performance(ui: &mut Ui, f: &mut Frame, s: &mut Settings) {
});
section(ui, "Tabs");
card(ui, |ui| {
row(ui, "Memory saver", "Unloads tabs you haven't looked at; they load again when you return", |ui| {
let opts: &[(u32, &str)] = &[(0, "Off"), (15, "15 min"), (30, "30 min"), (60, "1 h"), (120, "2 h")];
segmented(ui, &mut s.perf.discard_min, opts)
});
limited_row(ui, "Freeze background tabs", "Marks hidden tabs as frozen after a while", "WebKit already stops their drawing and slows their timers at once; it has no way to stop scripts or free memory, so unload for that", |ui| {
let opts: &[(u32, &str)] = &[(0, "Never"), (1, "1 min"), (5, "5 min"), (15, "15 min")];
segmented(ui, &mut s.perf.freeze_min, opts)
});
let saver = &mut s.perf.discard_min;
row(ui, "Memory saver", &format!("Unloads tabs you haven't looked at; they load again when you return. {}", rest_label(*saver, "Off")), |ui| rest_slider(ui, saver, UNLOAD_STEPS));
let freeze = &mut s.perf.freeze_min;
limited_row(ui, "Freeze background tabs", &format!("Marks hidden tabs as frozen after a while. {}", rest_label(*freeze, "Never")), "WebKit already stops their drawing and slows their timers at once; it has no way to stop scripts or free memory, so unload for that", |ui| rest_slider(ui, freeze, FREEZE_STEPS));
wide_row(ui, "Always keep these sites active", "Never unloaded or frozen", |ui| {
host_chips(ui, &mut s.perf.keep_active, "Remove", "No sites yet");
ui.end_row();