Skip to content

A treemap for finding and removing what fills your disk, for Omarchy. Rust + GPUI.

Rust

app: centre alert dialog popups in the window

Abdulwahb Isam45e47e04d92d
diff --git a/crates/disktree-app/src/tests.rs b/crates/disktree-app/src/tests.rs
index 1b7f4db..38102b4 100644
--- a/crates/disktree-app/src/tests.rs
+++ b/crates/disktree-app/src/tests.rs
@@ -5,10 +5,11 @@
//! that panics while painting, a binding that never fires, a removal that
//! reports success without removing anything.
-use std::path::Path;
+use std::path::{Path, PathBuf};
use disktree_core::removal::RemovalMode;
use disktree_core::scan::{ScanOptions, scan};
+use disktree_core::space::{SpaceInfo, Volume};
use disktree_core::treemap::Tile;
use gpui_kit::{
Bounds, Context, Entity, Pixels, Point, TestAppContext, VisualTestContext,
@@ -487,6 +488,62 @@ fn the_volume_picker_opens_moves_and_closes(cx: &mut TestAppContext) {
assert_eq!(read(&view, cx, |app| app.root_path.clone()), before);
}
+#[gpui_kit::test]
+fn the_volume_picker_is_centred_and_still_dismisses_from_outside(
+ cx: &mut TestAppContext,
+) {
+ cx.update(gpui_omarchy::init);
+ let temp = fixture();
+ let (view, cx) = view_over(temp.path(), cx);
+ // Fixed rows, so this is about where the popup lands and not about which
+ // disks the machine running the test happens to have.
+ update(&view, cx, |app, cx| {
+ app.volumes = vec![
+ Volume {
+ point: PathBuf::from("/one"),
+ device: Some("/dev/one".into()),
+ space: Some(SpaceInfo {
+ total: 1_000,
+ free: 500,
+ available: 400,
+ }),
+ },
+ Volume {
+ point: PathBuf::from("/two"),
+ device: None,
+ space: None,
+ },
+ ];
+ app.volumes_open = true;
+ cx.notify();
+ });
+ draw(cx);
+
+ let viewport = cx.update(|window, _| window.viewport_size());
+ let rows = cx.debug_bounds("volume-rows").expect("the rows are drawn");
+ let centre = rows.center();
+ assert!(
+ (centre.x - viewport.width / 2.0).abs() < px(4.),
+ "the popup is not centred across: {centre:?} in {viewport:?}"
+ );
+ assert!(
+ (centre.y - viewport.height / 2.0).abs() < px(150.),
+ "the popup is not centred down: {centre:?} in {viewport:?}"
+ );
+
+ // The wrapper that centres the popup must not swallow the backdrop's
+ // clicks: a click in the corner, outside the popup, still closes it.
+ cx.simulate_click(
+ gpui_kit::point(px(4.), px(4.)),
+ gpui_kit::Modifiers::none(),
+ );
+ draw(cx);
+ assert!(
+ !read(&view, cx, |app| app.volumes_open),
+ "clicking outside the picker closes it"
+ );
+}
+
#[gpui_kit::test]
fn showing_a_tile_that_is_gone_says_so_instead(cx: &mut TestAppContext) {
cx.update(gpui_omarchy::init);
diff --git a/crates/disktree-app/src/views.rs b/crates/disktree-app/src/views.rs
index 22febac..8748b56 100644
--- a/crates/disktree-app/src/views.rs
+++ b/crates/disktree-app/src/views.rs
@@ -13,7 +13,7 @@ use gpui_kit::base::CheckboxState;
use gpui_kit::{
App, AppContext as _, ClickEvent, Context, Div, DragMoveEvent, ElementId,
FontWeight, InteractiveElement as _, IntoElement, KeyDownEvent,
- ParentElement, Rems, SharedString, Stateful,
+ MouseDownEvent, ParentElement, Rems, SharedString, Stateful,
StatefulInteractiveElement as _, Styled, Window, anchored, deferred, div,
pattern_slash, px, relative,
};
@@ -128,7 +128,12 @@ fn volumes_dialog(
) -> impl IntoElement {
let theme = cx.omarchy().clone();
let cancel = cx.entity().downgrade();
- let mut rows = div().flex().flex_col().gap(space::XS);
+ let mut rows = div()
+ .id("volume-rows")
+ .debug_selector(|| "volume-rows".into())
+ .flex()
+ .flex_col()
+ .gap(space::XS);
if app.volumes.is_empty() {
rows = rows
.child(dialog_description("No other volume could be read.", cx));
@@ -176,6 +181,16 @@ fn volumes_dialog(
cx,
))
.child(rows);
+ let centred = centred_popup(popup, {
+ let close = cancel.clone();
+ move |_, window, cx| {
+ let _ = close.update(cx, |this, cx| {
+ this.volumes_open = false;
+ cx.notify();
+ this.apply_focus(window, cx);
+ });
+ }
+ });
alert_dialog(&app.confirm_focus, cx)
.open(true)
.on_cancel(move |_, window, cx| {
@@ -186,7 +201,42 @@ fn volumes_dialog(
});
false
})
- .popup(popup)
+ .popup(centred)
+}
+
+/// Put a dialog's popup in the middle of the window, and close it when a
+/// click lands outside it.
+///
+/// The base dialog hosts its popup as an ordinary child of a full-window box,
+/// so a popup lands in the top-left corner unless something centres it. That
+/// something ends up in front of the backdrop, which is what used to receive
+/// the click that dismisses a dialog, so the click is taken here instead. The
+/// card stops a mouse-down from reaching this wrapper, so a click on the
+/// dialog itself stays the dialog's own.
+fn centred_popup(
+ card: impl IntoElement,
+ on_outside: impl Fn(&MouseDownEvent, &mut Window, &mut App) + 'static,
+) -> Stateful<Div> {
+ div()
+ .id("dialog-outside")
+ .absolute()
+ .inset_0()
+ .flex()
+ .items_center()
+ .justify_center()
+ // Any button, as the backdrop took them: a right-click outside closes
+ // the dialog too.
+ .on_any_mouse_down(on_outside)
+ .child(
+ div()
+ .id("dialog-card")
+ .on_any_mouse_down(
+ |_: &MouseDownEvent, _: &mut Window, cx: &mut App| {
+ cx.stop_propagation();
+ },
+ )
+ .child(card),
+ )
}
/// The one question disktree asks: a permanent deletion cannot be undone, so
@@ -243,6 +293,17 @@ fn delete_dialog(
.child(dialog_title(title, cx))
.child(dialog_description(body, cx))
.child(actions);
+ // Built before the chain below, which moves `cancel` into its own
+ // handler for Escape.
+ let centred = centred_popup(popup, {
+ let close = cancel.clone();
+ move |_, window, cx| {
+ let _ = close.update(cx, |this, cx| {
+ this.cancel_delete(cx);
+ this.apply_focus(window, cx);
+ });
+ }
+ });
alert_dialog(&app.confirm_focus, cx)
.open(true)
.on_ok(move |_, window, cx| {
@@ -259,7 +320,7 @@ fn delete_dialog(
});
false
})
- .popup(popup)
+ .popup(centred)
}
// ── explore ─────────────────────────────────────────────────────────────