app: shade stale, queued-for-refresh, and actively updating tiles distinctly

This commit is contained in:
Tobi Lutke
2026-11-25 17:36:30 -05:00
parent 1d2225cd73
commit e0a4927b50
5 changed files with 413 additions and 19 deletions
+159 -4
View File
@@ -30,7 +30,8 @@ use disktree_core::treemap::{
LayoutOptions, Rect, Tile, TileKind, hit, layout_filtered,
};
use disktree_scanner::{
CachePolicy, Freshness, Scanner, StorageMode, TreeArena,
CachePolicy, DEFAULT_CACHE_TTL, Freshness, Scanner, StorageMode,
SubtreeStatus, TreeArena,
};
use gpui_kit::base::input::{InputEvent, InputState};
use gpui_kit::{
@@ -682,7 +683,7 @@ pub enum ScanIntent {
Widening { target: PathBuf },
/// Background refresh (`r`, Refresh button, or SWR revalidation): keeps
/// the current directory and selection by path when the new tree lands.
Refresh { metric: Metric },
Refresh { metric: Metric, forced: bool },
}
/// Exclusive handle to at most one in-flight scan: local OR remote, never both.
@@ -959,6 +960,9 @@ pub struct Disktree {
pub freshness: Option<Freshness>,
pub tree: Option<Arc<Node>>,
pub active_scan: Option<ActiveScan>,
pub updating_paths: FxHashSet<PathBuf>,
pub refreshed_paths: FxHashSet<PathBuf>,
pub active_scan_path: Option<PathBuf>,
pub scan_epoch: u64,
pub progress: ScanSnapshot,
pub scan_error: Option<String>,
@@ -1144,6 +1148,9 @@ impl Disktree {
freshness: None,
tree: None,
active_scan: None,
updating_paths: FxHashSet::default(),
refreshed_paths: FxHashSet::default(),
active_scan_path: None,
scan_epoch: 0,
progress: ScanSnapshot::default(),
scan_error: None,
@@ -1892,6 +1899,9 @@ impl Disktree {
/// and a widening scan leaves the tree it started from on screen.
pub fn cancel_scan(&mut self, cx: &mut Context<'_, Self>) {
self.scan_preview = None;
self.updating_paths.clear();
self.refreshed_paths.clear();
self.active_scan_path = None;
let Some(active) = self.active_scan.take() else {
return;
};
@@ -1931,7 +1941,11 @@ impl Disktree {
let epoch = self.scan_epoch;
let intent = ScanIntent::Refresh {
metric: self.options.metric,
forced: true,
};
self.updating_paths.clear();
self.refreshed_paths.clear();
self.active_scan_path = None;
self.progress = ScanSnapshot::default();
self.remote_progress =
disktree_core::remote_progress::Progress::default();
@@ -2017,6 +2031,9 @@ impl Disktree {
discard(self.tree.take());
self.arena = None;
self.freshness = None;
self.updating_paths.clear();
self.refreshed_paths.clear();
self.active_scan_path = None;
self.crumbs.clear();
self.selected = None;
self.hovered = None;
@@ -2128,7 +2145,7 @@ impl Disktree {
self.progress.bytes = self.remote_progress.bytes;
self.progress.errors = self.remote_progress.errors;
let refresh_metric = match intent {
ScanIntent::Refresh { metric } => Some(*metric),
ScanIntent::Refresh { metric, .. } => Some(*metric),
ScanIntent::Initial | ScanIntent::Widening { .. } => None,
};
let Some(outcome) = outcome else {
@@ -2189,6 +2206,7 @@ impl Disktree {
ActiveScan::Local { handle, intent } => {
let intent = intent.clone();
self.progress = handle.progress.snapshot();
self.active_scan_path = handle.active_path();
if self
.preview_updated
.is_none_or(|at| at.elapsed().as_secs() >= 1)
@@ -2216,6 +2234,9 @@ impl Disktree {
self.install_tree(node, None, cx);
if is_fresh {
self.active_scan = None;
self.updating_paths.clear();
self.refreshed_paths.clear();
self.active_scan_path = None;
self.scan_preview = None;
self.progress.finished = true;
cx.notify();
@@ -2227,12 +2248,20 @@ impl Disktree {
if let Some(active) = &mut self.active_scan {
active.set_intent(ScanIntent::Refresh {
metric: self.options.metric,
forced: false,
});
}
cx.notify();
return true;
}
ScanUpdate::SubtreeUpdating { path } => {
self.updating_paths.insert(path);
cx.notify();
return true;
}
ScanUpdate::SubtreeRefreshed { path, arena: sub } => {
self.updating_paths.remove(&path);
self.refreshed_paths.insert(path.clone());
if let Some(current_arena) = self.arena.as_mut() {
let root = current_arena.root_path().to_path_buf();
let updated = if path == root {
@@ -2277,7 +2306,7 @@ impl Disktree {
scanned_at: disktree_scanner::unix_now(),
});
match intent {
ScanIntent::Refresh { metric } => {
ScanIntent::Refresh { metric, .. } => {
let directory = self.current_path();
let selected = self
.selected
@@ -2306,6 +2335,9 @@ impl Disktree {
}
}
self.active_scan = None;
self.updating_paths.clear();
self.refreshed_paths.clear();
self.active_scan_path = None;
self.scan_preview = None;
self.progress.finished = true;
cx.notify();
@@ -3289,6 +3321,124 @@ impl Disktree {
// ── layout and hit-testing ──────────────────────────────────────────
/// When a background refresh is running, returns `Some(forced)`:
/// `true` for an explicit `r` / Refresh button rescan, `false` for SWR.
pub const fn refresh_mode(&self) -> Option<bool> {
match &self.active_scan {
Some(
ActiveScan::Local {
intent: ScanIntent::Refresh { forced, .. },
..
}
| ActiveScan::Remote {
intent: ScanIntent::Refresh { forced, .. },
..
},
) => Some(*forced),
_ => None,
}
}
/// Freshness / refresh-queue state of the tile at `crumbs`.
pub fn tile_freshness(
&self,
crumbs: &[usize],
) -> crate::treemap_view::TileFreshness {
self.tile_freshness_at(
crumbs,
disktree_scanner::unix_now(),
self.refresh_mode(),
)
}
fn tile_freshness_at(
&self,
crumbs: &[usize],
now_unix: u64,
refresh_mode: Option<bool>,
) -> crate::treemap_view::TileFreshness {
use crate::treemap_view::TileFreshness;
let ttl = DEFAULT_CACHE_TTL.as_secs();
if let Some(forced) = refresh_mode {
let Some(path) = self.path_at(crumbs) else {
return TileFreshness::Fresh;
};
let is_updating = self
.updating_paths
.iter()
.any(|u| path.starts_with(u) || u.starts_with(&path))
|| self.active_scan_path.as_ref().is_some_and(|active| {
active.starts_with(&path) || path.starts_with(active)
});
if is_updating {
return TileFreshness::Updating;
}
if self.refreshed_paths.iter().any(|r| path.starts_with(r)) {
return TileFreshness::Fresh;
}
if forced || self.arena_path_is_stale(&path, now_unix, ttl) {
return TileFreshness::Scheduled;
}
return TileFreshness::Fresh;
}
// Fast path: a freshly completed live scan is within TTL everywhere.
if let Some(Freshness::Live { scanned_at }) = self.freshness
&& now_unix.saturating_sub(scanned_at) <= ttl
{
return TileFreshness::Fresh;
}
let Some(path) = self.path_at(crumbs) else {
return TileFreshness::Fresh;
};
if self.arena_path_is_stale(&path, now_unix, ttl) {
TileFreshness::Stale
} else {
TileFreshness::Fresh
}
}
/// Whether the directory at (or holding) `path` in `self.arena` is stale
/// relative to `now_unix` and `ttl`.
fn arena_path_is_stale(
&self,
path: &Path,
now_unix: u64,
ttl: u64,
) -> bool {
let Some(arena) = &self.arena else {
return false;
};
let root = arena.root_path();
let Ok(rel) = path.strip_prefix(root) else {
return false;
};
let parts: Vec<String> = rel
.components()
.map(|c| c.as_os_str().to_string_lossy().into_owned())
.collect();
let Some(mut id) =
arena.find_relative(parts.iter().map(String::as_str))
else {
return false;
};
// A leaf inherits its containing directory's freshness.
if !arena.node(id).is_dir()
&& let Some(parent) = arena.node(id).parent
{
id = parent;
}
let node = arena.node(id);
match node.status() {
SubtreeStatus::Stale { .. } | SubtreeStatus::Unscanned => true,
SubtreeStatus::Fresh { scanned_at } => {
scanned_at > 0 && now_unix.saturating_sub(scanned_at) > ttl
}
SubtreeStatus::Updating { .. } => false,
}
}
/// Resolve everything the mosaic needs for this frame.
///
/// Runs once per render, before the paint callbacks: painting then only has
@@ -3324,6 +3474,8 @@ impl Disktree {
// view, so the first level always reads as the first level.
let age = self.color_mode == ColorMode::Age;
let now = self.scanned_at;
let now_unix = disktree_scanner::unix_now();
let refresh_mode = self.refresh_mode();
let mut decorations = Vec::with_capacity(tiles.len());
let mut slots: Vec<LabelSlot<'_>> = Vec::new();
for tile in tiles {
@@ -3344,6 +3496,8 @@ impl Disktree {
Some(Some(Keep::Partial { .. })) => Filtered::Holds,
Some(None) => Filtered::Out,
};
let freshness =
self.tile_freshness_at(crumbs, now_unix, refresh_mode);
let is_marked = marked.contains(crumbs);
// Everything inside a marked directory goes with it, so it is
// drawn marked too.
@@ -3358,6 +3512,7 @@ impl Disktree {
age_bucket,
reclaimable: node.is_some_and(|n| n.reclaim.is_some()),
filtered,
freshness,
unreadable: node.is_some_and(|n| n.read_error),
marked: is_marked,
covered: is_covered,
+83
View File
@@ -2557,3 +2557,86 @@ fn selection_section_keeps_static_height_across_hover_and_marks(
"height must not jump on covered file"
);
}
#[gpui_kit::test]
fn tiles_distinguish_fresh_stale_scheduled_and_updating_states(
cx: &mut TestAppContext,
) {
use crate::treemap_view::TileFreshness;
use disktree_scanner::SubtreeStatus;
cx.update(gpui_omarchy::init);
let temp = fixture();
let (view, cx) = view_over(temp.path(), cx);
let junk = temp.path().join("junk");
let keep = temp.path().join("keep");
let (junk_crumbs, keep_crumbs) = read(&view, cx, |app| {
(
app.crumbs_for_path(&junk).unwrap(),
app.crumbs_for_path(&keep).unwrap(),
)
});
// 1. Mark `junk` stale in the arena while `keep` stays fresh.
update(&view, cx, |app, _| {
let mut arena = (**app.arena.as_ref().unwrap()).clone();
let now = disktree_scanner::unix_now();
let junk_id = arena.find_relative(["junk"]).unwrap();
arena
.node_mut(junk_id)
.set_status(SubtreeStatus::Stale { scanned_at: 100 });
let keep_id = arena.find_relative(["keep"]).unwrap();
arena
.node_mut(keep_id)
.set_status(SubtreeStatus::Fresh { scanned_at: now });
app.arena = Some(std::sync::Arc::new(arena));
app.freshness = Some(disktree_scanner::Freshness::Stale {
oldest_at: 100,
newest_at: now,
stale_dirs: 1,
});
});
draw(cx);
read(&view, cx, |app| {
assert_eq!(app.tile_freshness(&junk_crumbs), TileFreshness::Stale);
assert_eq!(app.tile_freshness(&keep_crumbs), TileFreshness::Fresh);
});
// 2. Start an SWR background refresh: stale `junk` becomes Scheduled while
// fresh `keep` stays Fresh; active `updating_paths` flips to Updating.
update(&view, cx, |app, _| {
app.active_scan = Some(ActiveScan::Local {
handle: disktree_core::scan::ScanHandle::spawn(
temp.path().into(),
options(),
),
intent: ScanIntent::Refresh {
metric: app.options.metric,
forced: false,
},
});
});
draw(cx);
read(&view, cx, |app| {
assert_eq!(app.tile_freshness(&junk_crumbs), TileFreshness::Scheduled);
assert_eq!(app.tile_freshness(&keep_crumbs), TileFreshness::Fresh);
});
update(&view, cx, |app, _| {
app.updating_paths.insert(junk.clone());
});
draw(cx);
read(&view, cx, |app| {
assert_eq!(app.tile_freshness(&junk_crumbs), TileFreshness::Updating);
});
// 3. Once `junk` finishes refreshing, it returns to Fresh immediately.
update(&view, cx, |app, _| {
app.updating_paths.remove(&junk);
app.refreshed_paths.insert(junk.clone());
});
draw(cx);
read(&view, cx, |app| {
assert_eq!(app.tile_freshness(&junk_crumbs), TileFreshness::Fresh);
});
}
+96 -10
View File
@@ -27,6 +27,24 @@ use crate::appearance;
use crate::palette;
use crate::state::{Disktree, Filtered, Label, View};
/// Freshness & refresh-queue state of a tile, painted independently of
/// category colour and reclaimable hatching.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TileFreshness {
/// Freshly scanned or within the cache TTL.
#[default]
Fresh,
/// Cached older than TTL and not currently scheduled for refresh:
/// gently washed toward the inset surface with a muted corner dot.
Stale,
/// Queued in an active background refresh / SWR pass: lightly veiled
/// with a dashed accent border.
Scheduled,
/// Actively being scanned right now (current worker or prioritized):
/// subtle accent wash with a solid accent border.
Updating,
}
/// How one tile should be drawn, resolved before the paint callback runs so
/// that painting never has to look anything up.
#[derive(Clone, Debug)]
@@ -43,6 +61,8 @@ pub struct TileDeco {
pub reclaimable: bool,
/// How it stands against the find text.
pub filtered: Filtered,
/// Cache freshness / background refresh queue state.
pub freshness: TileFreshness,
/// Part of it could not be read: flagged in its corner.
pub unreadable: bool,
pub marked: bool,
@@ -120,6 +140,7 @@ pub fn preview(
age_bucket: None,
reclaimable: false,
filtered: Filtered::Shown,
freshness: TileFreshness::Fresh,
unreadable: false,
marked: false,
covered: false,
@@ -218,6 +239,8 @@ struct Colors {
selected_border: Hsla,
marked_border: Hsla,
marked_label: Hsla,
accent: Hsla,
stale_pip: Hsla,
warning: Hsla,
hatch: Hsla,
/// Fills per category, then per depth.
@@ -269,6 +292,8 @@ impl Colors {
selected_border: palette::highlight(theme),
marked_border: theme.danger,
marked_label: theme.danger,
accent: theme.accent,
stale_pip: theme.secondary.opacity(0.65),
warning: theme.warning,
hatch: palette::hatch(theme),
fill: CATEGORIES
@@ -303,6 +328,15 @@ impl Colors {
Some(bucket) => self.age[bucket.min(self.age.len() - 1)][depth],
None => self.fill[category_index(tile.category)][depth],
};
// Freshness shading sits beneath find-filtering so stale folders read
// as softly washed out, queued folders lightly veiled, and actively
// refreshing folders softly tinted with the accent.
let fill = match tile.freshness {
TileFreshness::Fresh => fill,
TileFreshness::Stale => palette::mix(fill, self.inset, 0.44),
TileFreshness::Scheduled => palette::mix(fill, self.inset, 0.22),
TileFreshness::Updating => palette::mix(fill, self.accent, 0.14),
};
// Only what matches keeps its colour; a directory holding matches
// steps back less, so the way to them stays readable.
match tile.filtered {
@@ -326,11 +360,18 @@ fn paint_tiles(
) {
let none = Edges::all(px(0.));
let solid = gpui_kit::BorderStyle::Solid;
let dashed = gpui_kit::BorderStyle::Dashed;
let scale = window.scale_factor();
// Outlines are drawn after every fill: a directory's children paint over
// its body, and would otherwise cover its selection ring, leaving only
// slivers of it showing in the gaps between them.
let mut outlines: Vec<(u8, Bounds<Pixels>, f32, Hsla)> = Vec::new();
let mut outlines: Vec<(
u8,
Bounds<Pixels>,
f32,
Hsla,
gpui_kit::BorderStyle,
)> = Vec::new();
for tile in tiles {
let rect = view.project(tile.rect);
if rect.w <= 0.5 || rect.h <= 0.5 {
@@ -369,7 +410,9 @@ fn paint_tiles(
}
// A top-level directory carries a thin strip of its colour, so the
// first level of structure reads before any detail.
// first level of structure reads before any detail. Freshness modulates
// the strip: stale dims it, scheduled tints it softly with the accent,
// and actively updating lights it up in the accent.
if tile.depth == 0
&& tile.age_bucket.is_none()
&& tile.filtered != Filtered::Out
@@ -381,10 +424,17 @@ fn paint_tiles(
px(2.0_f32.min(quad_bounds.size.height.as_f32())),
),
);
let base = colors.strip[category_index(tile.category)];
let strip_color = match tile.freshness {
TileFreshness::Fresh => base,
TileFreshness::Stale => base.opacity(0.32),
TileFreshness::Scheduled => colors.accent.opacity(0.50),
TileFreshness::Updating => colors.accent,
};
window.paint_quad(quad(
strip,
Corners::default(),
colors.strip[category_index(tile.category)],
strip_color,
none,
colors.hover_border,
solid,
@@ -408,31 +458,67 @@ fn paint_tiles(
colors.warning,
solid,
));
} else if tile.freshness == TileFreshness::Stale
&& tile.depth <= 1
&& rect.w > 18.0
&& rect.h > 14.0
{
// A quiet muted corner dot on stale cached directories so even
// low-chroma categories visibly read as stale.
let mark = Bounds::new(
Point::new(
quad_bounds.origin.x + quad_bounds.size.width - px(6.),
quad_bounds.origin.y + px(4.),
),
Size::new(px(3.), px(3.)),
);
window.paint_quad(quad(
mark,
Corners::default(),
colors.stale_pip,
none,
colors.stale_pip,
solid,
));
}
// Ranked so the most important ring is painted last, on top.
// Scheduled refresh tiles get a dashed accent border (rank 0) and
// actively updating tiles get a solid accent border (rank 1), always
// yielding to marked/hover/selected rings.
let outline = if tile.selected {
Some((3, 2.0, colors.selected_border))
Some((4, 2.0, colors.selected_border, solid))
} else if tile.hovered {
Some((2, 1.0, colors.hover_border))
Some((3, 1.0, colors.hover_border, solid))
} else if tile.marked {
Some((1, 2.0, colors.marked_border))
Some((2, 2.0, colors.marked_border, solid))
} else if tile.freshness == TileFreshness::Updating
&& rect.w > 14.0
&& rect.h > 12.0
{
Some((1, 2.0, colors.accent, solid))
} else if tile.freshness == TileFreshness::Scheduled
&& tile.depth <= 1
&& rect.w > 18.0
&& rect.h > 14.0
{
Some((0, 1.0, colors.accent.opacity(0.70), dashed))
} else {
None
};
if let Some((rank, width, color)) = outline {
outlines.push((rank, quad_bounds, width, color));
if let Some((rank, width, color, style)) = outline {
outlines.push((rank, quad_bounds, width, color, style));
}
}
outlines.sort_by_key(|(rank, ..)| *rank);
for (_, ring, width, color) in outlines {
for (_, ring, width, color, style) in outlines {
window.paint_quad(quad(
ring,
Corners::default(),
gpui_kit::transparent_black(),
Edges::all(px(width)),
color,
solid,
style,
));
}
}
+57 -1
View File
@@ -1694,6 +1694,31 @@ fn legend(app: &Disktree, theme: &Theme, cx: &App) -> Div {
}
let ground = palette::category_fill(theme, Category::Other, 0);
let hatch = cx.omarchy().bright.opacity(0.5);
let show_queued = app.is_refreshing();
let show_stale = matches!(
app.freshness,
Some(disktree_scanner::Freshness::Stale { .. })
);
let stale_swatch = div()
.relative()
.flex_shrink_0()
.size(size::SWATCH)
.bg(palette::mix(ground, theme.inset, 0.44))
.child(
div()
.absolute()
.top(px(1.))
.right(px(1.))
.size(px(3.))
.bg(theme.secondary.opacity(0.75)),
);
let queued_swatch = div()
.flex_shrink_0()
.size(size::SWATCH)
.bg(palette::mix(ground, theme.inset, 0.22))
.border_1()
.border_dashed()
.border_color(theme.accent.opacity(0.8));
div()
.flex()
.flex_row()
@@ -1702,6 +1727,8 @@ fn legend(app: &Disktree, theme: &Theme, cx: &App) -> Div {
.min_w_0()
.overflow_hidden()
.child(item(widgets::hatch_swatch(hatch, ground), "Reclaimable"))
.when(show_stale, |row| row.child(item(stale_swatch, "Stale")))
.when(show_queued, |row| row.child(item(queued_swatch, "Queued")))
.child(lane)
}
@@ -1815,7 +1842,7 @@ fn selection_section(
);
let ancestor = path.as_deref().and_then(|path| app.marked_ancestor(path));
let mark_crumbs = target;
let mark_crumbs = target.clone();
let unmark_mode = marked || covered_by.is_some();
let mark_tip = match &ancestor {
Some(ancestor) if !marked => {
@@ -1982,6 +2009,18 @@ fn selection_section(
);
let mut chips = Vec::new();
match app.tile_freshness(&target) {
treemap_view::TileFreshness::Fresh => {}
treemap_view::TileFreshness::Stale => {
chips.push(widgets::chip("Stale", theme.secondary, cx));
}
treemap_view::TileFreshness::Scheduled => {
chips.push(widgets::chip("Queued", theme.accent, cx));
}
treemap_view::TileFreshness::Updating => {
chips.push(widgets::chip("Refreshing…", theme.accent, cx));
}
}
if marked {
chips.push(widgets::chip("Marked", theme.danger, cx));
} else if let Some(ancestor) = &covered_by {
@@ -4092,6 +4131,23 @@ fn node_card(
);
let mut badges = div().flex().flex_row().gap(space::XS).flex_wrap();
match app.tile_freshness(crumbs) {
treemap_view::TileFreshness::Fresh => {}
treemap_view::TileFreshness::Stale => {
badges = badges.child(widgets::chip("Stale", theme.secondary, cx));
}
treemap_view::TileFreshness::Scheduled => {
badges = badges.child(widgets::chip(
"Queued for refresh",
theme.accent,
cx,
));
}
treemap_view::TileFreshness::Updating => {
badges =
badges.child(widgets::chip("Refreshing…", theme.accent, cx));
}
}
if hidden {
badges = badges.child(widgets::chip("Hidden", theme.secondary, cx));
}
+18 -4
View File
@@ -211,6 +211,8 @@ pub enum ScanUpdate {
node: Node,
freshness: Freshness,
},
/// A directory has started actively walking during a refresh.
SubtreeUpdating { path: PathBuf },
/// A prioritized subtree finished scanning while background revalidation
/// continues on the rest of the root.
SubtreeRefreshed {
@@ -399,6 +401,16 @@ impl ScanHandle {
}
}
/// Most recently started directory path in the background walk, if any.
pub fn active_path(&self) -> Option<PathBuf> {
self.progress
.walk
.active_path
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clone()
}
/// Drain the next incremental update from an SWR session or one-shot walk.
pub fn poll_update(&self) -> Option<ScanUpdate> {
match &self.backend {
@@ -445,6 +457,9 @@ impl ScanHandle {
self.progress.finish();
return Some(ScanUpdate::Finished);
}
Ok(ScanEvent::SubtreeUpdating { path, .. }) => {
return Some(ScanUpdate::SubtreeUpdating { path });
}
Ok(ScanEvent::SubtreeRefreshed { path, arena, .. }) => {
return Some(ScanUpdate::SubtreeRefreshed {
path,
@@ -453,7 +468,6 @@ impl ScanHandle {
}
Ok(
ScanEvent::Progress { .. }
| ScanEvent::SubtreeUpdating { .. }
| ScanEvent::StorageFallback { .. },
) => {}
Err(std::sync::mpsc::TryRecvError::Empty) => return None,
@@ -496,9 +510,9 @@ impl ScanHandle {
freshness: Freshness::Fresh { .. },
..
} => Some(Ok(node)),
ScanUpdate::Cached { .. } | ScanUpdate::SubtreeRefreshed { .. } => {
None
}
ScanUpdate::Cached { .. }
| ScanUpdate::SubtreeUpdating { .. }
| ScanUpdate::SubtreeRefreshed { .. } => None,
ScanUpdate::Error(err) => Some(Err(err)),
ScanUpdate::Finished => Some(Err(io::Error::other(
"the scan finished without producing a tree",