diff --git a/crates/disktree-app/src/state.rs b/crates/disktree-app/src/state.rs index 6191e3e..46195f2 100644 --- a/crates/disktree-app/src/state.rs +++ b/crates/disktree-app/src/state.rs @@ -433,6 +433,8 @@ pub struct Disktree { /// on. `None` when closed; empty when no volume besides the current one /// could be read. pub volumes_open: bool, + pub volumes_loading: bool, + volume_epoch: u64, pub volumes: Vec, pub volume_highlight: usize, /// The side panel's width, in rem; dragged from its left edge. @@ -526,6 +528,8 @@ impl Disktree { restarting: false, disk_root: None, volumes_open: false, + volumes_loading: false, + volume_epoch: 0, volumes: Vec::new(), volume_highlight: 0, panel_rems: PANEL_REMS, @@ -632,28 +636,58 @@ impl Disktree { /// is left alone until a volume is picked; picking calls [`set_root`], /// which cancels it. pub fn open_volumes(&mut self, cx: &mut Context<'_, Self>) { - let mut volumes = disktree_core::space::volumes(); - // The current disk is always offered, even when the table misses it: - // it is the one row that is never wrong. - if let Some(root) = volume_root_for(&self.root_path) - && !volumes.iter().any(|volume| volume.point == root) - { - volumes.push(disktree_core::space::Volume { - point: root.clone(), - device: device_for(&root), - space: space_info(&root).ok(), - }); - } - volumes.retain(|volume| volume.point != self.root_path); + self.volume_epoch += 1; + let epoch = self.volume_epoch; + let root_path = self.root_path.clone(); + self.volumes.clear(); self.volume_highlight = 0; - self.volumes = volumes; + self.volumes_loading = true; self.volumes_open = true; + self.focus_request = Some(FocusTarget::Dialog); + // Removable media and network volumes can take seconds to answer. + // Keep Escape and the window responsive while probing them. + let task = cx.background_executor().spawn(async move { + let mut volumes = disktree_core::space::volumes(); + if let Some(root) = volume_root_for(&root_path) + && !volumes.iter().any(|volume| volume.point == root) + { + volumes.push(disktree_core::space::Volume { + point: root.clone(), + device: device_for(&root), + space: space_info(&root).ok(), + }); + } + volumes.retain(|volume| volume.point != root_path); + volumes + }); + cx.spawn(async move |this, cx| { + let volumes = task.await; + let _ = this.update(cx, |this, cx| { + if this.volumes_open && this.volume_epoch == epoch { + this.volumes = volumes; + this.volumes_loading = false; + cx.notify(); + } + }); + }) + .detach(); + cx.notify(); + } + + /// Close the picker and return the keyboard to the treemap. + pub fn close_volumes(&mut self, cx: &mut Context<'_, Self>) { + self.volumes_open = false; + self.focus_request = Some(FocusTarget::Root); cx.notify(); } /// Scan the highlighted volume from scratch; closes the picker when there /// is nothing to pick. pub fn choose_volume(&mut self, cx: &mut Context<'_, Self>) { + if self.volumes_loading { + return; + } + self.focus_request = Some(FocusTarget::Root); let Some(point) = self .volumes .get(self.volume_highlight) @@ -2244,7 +2278,9 @@ impl Disktree { /// behind the confirmation, and not under the review list or a removal /// that is still running. pub fn can_start_over(&self) -> bool { - self.screen == Screen::Explore && !self.confirm_open + self.screen == Screen::Explore + && !self.confirm_open + && !self.volumes_open } /// Ask for a directory and scan it: an app opened from the Dock has no @@ -2389,10 +2425,7 @@ impl Disktree { // picks, Escape closes, and nothing behind it acts. if self.volumes_open { match key { - "escape" => { - self.volumes_open = false; - cx.notify(); - } + "escape" => self.close_volumes(cx), "enter" => self.choose_volume(cx), "up" | "k" if !control => { self.move_volume_highlight(-1, cx); diff --git a/crates/disktree-app/src/tests.rs b/crates/disktree-app/src/tests.rs index 21926e5..2eb15d2 100644 --- a/crates/disktree-app/src/tests.rs +++ b/crates/disktree-app/src/tests.rs @@ -488,6 +488,35 @@ fn the_volume_picker_opens_moves_and_closes(cx: &mut TestAppContext) { assert_eq!(read(&view, cx, |app| app.root_path.clone()), before); } +/// Enter must reach the picker even when its dialog owns keyboard focus. +#[gpui_kit::test] +fn enter_in_the_focused_volume_picker_scans_the_selected_root( + cx: &mut TestAppContext, +) { + cx.update(gpui_omarchy::init); + let temp = fixture(); + let target = temp.path().join("junk"); + let (view, cx) = view_over(temp.path(), cx); + update(&view, cx, |app, _| { + app.volumes = vec![disktree_core::space::Volume { + point: target.clone(), + device: None, + space: None, + }]; + app.volume_highlight = 0; + app.volumes_open = true; + }); + let focus = read(&view, cx, |app| app.confirm_focus.clone()); + cx.update(|window, cx| window.focus(&focus, cx)); + draw(cx); + assert!(!read(&view, cx, Disktree::can_start_over)); + press(cx, "enter"); + assert!(!read(&view, cx, |app| app.volumes_open)); + assert_eq!(read(&view, cx, |app| app.root_path.clone()), target); + finish_scan(&view, cx); + assert!(read(&view, cx, |app| app.tree().is_some())); +} + #[gpui_kit::test] fn the_volume_picker_is_centred_and_still_dismisses_from_outside( cx: &mut TestAppContext, diff --git a/crates/disktree-app/src/views.rs b/crates/disktree-app/src/views.rs index 2cb6ec7..b3b4b61 100644 --- a/crates/disktree-app/src/views.rs +++ b/crates/disktree-app/src/views.rs @@ -134,7 +134,9 @@ fn volumes_dialog( .flex() .flex_col() .gap(space::XS); - if app.volumes.is_empty() { + if app.volumes_loading { + rows = rows.child(dialog_description("Looking for volumes…", cx)); + } else if app.volumes.is_empty() { rows = rows .child(dialog_description("No other volume could be read.", cx)); } @@ -185,18 +187,24 @@ fn volumes_dialog( let close = cancel.clone(); move |_, window, cx| { let _ = close.update(cx, |this, cx| { - this.volumes_open = false; - cx.notify(); + this.close_volumes(cx); this.apply_focus(window, cx); }); } }); + let accept = cx.entity().downgrade(); alert_dialog(&app.confirm_focus, cx) .open(true) + .on_ok(move |_, window, cx| { + let _ = accept.update(cx, |this, cx| { + this.choose_volume(cx); + this.apply_focus(window, cx); + }); + false + }) .on_cancel(move |_, window, cx| { let _ = cancel.update(cx, |this, cx| { - this.volumes_open = false; - cx.notify(); + this.close_volumes(cx); this.apply_focus(window, cx); }); false @@ -226,7 +234,10 @@ fn centred_popup( .justify_center() // Any button, as the backdrop took them: a right-click outside closes // the dialog too. - .on_any_mouse_down(on_outside) + .on_any_mouse_down(move |event, window, cx| { + cx.stop_propagation(); + on_outside(event, window, cx); + }) .child( div() .id("dialog-card") @@ -1813,7 +1824,7 @@ fn disk_section( .tab_stop(false) .on_click(cx.listener(|this, _, window, cx| { this.open_volumes(cx); - window.focus(&this.focus, cx); + this.apply_focus(window, cx); })), ); let mut section = div().flex().flex_col().gap(space::SM).child(header); @@ -3248,7 +3259,7 @@ fn help_overlay(app: &Disktree, cx: &gpui_kit::App) -> Div { let theme = cx.omarchy(); // Sentence case, and the tile a key acts on is always the one under the // pointer if the pointer moved last, else the keyboard selection. - let rows: [(&str, &str); 28] = [ + let rows = [ ("space / x", "Mark or unmark the tile you point at"), (MODIFIER_CLICK, "Mark without moving the selection"), ("enter", "Open that directory, at any depth"), diff --git a/crates/disktree-core/src/space.rs b/crates/disktree-core/src/space.rs index 91dade7..7fab726 100644 --- a/crates/disktree-core/src/space.rs +++ b/crates/disktree-core/src/space.rs @@ -343,16 +343,6 @@ pub struct Volume { pub space: Option, } -impl Volume { - /// Free bytes for ranking; an unreadable volume sorts last. - const fn available(&self) -> u64 { - match &self.space { - Some(space) => space.available, - None => 0, - } - } -} - /// Every volume worth offering as a scan root, fullest first. /// /// Pseudo filesystems (`/proc`, `/sys`, tmpfs, …), snapshot subvolumes and @@ -405,7 +395,13 @@ pub fn volumes_in(mounts: &[Mount]) -> Vec { /// Fullest first; an unreadable volume sorts last. fn sort_by_free_space(volumes: &mut [Volume]) { - volumes.sort_by_key(|volume| std::cmp::Reverse(volume.available())); + volumes.sort_by_key(|volume| { + ( + volume.space.is_none(), + volume.space.map_or(0, |space| space.available), + volume.point.clone(), + ) + }); } /// Whether the mount is a real disk worth scanning: a device-backed @@ -515,6 +511,31 @@ tmpfs /tmp tmpfs rw 0 0 portal /run/user/1000/doc fuse.portal rw 0 0 "; + #[test] + fn volumes_with_least_space_come_first_and_unknowns_last() { + let volume = |name: &str, available: Option| Volume { + point: PathBuf::from(name), + device: None, + space: available.map(|available| SpaceInfo { + total: 100, + free: available, + available, + }), + }; + let mut volumes = vec![ + volume("unknown", None), + volume("roomy", Some(90)), + volume("full", Some(0)), + volume("nearly-full", Some(5)), + ]; + sort_by_free_space(&mut volumes); + let names: Vec<_> = volumes.iter().map(|v| v.point.clone()).collect(); + assert_eq!( + names, + ["full", "nearly-full", "roomy", "unknown"].map(PathBuf::from) + ); + } + #[test] fn a_volume_includes_its_subvolumes_and_nothing_else() { let mounts = parse_mounts(OMARCHY);