Skip to main content

ui/popover/
modal.rs

1//! Modals, and sheets pinned to an edge.
2
3use super::*;
4
5/// Modal/overlay scrim at the *current* appearance, quoted in dark-mode terms
6/// like [`ink`]/[`hairline`] — for callers (`modal`, the attachment lightbox)
7/// that paint from a `deferred`/`anchored` layer with no `Theme`/`cx` in
8/// scope. Mirrors [`Theme::scrim`], which is pinned at `X = 0.6` dark /
9/// `0.32` light; other dark-mode alphas scale the light side by the same
10/// ratio so the *dark* result is always exactly `alpha_dark` (never routed
11/// through [`Hsla::opacity`], whose `0..=1` clamp would clip a
12/// larger-than-0.6 alpha before it could scale the light side).
13pub(crate) fn scrim_alpha(alpha_dark: f32) -> gpui::Hsla {
14    theme::scrim(alpha_dark)
15}
16
17/// Full-window modal: dim scrim + centered card with the `dialog-in` entrance.
18/// The scrim swallows clicks; the caller wires its own dismiss/confirm. Binds
19/// no keys, `escape` included.
20/// `viewport` is the window size (an `anchored` layer sizes to its children,
21/// so the scrim needs explicit dimensions). The frost radius matches
22/// [`dialog_card`]'s 16px rounding.
23/// `on_dismiss` is the scrim press. It is a parameter rather than the caller's
24/// `.on_mouse_down_out`, for the same reason [`sheet`]'s is: the scrim lives
25/// inside this deferred layer, so nothing outside can reach it. Without it a
26/// dialog could not be dismissed by clicking away from it *by any caller* —
27/// which is how this one shipped, and what it looked like was a dialog that
28/// only closed on its own buttons.
29pub fn modal(
30    id: impl Into<ElementId>,
31    viewport: gpui::Size<Pixels>,
32    card: AnyElement,
33    on_dismiss: impl Fn(&gpui::MouseDownEvent, &mut gpui::Window, &mut gpui::App) + 'static,
34) -> AnyElement {
35    modal_with(id, viewport, card, DIALOG_RADIUS, 0.6, on_dismiss)
36}
37
38/// [`modal`] for glass-tinted cards (the add-space palette): a LIGHTER scrim,
39/// so the material card reads like the popovers — the standard 0.6 dim buried
40/// the backdrop hue under the blur and the palette came out a flat grey slab
41/// next to the hue-inheriting menus (user report).
42///
43/// The radius is [`Theme::surface_radius`], not a parameter: a glass-tinted
44/// modal *is* a popover surface, and the parameter this used to take carried
45/// the doc line "must match the card's rounding" — a footgun handed to the
46/// caller in writing.
47pub fn modal_glass(
48    id: impl Into<ElementId>,
49    viewport: gpui::Size<Pixels>,
50    card: AnyElement,
51    on_dismiss: impl Fn(&gpui::MouseDownEvent, &mut gpui::Window, &mut gpui::App) + 'static,
52) -> AnyElement {
53    modal_with(
54        id,
55        viewport,
56        card,
57        Theme::surface_radius(),
58        0.35,
59        on_dismiss,
60    )
61}
62
63pub(super) fn modal_with(
64    id: impl Into<ElementId>,
65    viewport: gpui::Size<Pixels>,
66    card: AnyElement,
67    corner_radius: f32,
68    scrim: f32,
69    on_dismiss: impl Fn(&gpui::MouseDownEvent, &mut gpui::Window, &mut gpui::App) + 'static,
70) -> AnyElement {
71    let card = crate::surface::popover(corner_radius, card).into_any_element();
72    gpui::deferred(
73        gpui::anchored()
74            .position(gpui::point(px(0.0), px(0.0)))
75            .child(
76                div()
77                    .occlude()
78                    .w(viewport.width)
79                    .h(viewport.height)
80                    .bg(scrim_alpha(scrim))
81                    .flex()
82                    .items_center()
83                    .justify_center()
84                    // A press hit-tested onto the scrim: the card occludes it,
85                    // and so does a menu the card opens, which draws on a layer
86                    // above and may reach past the card's edge.
87                    .on_any_mouse_down(on_dismiss)
88                    .child(crate::cover::cover())
89                    .child(motion::dialog_in(id, div().occlude().child(card))),
90            ),
91    )
92    .priority(MODAL_LAYER)
93    .into_any_element()
94}
95
96// ---------------------------------------------------------------------------
97// Sheet — a dialog pinned to an edge
98// ---------------------------------------------------------------------------
99
100/// Which edge a [`sheet`] slides in from.
101#[derive(Debug, Clone, Copy, PartialEq, Eq)]
102pub enum Side {
103    Left,
104    Right,
105    /// Up from the bottom edge, full width — the shape a phone puts a picker
106    /// or a share list in, and what a narrow window wants instead of a side
107    /// panel that leaves no room for the page behind it.
108    Bottom,
109}
110
111impl Side {
112    /// Which way the panel travels. The extent a [`sheet`] is given is read
113    /// along this axis: a width for the vertical edges, a height for the
114    /// horizontal one.
115    fn axis(self) -> gpui::Axis {
116        match self {
117            Side::Left | Side::Right => gpui::Axis::Horizontal,
118            Side::Bottom => gpui::Axis::Vertical,
119        }
120    }
121}
122
123/// Corner rounding of [`dialog_card`], and of a [`sheet_panel`]'s two inner
124/// corners (the two on the window edge are off-screen). One number rather than
125/// two that happen to match: a sheet *is* the dialog card, pinned to an edge
126/// instead of centred. Read three times over — the card, the sheet panel, and
127/// the blur under each — which is exactly why it is not a literal.
128pub(super) const DIALOG_RADIUS: f32 = 16.0;
129
130/// The panel body of a [`sheet`]: glass card chrome rounded and hairlined on
131/// its *inner* edge only — the corners against the window edge are off-screen
132/// — so it reads as pulled out of the window rather than floating near it.
133pub fn sheet_panel(theme: &Theme, side: Side) -> gpui::Div {
134    let card = div()
135        .size_full()
136        .flex()
137        .flex_col()
138        .shadow_lg()
139        .text_color(theme.text);
140    let card = match side {
141        Side::Left => card
142            .rounded_r(px(DIALOG_RADIUS))
143            .border_r_1()
144            .border_color(theme.border),
145        Side::Right => card
146            .rounded_l(px(DIALOG_RADIUS))
147            .border_l_1()
148            .border_color(theme.border),
149        Side::Bottom => card
150            .rounded_t(px(DIALOG_RADIUS))
151            .border_t_1()
152            .border_color(theme.border),
153    };
154    if theme.glass {
155        card.bg(theme.glass_overlay())
156    } else {
157        card.bg(theme.surface_overlay)
158    }
159}
160
161/// A panel over a dim scrim — [`modal`] pinned to an edge. It slides in over
162/// [`motion::DIALOG_IN`] and, once the caller's [`Popup`] enters its exit
163/// phase, back out over [`motion::MENU_OUT`] — which it must, because
164/// [`Popup::finish_close`] reaps on that spec's span.
165///
166/// `extent` is measured across the edge the sheet is pinned to: a width on
167/// [`Side::Left`] and [`Side::Right`], a height on [`Side::Bottom`]. The other
168/// axis always spans the viewport.
169///
170/// `on_dismiss` is the scrim click. Unlike the anchored menus, dismissal
171/// cannot be the caller's `.on_mouse_down_out`: the scrim lives inside this
172/// deferred layer, so nothing outside can reach it.
173///
174/// The slide is written here rather than as a `motion` helper because
175/// only the *spec* is motion — which inset carries it is layout, and it
176/// differs per side.
177pub fn sheet(
178    id: impl Into<SharedString>,
179    viewport: gpui::Size<Pixels>,
180    side: Side,
181    extent: Pixels,
182    content: AnyElement,
183    closing: Option<web_time::Instant>,
184    on_dismiss: impl Fn(&gpui::ClickEvent, &mut gpui::Window, &mut gpui::App) + 'static,
185) -> AnyElement {
186    let id = id.into();
187    let exit = closing.map(exit_progress);
188    // The panel spans the edge it is pinned to and takes `extent` across it,
189    // which is why the argument is not called a width: on [`Side::Bottom`] it
190    // is the height.
191    let panel = div().absolute();
192    let panel = match side.axis() {
193        gpui::Axis::Horizontal => panel.top_0().bottom_0().w(extent),
194        gpui::Axis::Vertical => panel.left_0().right_0().h(extent),
195    };
196    let panel = panel.child(crate::surface::popover(DIALOG_RADIUS, content));
197    // `t` runs 0 (fully off-screen) → 1 (seated against the edge).
198    let seat = move |el: gpui::Div, t: f32| {
199        let inset = extent * (t - 1.0);
200        match side {
201            Side::Left => el.left(inset),
202            Side::Right => el.right(inset),
203            Side::Bottom => el.bottom(inset),
204        }
205    };
206    let panel = if let Some(t) = exit {
207        // The dying panel must not take clicks — same overlay `menu_motion`
208        // puts over an exiting menu.
209        let panel = seat(panel, 1.0 - t).child(div().absolute().inset_0().occlude());
210        panel
211            .with_animation(
212                SharedString::from(format!("{id}-out")),
213                motion::MENU_OUT.animation(),
214                move |el, _| el,
215            )
216            .into_any_element()
217    } else {
218        panel
219            .with_animation(id.clone(), motion::DIALOG_IN.animation(), seat)
220            .into_any_element()
221    };
222
223    gpui::deferred(
224        gpui::anchored()
225            .position(gpui::point(px(0.0), px(0.0)))
226            .child(
227                div()
228                    .id(SharedString::from(format!("{id}-scrim")))
229                    .occlude()
230                    .relative()
231                    .w(viewport.width)
232                    .h(viewport.height)
233                    .bg(scrim_alpha(0.6 * (1.0 - exit.unwrap_or(0.0))))
234                    .on_click(on_dismiss)
235                    .child(crate::cover::cover())
236                    .child(panel),
237            ),
238    )
239    .priority(MODAL_LAYER)
240    .into_any_element()
241}