Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ All notable changes to Tarn are documented in this file. The format is based on

### Changed

- Less work while idle, for battery, on every client: the start page's fog and search bar effect
hold their frame after a minute without input in the window, stop their timers, and move on from
that frame at the next input; while Tarn is not the active app it no longer checks every two
seconds whether the shown page still answers; extension messages, a title that did not change
and memory samples no longer redraw the window unless the heavy tabs or the Memory page change;
the extension timer on Linux, iOS and Android does nothing without an installed extension;
Chromium's Shield counts are written once a minute in one transaction, and only when something
was blocked.
- Less memory: on macOS a browser window keeps two screen buffers instead of three (16 MB less GPU
memory for a large window), and on macOS and Linux Tarn hands memory it freed back to the system
after tabs close or sleep and once when it leaves the front.
- The sidebar's New Tab row has one Organize Tabs button (…) in place of four: New Tab Group, New
Folder, Clean Up and Group with Assistant are in its menu, on every client.
- Enter on an address Tarn cannot open shakes the field instead of going anywhere, on every
Expand Down
11 changes: 8 additions & 3 deletions crates/app/src/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ pub struct Browser {
pub(crate) drawn_at: Option<Instant>,
pub(crate) page_fade: Option<(Rc<WebView>, Instant)>,
pub(crate) overlay_exit: Option<Instant>,
pub(crate) watch: crate::content::Watch,
pub(crate) fog: gpui::Entity<crate::content::Fog>,
pub(crate) search_fx: gpui::Entity<crate::content::SearchFx>,
pub(crate) floating: crate::content::Floating,
Expand Down Expand Up @@ -509,10 +510,13 @@ impl Browser {
let tab_placement = browser_core::layout::TabPlacement::load(disk);
let sidebar_open = crate::prefs::sidebar_open(disk) && tab_placement == browser_core::layout::TabPlacement::Side;
let sidebar_width = crate::prefs::sidebar_width(disk);
let watch = crate::content::Watch::default();
let host = Host::from_window(window);
host.iter().for_each(Host::double_buffer);
let mut browser = Browser {
core,
warning: None,
host: Host::from_window(window),
host,
model: browser_core::tabs::Model::new(live_tabs),
views: HashMap::new(),
theme_generation: ui::theme::Theme::generation(),
Expand Down Expand Up @@ -607,8 +611,9 @@ impl Browser {
drawn_at: None,
page_fade: None,
overlay_exit: None,
fog: cx.new(|_| Default::default()),
search_fx: cx.new(|_| Default::default()),
fog: cx.new(|_| watch.fog()),
search_fx: cx.new(|_| watch.search_fx()),
watch,
floating: Default::default(),
bookmarks: Vec::new(),
motion: Default::default(),
Expand Down
9 changes: 8 additions & 1 deletion crates/app/src/browser/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,19 @@ impl Browser {
}
}
Event::TitleChanged(title) => {
self.store(key.private).set_tab_title(key.id, &title).ok();
let unchanged = self.model.tab(key).is_some_and(|t| t.title == title);
if !unchanged {
self.store(key.private).set_tab_title(key.id, &title).ok();
}
// WebKit can report an empty title for the outgoing page just before the next one commits.
if let (false, Some(profile_id), Some((visit, url))) =
(title.is_empty(), self.profile_of(key), self.pages.get(&key).and_then(|p| p.visit.clone()))
{
self.store(key.private).retitle_visit(profile_id, visit, &url, &title).ok();
}
if unchanged {
return;
}
if self.private_matches(key)
&& let Some(t) = self.model.tabs.iter_mut().find(|t| t.id == key.id)
{
Expand Down Expand Up @@ -259,6 +265,7 @@ impl Browser {
if let Some(profile) = self.profile_of(key).filter(|_| !key.private) {
self.core.tarn.borrow().send(crate::tarn_ext::Msg::Tab { profile, tab: key.id as u64, world, frame, origin, json });
}
return;
}
Event::ContentProcessTerminated => {
self.link_hovered(key, None, cx);
Expand Down
147 changes: 106 additions & 41 deletions crates/app/src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,17 +796,112 @@ type FogLine = (SharedString, Vec<(std::ops::Range<usize>, HighlightStyle)>);
/// Columns, rows, animation step and theme generation of a cached frame.
type FogKey = (usize, usize, u32, u32);

type Input = Rc<std::cell::Cell<Option<Instant>>>;

#[derive(Default)]
pub(crate) struct Watch {
input: Input,
pointer: Rc<std::cell::Cell<Option<gpui::Point<gpui::Pixels>>>>,
keys: Option<gpui::Subscription>,
start: bool,
}

impl Watch {
pub(crate) fn fog(&self) -> Fog {
Fog { reduce: false, left: 0., motion: Motion::new(self.input.clone()), frame: None }
}

pub(crate) fn search_fx(&self) -> SearchFx {
SearchFx { reduce: false, motion: Motion::new(self.input.clone()) }
}

pub(crate) fn element(&mut self, start: bool, views: [gpui::EntityId; 2], window: &Window, cx: &mut Context<Browser>) -> impl IntoElement + use<> {
fn on<E: gpui::MouseEvent>(window: &mut Window, input: &Input, views: [gpui::EntityId; 2], counts: impl Fn(&E) -> bool + 'static) {
let input = input.clone();
window.on_mouse_event(move |event: &E, phase, _, cx| {
if phase == gpui::DispatchPhase::Capture && counts(event) && input.replace(Some(Instant::now())).is_none() {
views.into_iter().for_each(|id| cx.notify(id));
}
});
}
if std::mem::replace(&mut self.start, start) != start && start {
self.input.set(Some(Instant::now()));
}
if self.keys.is_none() {
let (input, handle) = (self.input.clone(), window.window_handle());
self.keys = Some(cx.observe_keystrokes(move |_, _, window, cx| {
if window.window_handle() == handle && input.replace(Some(Instant::now())).is_none() {
views.into_iter().for_each(|id| gpui::App::notify(cx, id));
}
}));
}
let (input, pointer) = (self.input.clone(), self.pointer.clone());
canvas(
|_, _, _| {},
move |_, (), window, _| {
let pointer = pointer.clone();
on(window, &input, views, move |e: &gpui::MouseMoveEvent| {
let moved =
pointer.get().is_some_and(|p| browser_core::effects::moved((p.x.into(), p.y.into()), (e.position.x.into(), e.position.y.into())));
if moved || pointer.get().is_none() {
pointer.set(Some(e.position));
}
moved
});
on(window, &input, views, |_: &gpui::MouseDownEvent| true);
on(window, &input, views, |_: &gpui::ScrollWheelEvent| true);
},
)
.absolute()
}
}

struct Motion {
input: Input,
clock: browser_core::effects::Clock,
ticking: Rc<std::cell::Cell<bool>>,
active: bool,
}

impl Motion {
fn new(input: Input) -> Self {
Motion { input, clock: Default::default(), ticking: Default::default(), active: false }
}

fn elapsed<V: 'static>(&mut self, run: bool, frame: std::time::Duration, window: &Window, cx: &mut Context<V>) -> std::time::Duration {
let now = Instant::now();
let active = engine::app_active() && window.is_window_active();
if active && !self.active {
self.input.set(Some(now));
}
self.active = active;
if !self.input.get().is_some_and(|at| browser_core::effects::awake(now - at)) {
self.input.set(None);
}
let Some(elapsed) = self.clock.tick(now, run && active && self.input.get().is_some()) else { return self.clock.held() };
if !self.ticking.replace(true) {
let ticking = self.ticking.clone();
cx.spawn(async move |this, cx| {
cx.background_executor().timer(frame).await;
ticking.set(false);
this.update(cx, |_, cx| cx.notify()).ok();
})
.detach();
}
elapsed
}
}

/// The ASCII cloud bank behind the start card, redrawn every `fog::FRAME`; its own view, so
/// windows that observe the `Browser` are not notified. One still frame while motion is reduced
/// or the theme asks for it; none when the theme turns it off. Paused unless its window is the
/// active one of the active app, so a frame redraws one window only.
#[derive(Default)]
/// active one of the active app, so a frame redraws one window only, and held after
/// `effects::IDLE` without input.
pub struct Fog {
pub(crate) reduce: bool,
/// The window's width left of the content area, which the fog does not cover.
pub(crate) left: f32,
started: Option<Instant>,
ticking: bool,
motion: Motion,
frame: Option<(FogKey, Vec<FogLine>)>,
}

Expand All @@ -817,24 +912,8 @@ impl gpui::Render for Fog {
let (cols, rows) = (((f32::from(size.width) - self.left).max(0.) / FOG_FONT.1) as usize, (f32::from(size.height) / FOG_LINE) as usize);
let mode = Theme::fog_mode();
let still = self.reduce || mode != browser_core::theme::Fog::On;
let active = engine::app_active() && window.is_window_active();
let step = match (still, active) {
(true, _) => 0,
(false, true) => fog::step(self.started.get_or_insert_with(Instant::now).elapsed()),
(false, false) => self.frame.as_ref().map_or(0, |(key, _)| key.2),
};
if !still && !self.ticking && active {
self.ticking = true;
cx.spawn(async move |this, cx| {
cx.background_executor().timer(std::time::Duration::from_secs_f32(fog::FRAME)).await;
this.update(cx, |fog, cx| {
fog.ticking = false;
cx.notify();
})
.ok();
})
.detach();
}
let elapsed = self.motion.elapsed(!still, std::time::Duration::from_secs_f32(fog::FRAME), window, cx);
let step = if still { 0 } else { fog::step(elapsed) };
let key = (cols, rows, step, Theme::generation());
if mode == browser_core::theme::Fog::Off {
self.frame = None;
Expand Down Expand Up @@ -892,33 +971,19 @@ const SEARCH_ROW: f32 = 54.;

/// The theme's search bar effect behind the start and command card (`browser_core::effects`),
/// redrawn at `effects::FPS` while its window is the active one of the active app; its own view,
/// like `Fog`. One still frame while motion is reduced or the window is in the background.
#[derive(Default)]
/// like `Fog`, and held like it. One still frame while motion is reduced.
pub struct SearchFx {
pub(crate) reduce: bool,
started: Option<Instant>,
ticking: bool,
motion: Motion,
}

impl gpui::Render for SearchFx {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
use browser_core::effects::{self, Effect};
let effect = Theme::search_effect();
let animate = !self.reduce && effect.fps() > 0. && engine::app_active() && window.is_window_active();
let t = if animate { effects::clock(self.started.get_or_insert_with(Instant::now).elapsed()) } else { effects::STILL };
if animate && !self.ticking {
self.ticking = true;
let frame = std::time::Duration::from_secs_f32(1. / effect.fps());
cx.spawn(async move |this, cx| {
cx.background_executor().timer(frame).await;
this.update(cx, |fx, cx| {
fx.ticking = false;
cx.notify();
})
.ok();
})
.detach();
}
let animate = !self.reduce && effect.fps() > 0.;
let elapsed = self.motion.elapsed(animate, std::time::Duration::from_secs_f32(1. / effect.fps().max(1.)), window, cx);
let t = if animate { effects::clock(elapsed + std::time::Duration::from_secs_f32(effects::STILL)) } else { effects::STILL };
let (side, rise) = effects::reach(effect, SEARCH_ROW);
let color = Theme::search_color();
let layer = div().absolute().top(px(-rise)).left(px(-side)).right(px(-side)).bottom(px(-side)).overflow_hidden();
Expand Down
3 changes: 3 additions & 0 deletions crates/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ impl Browser {
if self.hidden_field_focused(window, cx) || (std::mem::take(&mut self.refocus) || stranded) && !self.overlay_open() && self.active_prompt().is_none() {
window.focus(&self.focus_handle(cx));
}
let start = (self.command_mode.is_some() && !self.inline_editing()) || self.model.active.is_none();
let watch = self.watch.element(start, [self.fog.entity_id(), self.search_fx.entity_id()], window, cx);
let mut root = self.root_shell(cx);
root = self.root_tab_actions(root, cx);
root = self.root_listeners(root, cx);
Expand Down Expand Up @@ -268,6 +270,7 @@ impl Browser {
.child(timed("content", || content::render(self, window, cx))),
)
.children(assistant::render(self, window, cx))
.child(watch)
.into_any_element()
}

Expand Down
3 changes: 2 additions & 1 deletion crates/app/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ impl LibraryWindow {

fn sample_on_memory_page(&self, cx: &mut Context<Self>) {
if self.page == Page::Memory {
self.browser.update(cx, |b, cx| b.sample_processes(cx)).ok();
self.browser.update(cx, |b, _| b.sample_processes()).ok();
cx.notify();
}
}

Expand Down
4 changes: 1 addition & 3 deletions crates/app/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::collections::{HashMap, VecDeque};
use std::hash::Hash;
use std::time::{Duration, Instant};

use gpui::Context;
use platform::memory::Usage;

use crate::browser::{Browser, TabKey};
Expand Down Expand Up @@ -86,10 +85,9 @@ impl Browser {
}

/// Samples every tab's process; the CPU figures come from the change between samples.
pub(crate) fn sample_processes(&mut self, cx: &mut Context<Self>) {
pub(crate) fn sample_processes(&mut self) {
let live = self.tab_processes().into_iter().filter_map(|(_, pid)| Some((pid, platform::memory::usage(pid)?))).collect();
self.samples.record(live, Instant::now());
cx.notify();
}

pub(crate) fn tab_costs(&self) -> HashMap<TabKey, Cost> {
Expand Down
27 changes: 21 additions & 6 deletions crates/app/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,19 @@ pub(crate) fn start(cx: &mut App) -> bool {
})
.detach();
cx.spawn(async move |cx| {
let mut was_active = true;
loop {
cx.background_executor().timer(MINUTE).await;
let ticked = cx.update(|cx| {
let mut closed = false;
each(cx, |b, window, cx| {
b.sleep_idle_tabs(cx);
closed |= b.sleep_idle_tabs(cx);
b.follow_schedule(window, cx);
});
let active = engine::app_active();
if engine::lifecycle::release_memory(closed, std::mem::replace(&mut was_active, active), active) {
platform::memory::release_free();
}
#[cfg(any(feature = "chromium", windows))]
count_chromium_blocks(&self::core(cx));
});
Expand Down Expand Up @@ -293,21 +299,30 @@ pub(crate) fn start(cx: &mut App) -> bool {
pub(crate) fn count_chromium_blocks(core: &AppCore) {
let blocked = engine::chromium::take_blocked();
let Some(profile_id) = core.chromium_profile.get() else { return };
for (host, n) in blocked {
core.disk.count(profile_id, store::Counter::ChromiumRequests, n, Some(&host)).ok();
if blocked.is_empty() {
return;
}
let items: Vec<_> = blocked.iter().map(|(host, n)| (Some(host.as_str()), *n)).collect();
core.disk.count_many(profile_id, store::Counter::ChromiumRequests, &items).ok();
crate::shield_card::touch();
}

/// Each profile has its own Chromium profile on Windows, so each counts its own blocks.
#[cfg(windows)]
pub(crate) fn count_chromium_blocks(core: &AppCore) {
let mut counted = false;
for (&(private, profile_id), rt) in core.profiles.borrow().iter() {
for (host, n) in rt.data.take_blocked() {
core.store(private).count(profile_id, store::Counter::ChromiumRequests, n, Some(&host)).ok();
let blocked = rt.data.take_blocked();
if blocked.is_empty() {
continue;
}
let items: Vec<_> = blocked.iter().map(|(host, n)| (Some(host.as_str()), *n)).collect();
core.store(private).count_many(profile_id, store::Counter::ChromiumRequests, &items).ok();
counted = true;
}
if counted {
crate::shield_card::touch();
}
crate::shield_card::touch();
}

fn toast_front(tone: browser_core::toast::Tone, text: impl Into<gpui::SharedString>, action: Option<crate::toast::Action>, cx: &mut App) {
Expand Down
Loading
Loading