diff --git a/crates/disktree-app/src/state.rs b/crates/disktree-app/src/state.rs index 25d605d..6a2212a 100644 --- a/crates/disktree-app/src/state.rs +++ b/crates/disktree-app/src/state.rs @@ -1251,6 +1251,12 @@ impl Disktree { self.toggle_mark(&crumbs, cx); } + /// Mark or unmark the node at `crumbs`. + /// + /// A mark covers everything beneath it, since removing a directory takes + /// its contents with it: marking a directory absorbs the marks already + /// inside it, and a path inside a marked directory cannot be marked or + /// kept on its own — it says which mark it goes with instead. pub fn toggle_mark( &mut self, crumbs: &[usize], @@ -1259,34 +1265,61 @@ impl Disktree { let Some(target) = self.target_at(crumbs) else { return; }; - let marked_now = self.marks.toggle(target); self.notice = None; - if !marked_now { - cx.notify(); - return; - } - // A mark inside something already marked is redundant, and saying so - // now is friendlier than showing it as redundant on the review screen. - let marked = self.marks.items().last().cloned(); - if let Some(marked) = marked - && let Some(parent) = self.marks.items().iter().find(|item| { - item.path != marked.path && marked.path.starts_with(&item.path) - }) + if !self.marks.contains(&target.path) + && let Some(ancestor) = self.marked_ancestor(&target.path) { - let parent = parent.path.clone(); - self.space_baseline = self.space_baseline.or(self.space); self.notice = Some(( format!( - "{} is already inside the marked {}", - display_path(&marked.path, self.home.as_deref()), - display_path(&parent, self.home.as_deref()) + "{} goes with the marked {}; unmark that to keep it", + display_path(&target.path, self.home.as_deref()), + display_path(&ancestor, self.home.as_deref()) ), Status::Warning, )); + cx.notify(); + return; + } + let path = target.path.clone(); + if self.marks.toggle(target) { + self.space_baseline = self.space_baseline.or(self.space); + let inside: Vec = self + .marks + .items() + .iter() + .filter(|item| { + item.path != path && item.path.starts_with(&path) + }) + .map(|item| item.path.clone()) + .collect(); + for inner in &inside { + self.marks.remove(inner); + } + if !inside.is_empty() { + self.notice = Some(( + format!( + "{} now covers {} mark{} inside it", + display_path(&path, self.home.as_deref()), + inside.len(), + if inside.len() == 1 { "" } else { "s" } + ), + Status::Neutral, + )); + } } cx.notify(); } + /// The marked directory `path` is inside, if any; never `path` itself. + pub fn marked_ancestor(&self, path: &Path) -> Option { + self.marks + .items() + .iter() + .filter(|item| item.path != path && path.starts_with(&item.path)) + .map(|item| item.path.clone()) + .min_by_key(|ancestor| ancestor.as_os_str().len()) + } + pub fn target_at(&self, crumbs: &[usize]) -> Option { let node = self.node_at(crumbs)?; let path = self.path_at(crumbs)?; @@ -1335,14 +1368,6 @@ impl Disktree { marked.insert(crumbs); } } - let mut covered: FxHashSet> = FxHashSet::default(); - for crumbs in &marked { - if (1..crumbs.len()) - .any(|length| marked.contains(&crumbs[..length])) - { - covered.insert(crumbs.clone()); - } - } let Some(tiles) = self.layout().map(<[Tile]>::to_vec) else { return Mosaic { @@ -1377,7 +1402,11 @@ impl Disktree { Some(None) => Filtered::Out, }; let is_marked = marked.contains(crumbs); - let is_covered = covered.contains(crumbs); + // Everything inside a marked directory goes with it, so it is + // drawn marked too. + let is_covered = !marked.is_empty() + && (1..crumbs.len()) + .any(|length| marked.contains(&crumbs[..length])); decorations.push(TileDeco { rect: self.animated_rect(tile.rect), depth: tile.depth, diff --git a/crates/disktree-app/src/tests.rs b/crates/disktree-app/src/tests.rs index 829d6aa..946b06a 100644 --- a/crates/disktree-app/src/tests.rs +++ b/crates/disktree-app/src/tests.rs @@ -1101,3 +1101,67 @@ fn a_crumb_lists_its_siblings_and_jumps_sideways(cx: &mut TestAppContext) { fn names_at(names: &[String], index: usize) -> String { names.get(index).cloned().unwrap_or_default() } + +#[gpui_kit::test] +fn marking_a_directory_marks_everything_inside_it(cx: &mut TestAppContext) { + cx.update(gpui_omarchy::init); + let temp = fixture(); + let (view, cx) = view_over(temp.path(), cx); + draw(cx); + let crumbs = |view: &Entity, cx: &mut Window, rel: &str| { + let path = temp.path().join(rel); + read(view, cx, |app| app.crumbs_for_path(&path)).expect(rel) + }; + let junk = crumbs(&view, cx, "junk"); + let deeper = crumbs(&view, cx, "junk/deeper"); + + // Marked inside first, then the directory around it: one mark remains, + // and it covers the inner one. + update(&view, cx, |app, cx| app.toggle_mark(&deeper, cx)); + update(&view, cx, |app, cx| app.toggle_mark(&junk, cx)); + let marks = read(&view, cx, |app| { + app.marks + .items() + .iter() + .map(|item| item.path.clone()) + .collect::>() + }); + assert_eq!( + marks, + vec![temp.path().join("junk")], + "the inner mark is absorbed" + ); + + // Every tile inside it is drawn marked: the ones below the mark are + // "covered", which paints the same fill and label. + let mosaic = update(&view, cx, |app, _| app.prepare()); + let (inside, covered) = update(&view, cx, |app, _| { + let tiles = app.layout().map(<[Tile]>::to_vec).unwrap_or_default(); + let inside: Vec = tiles + .iter() + .zip(&mosaic.tiles) + .filter(|(tile, _)| { + tile.crumbs().starts_with(&junk) && tile.crumbs() != junk + }) + .map(|(_, deco)| deco.covered) + .collect(); + (inside.len(), inside.iter().all(|covered| *covered)) + }); + assert!(inside >= 2, "blob.bin and deeper are drawn inside junk"); + assert!(covered, "every tile inside junk goes with it"); + + // Marking something inside it is refused, and says why. + update(&view, cx, |app, cx| app.toggle_mark(&deeper, cx)); + let (count, notice) = read(&view, cx, |app| { + ( + app.marks.len(), + app.notice.as_ref().map(|(text, _)| text.clone()), + ) + }); + assert_eq!(count, 1); + assert!(notice.is_some_and(|text| text.contains("goes with the marked"))); + + // Unmarking the directory unmarks everything. + update(&view, cx, |app, cx| app.toggle_mark(&junk, cx)); + assert!(read(&view, cx, |app| app.marks.is_empty())); +} diff --git a/crates/disktree-app/src/treemap_view.rs b/crates/disktree-app/src/treemap_view.rs index 7b5589b..4eaace1 100644 --- a/crates/disktree-app/src/treemap_view.rs +++ b/crates/disktree-app/src/treemap_view.rs @@ -151,7 +151,6 @@ struct Colors { selected_border: Hsla, marked_border: Hsla, marked_label: Hsla, - covered_border: Hsla, warning: Hsla, hatch: Hsla, /// Fills per category, then per depth. @@ -203,7 +202,6 @@ impl Colors { selected_border: palette::highlight(theme), marked_border: theme.danger, marked_label: theme.danger, - covered_border: theme.danger.opacity(0.4), warning: theme.warning, hatch: palette::hatch(theme), fill: CATEGORIES @@ -229,7 +227,8 @@ impl Colors { } fn fill(&self, tile: &TileDeco) -> Hsla { - if tile.marked { + // Marked, or inside something marked: it all goes together. + if tile.marked || tile.covered { return self.marked_fill; } let depth = (tile.depth as usize).min(DEPTHS - 1); @@ -287,7 +286,10 @@ fn paint_tiles( // "can it go", the colour "what is it". Everything inside a // reclaimable directory is reclaimable too, so only the outermost // one needs painting; its children repaint their own fill and hatch. - if tile.reclaimable && !tile.marked && tile.filtered == Filtered::Shown + if tile.reclaimable + && !tile.marked + && !tile.covered + && tile.filtered == Filtered::Shown { window.paint_quad(quad( quad_bounds, @@ -348,8 +350,6 @@ fn paint_tiles( Some((2, 1.0, colors.hover_border)) } else if tile.marked { Some((1, 2.0, colors.marked_border)) - } else if tile.covered { - Some((0, 1.0, colors.covered_border)) } else { None }; diff --git a/crates/disktree-app/src/views.rs b/crates/disktree-app/src/views.rs index f408684..ca7b193 100644 --- a/crates/disktree-app/src/views.rs +++ b/crates/disktree-app/src/views.rs @@ -1057,8 +1057,8 @@ fn selection_section( } if let Some(ancestor) = &covered_by { chips.push(widgets::chip( - format!("Inside marked {ancestor}"), - theme.secondary, + format!("Goes with {ancestor}"), + theme.danger, cx, )); } @@ -1091,23 +1091,34 @@ fn selection_section( })), ); } + // Inside a marked directory there is nothing to mark on its own: it + // goes with that directory, so the button offers to keep it by + // unmarking the directory instead. + let ancestor = + path.as_deref().and_then(|path| app.marked_ancestor(path)); let crumbs = target; - let mark = button( - "mark", - if marked { "Unmark" } else { "Mark for removal" }, - ButtonVariant::Primary, - cx, - ) - .tab_stop(false) - .flex_1() - .justify_center() - .on_click(cx.listener(move |this, _, window, cx| { - this.toggle_mark(&crumbs.clone(), cx); - window.focus(&this.focus, cx); - })); - actions = actions.child(if marked { - mark - } else { + let label = match &ancestor { + Some(ancestor) if !marked => { + format!("Unmark {}", short_name(ancestor)) + } + _ if marked => "Unmark".to_string(), + _ => "Mark for removal".to_string(), + }; + let mark = button("mark", label, ButtonVariant::Primary, cx) + .tab_stop(false) + .flex_1() + .justify_center() + .on_click(cx.listener(move |this, _, window, cx| { + if let Some(ancestor) = ancestor.as_deref().filter(|_| !marked) + { + this.unmark(ancestor, cx); + } else { + this.toggle_mark(&crumbs.clone(), cx); + } + window.focus(&this.focus, cx); + })); + let filled = !marked && covered_by.is_none(); + actions = actions.child(if filled { let on = palette::on_highlight(theme); mark.bg(highlight) .border_color(highlight) @@ -1116,6 +1127,8 @@ fn selection_section( .hover(move |style| { style.bg(highlight.opacity(0.85)).border_color(highlight) }) + } else { + mark }); }