Skip to content
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ rescan, ⌘[ and ⌘] for back and forward, and ⌘Q, ⌘H and ⌘W (closing the
window quits); other ⌘ chords are left to the system. On Linux and Windows
the same work with ctrl, with F5 to rescan too.

Every hint in the bar along the bottom is also a button for its key, on the
review screen as well; `hjkl` is four keys and stays a label.

On the review screen: `m` trash, `p` permanent, `!` unmark all, `enter`
commits, `esc` goes back. Or hand the list on instead of acting on it: `s`
saves it as a text file, one path per line, and `a` copies a prompt for a
Expand Down
39 changes: 37 additions & 2 deletions crates/disktree-app/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ pub struct History {
/// through, and small enough that pruning after a scan costs nothing.
const HISTORY_DEPTH: usize = 100;

/// The most levels drawn at once. The depth hint's click wraps here, so
/// it has to be the same number the keys stop at.
const DEPTH_LIMIT: u32 = 6;

/// Result of the removal run, summarised for the final screen.
#[derive(Clone, Debug, Default)]
pub struct RunSummary {
Expand Down Expand Up @@ -491,7 +495,7 @@ impl Disktree {
view: View::default(),
transition: None,
layout_options: LayoutOptions {
max_depth: depth.clamp(1, 6),
max_depth: depth.clamp(1, DEPTH_LIMIT),
..LayoutOptions::default()
},
cache: None,
Expand Down Expand Up @@ -2048,7 +2052,7 @@ impl Disktree {
/// a treemap: seeing further in without changing what is on screen.
pub fn adjust_depth(&mut self, step: i32, cx: &mut Context<'_, Self>) {
let depth = (self.layout_options.max_depth.cast_signed() + step)
.clamp(1, 6) as u32;
.clamp(1, DEPTH_LIMIT.cast_signed()) as u32;
self.layout_options.max_depth = depth;
self.cache = None;
cx.notify();
Expand Down Expand Up @@ -2634,6 +2638,37 @@ impl Disktree {
}
}

/// A click on a key hint does what the key would.
pub fn press(&mut self, key: &str, cx: &mut Context<'_, Self>) {
// What owns the keyboard owns it against a click behind it, too.
if self.confirm_open || self.volumes_open {
return;
}
if key == "?" {
self.show_help = !self.show_help;
cx.notify();
return;
}
if self.show_help {
return;
}
match self.screen {
// The hint names both keys and a click can only be one: it
// draws one level more, and past the deepest starts over.
Screen::Explore if key == "[ ]" => {
let step = if self.layout_options.max_depth >= DEPTH_LIMIT {
1 - DEPTH_LIMIT.cast_signed()
} else {
1
};
self.adjust_depth(step, cx);
}
Screen::Explore => self.on_explore_key(key, false, false, cx),
Screen::Review => self.on_review_key(key, cx),
Screen::Running | Screen::Done => {}
}
}

fn on_explore_key(
&mut self,
key: &str,
Expand Down
50 changes: 50 additions & 0 deletions crates/disktree-app/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,3 +1844,53 @@ fn the_power_menu_offers_only_what_the_cpus_can_tell_apart(
draw(cx);
assert!(read(&view, cx, |app| app.power_menu.is_none()));
}

#[gpui_kit::test]
fn a_click_on_a_key_hint_does_what_the_key_does(cx: &mut TestAppContext) {
cx.update(gpui_omarchy::init);
let temp = fixture();
let (view, cx) = view_over(temp.path(), cx);
draw(cx);

let click = |cx: &mut Window, selector: &'static str| {
let hint = cx.debug_bounds(selector).expect("the hint is drawn");
cx.simulate_click(hint.center(), gpui_kit::Modifiers::none());
draw(cx);
};

// `]` draws one level more; past the deepest the hint starts over.
assert_eq!(read(&view, cx, |app| app.layout_options.max_depth), 3);
click(cx, "key-depth");
assert_eq!(read(&view, cx, |app| app.layout_options.max_depth), 4);
update(&view, cx, |app, cx| app.adjust_depth(2, cx));
draw(cx);
click(cx, "key-depth");
assert_eq!(read(&view, cx, |app| app.layout_options.max_depth), 1);

// `?` opens the key list, and the keyboard still closes it: the click
// must not have left the keys with the hint.
click(cx, "key-all keys");
assert!(read(&view, cx, |app| app.show_help));
press(cx, "escape");
assert!(!read(&view, cx, |app| app.show_help));

// `v` opens the picker, which takes the keyboard for itself.
click(cx, "key-volumes");
assert!(read(&view, cx, |app| app.volumes_open));
press(cx, "escape");
assert!(!read(&view, cx, |app| app.volumes_open));

// Moving is four keys and no one click, so that hint stays a label.
assert!(cx.debug_bounds("key-move").is_none());

// `space` marks the selection, as the key does.
assert!(read(&view, cx, |app| app.marks.is_empty()));
click(cx, "key-mark");
assert_eq!(read(&view, cx, |app| app.marks.len()), 1);

// And on the review screen the footer's hints are buttons too.
click(cx, "key-review");
assert_eq!(read(&view, cx, |app| app.screen), Screen::Review);
click(cx, "key-back");
assert_eq!(read(&view, cx, |app| app.screen), Screen::Explore);
}
97 changes: 65 additions & 32 deletions crates/disktree-app/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2208,20 +2208,23 @@ fn review_button(

/// The keys, quietly: outlines and light labels, there when needed. The key
/// to every other key and the scan's own numbers hold the trailing edge.
fn key_bar(app: &Disktree, theme: &Theme, cx: &App) -> Div {
// Most useful first, so a narrow window clips the least useful.
let hints: [(&str, &str); 11] = [
("space", "mark"),
("enter", "open"),
("\u{232b}", "up"),
("c", "review"),
("hjkl", "move"),
("/", "filter"),
("[ ]", "depth"),
("t", "mode"),
("0", "reset"),
("v", "volumes"),
("r", "rescan"),
fn key_bar(app: &Disktree, theme: &Theme, cx: &Context<'_, Disktree>) -> Div {
// Most useful first, so a narrow window clips the least useful. Each
// is a button for the key it names, for a hand already on the mouse;
// the last column is that key as the bindings spell it. Moving between
// tiles is four keys and no one click, so it stays a label.
let hints: [(&str, &str, Option<&str>); 11] = [
("space", "mark", Some("space")),
("enter", "open", Some("enter")),
("\u{232b}", "up", Some("backspace")),
("c", "review", Some("c")),
("hjkl", "move", None),
("/", "filter", Some("/")),
("[ ]", "depth", Some("[ ]")),
("t", "mode", Some("t")),
("0", "reset", Some("0")),
("v", "volumes", Some("v")),
("r", "rescan", Some("r")),
];
let mut lane = div()
.flex()
Expand All @@ -2231,8 +2234,13 @@ fn key_bar(app: &Disktree, theme: &Theme, cx: &App) -> Div {
.flex_1()
.min_w_0()
.overflow_hidden();
for (keys, label) in hints {
lane = lane.child(widgets::hint(keys, label, cx).flex_shrink_0());
for (keys, label, key) in hints {
lane = lane.child(match key {
Some(key) => key_hint(keys, label, key, cx).into_any_element(),
None => widgets::hint(keys, label, cx)
.flex_shrink_0()
.into_any_element(),
});
}

let mut row = div()
Expand Down Expand Up @@ -2274,14 +2282,35 @@ fn key_bar(app: &Disktree, theme: &Theme, cx: &App) -> Div {
widgets::human_count(app.progress.files)
)
};
row.child(widgets::hint("?", "all keys", cx).flex_shrink_0())
.child(
div()
.flex_shrink_0()
.text_size(text::CAPTION)
.text_color(theme.secondary.opacity(0.7))
.child(scan),
)
row.child(key_hint("?", "all keys", "?", cx)).child(
div()
.flex_shrink_0()
.text_size(text::CAPTION)
.text_color(theme.secondary.opacity(0.7))
.child(scan),
)
}

/// A key hint that is also a button: a click does what `key` does, and the
/// keyboard goes back to where that leaves it.
fn key_hint(
keys: &'static str,
label: &'static str,
key: &'static str,
cx: &Context<'_, Disktree>,
) -> Stateful<Div> {
let id = format!("key-{label}");
widgets::hint_button(ElementId::Name(id.clone().into()), keys, label, cx)
.debug_selector(move || id)
.on_click(cx.listener(move |this, _, window, cx| {
this.press(key, cx);
// A dialog the key opened takes the keyboard; anything else
// hands it back to the treemap, which a click took it from.
if this.focus_request.is_none() {
window.focus(&this.focus, cx);
}
this.apply_focus(window, cx);
}))
}

/// What the viewport shows while the first scan is running.
Expand Down Expand Up @@ -2934,7 +2963,11 @@ fn commit_controls(
)
}

fn review_footer(app: &Disktree, theme: &Theme, cx: &App) -> Div {
fn review_footer(
app: &Disktree,
theme: &Theme,
cx: &Context<'_, Disktree>,
) -> Div {
let commit = match app.removal_mode {
RemovalMode::Trash => "move to trash",
RemovalMode::Permanent => "delete\u{2026}",
Expand All @@ -2948,13 +2981,13 @@ fn review_footer(app: &Disktree, theme: &Theme, cx: &App) -> Div {
.py(space::SM)
.border_t_1()
.border_color(theme.divider())
.child(widgets::hint("enter", commit, cx))
.child(widgets::hint("m", "trash", cx))
.child(widgets::hint("p", "permanent", cx))
.child(widgets::hint("!", "unmark all", cx))
.child(widgets::hint("s", "save list", cx))
.child(widgets::hint("a", "copy as prompt", cx))
.child(widgets::hint("esc", "back", cx))
.child(key_hint("enter", commit, "enter", cx))
.child(key_hint("m", "trash", "m", cx))
.child(key_hint("p", "permanent", "p", cx))
.child(key_hint("!", "unmark all", "!", cx))
.child(key_hint("s", "save list", "s", cx))
.child(key_hint("a", "copy as prompt", "a", cx))
.child(key_hint("esc", "back", "escape", cx))
.child(div().flex_1())
.child(
div()
Expand Down
27 changes: 26 additions & 1 deletion crates/disktree-app/src/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use disktree_core::tree::{Metric, Node};
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::{
App, Div, ElementId, FontWeight, Hsla, InteractiveElement as _,
ParentElement, SharedString, Styled, div, relative,
ParentElement, SharedString, StatefulInteractiveElement as _, Styled, div,
relative,
};
use gpui_omarchy::{ActiveTheme, Status};

Expand Down Expand Up @@ -248,6 +249,30 @@ pub fn hint(
)
}

/// A hint that can be clicked: the same pair, with the hover and the
/// pointer of something that does what its key does.
pub fn hint_button(
id: impl Into<ElementId>,
keys: impl Into<SharedString>,
label: impl Into<SharedString>,
cx: &App,
) -> gpui_kit::Stateful<Div> {
let theme = cx.omarchy();
hint(keys, label, cx)
.id(id)
.flex_shrink_0()
// Framed and padded evenly, so the key and its word sit in the
// middle of something that reads as pressable.
.justify_center()
.px(space::SM)
.py(space::XXS)
.border_1()
.border_color(theme.control_border())
.cursor_pointer()
.hover(|style| style.bg(theme.hover_fill()))
.active(|style| style.bg(theme.pressed_fill()))
}

/// A section heading inside a panel.
pub fn section(label: impl Into<SharedString>, cx: &App) -> Div {
let theme = cx.omarchy();
Expand Down