gpui-material-components 0.3.2

Material 3 Expressive desktop components for GPUI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::{cell::Cell, rc::Rc};

use gpui::{
    AnyElement, App, AvailableSpace, Bounds, Canvas, Div, Edges, ElementId, InteractiveElement,
    IntoElement, ParentElement, Pixels, RenderOnce, Styled, Window, canvas, div,
    prelude::FluentBuilder, px,
};
use gpui_base::TestSupportExt;
use gpui_material_foundation::current_material_theme;
use gpui_material_theme::Direction;

use crate::{ScaffoldInsets, Sheet};

/// Positions a scaffold's floating action within the usable content width.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ScaffoldFabPosition {
    /// Align the floating action button to the logical start of the content area.
    Start,
    /// Center the floating action button horizontally within the content area.
    Center,
    /// Align the floating action button to the logical end of the content area.
    #[default]
    End,
    /// At the logical end of the scaffold, over the bottom bar.
    EndOverlay,
}

/// Resolved body layout before the content builder applies its padding.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ScaffoldContentLayout {
    /// Window-local bounds after the scaffold has allocated its bars and rail.
    pub bounds: Bounds<Pixels>,
    /// Space already allocated to the bars and rail.
    pub occupied: ScaffoldInsets,
    /// Remaining insets for the body or a child inside its scroll area.
    pub padding: ScaffoldInsets,
    /// Active layout direction from the window theme.
    pub direction: Direction,
}

type ContentBuilder = Box<dyn FnOnce(ScaffoldContentLayout, &mut Window, &mut App) -> AnyElement>;

enum Content {
    Element(AnyElement),
    Builder(ContentBuilder),
}

/// Arranges the top app bar, leading rail, content and bottom bar. The floating
/// action and snackbar are anchored within the content region above bottom
/// navigation. Give the scaffold a bounded size and give its content a scroll
/// viewport if needed.
#[derive(IntoElement)]
pub struct Scaffold {
    id: ElementId,
    content: Content,
    app_bar: Option<AnyElement>,
    rail: Option<AnyElement>,
    bottom_bar: Option<AnyElement>,
    bottom_sheet: Option<Sheet>,
    fab: Option<AnyElement>,
    snackbar: Option<AnyElement>,
    fab_position: ScaffoldFabPosition,
    content_insets: ScaffoldInsets,
}

impl Scaffold {
    /// Creates a scaffold with the given element ID and primary body content.
    pub fn new(id: impl Into<ElementId>, content: impl IntoElement) -> Self {
        Self::with_content(id.into(), Content::Element(content.into_any_element()))
    }

    /// Builds the body with its current layout. Apply `layout.padding` once;
    /// the scaffold has already allocated `layout.occupied`.
    pub fn with_layout<E: IntoElement>(
        id: impl Into<ElementId>,
        content: impl FnOnce(ScaffoldContentLayout, &mut Window, &mut App) -> E + 'static,
    ) -> Self {
        Self::with_content(
            id.into(),
            Content::Builder(Box::new(move |layout, window, cx| {
                content(layout, window, cx).into_any_element()
            })),
        )
    }

    fn with_content(id: ElementId, content: Content) -> Self {
        Self {
            id,
            content,
            app_bar: None,
            rail: None,
            bottom_bar: None,
            bottom_sheet: None,
            fab: None,
            snackbar: None,
            fab_position: ScaffoldFabPosition::default(),
            content_insets: ScaffoldInsets::default(),
        }
    }

    /// Places a top app bar above the content region.
    pub fn app_bar(mut self, element: impl IntoElement) -> Self {
        self.app_bar = Some(element.into_any_element());
        self
    }

    /// Places a leading navigation rail or drawer at the logical start of the scaffold.
    pub fn rail(mut self, element: impl IntoElement) -> Self {
        self.rail = Some(element.into_any_element());
        self
    }

    /// Places a bottom navigation bar or bottom app bar beneath the content region.
    pub fn bottom_bar(mut self, element: impl IntoElement) -> Self {
        self.bottom_bar = Some(element.into_any_element());
        self
    }

    /// Places a Standard bottom sheet beneath the measured content column.
    pub fn bottom_sheet(mut self, sheet: Sheet) -> Self {
        self.bottom_sheet = Some(sheet);
        self
    }

    /// Anchors a floating action button within the scaffold according to [`Self::fab_position`].
    pub fn fab(mut self, element: impl IntoElement) -> Self {
        self.fab = Some(element.into_any_element());
        self
    }

    /// Mounts a snackbar host anchored above bottom navigation and the floating action button.
    pub fn snackbar(mut self, element: impl IntoElement) -> Self {
        self.snackbar = Some(element.into_any_element());
        self
    }

    /// Sets the horizontal and overlay placement of the floating action button (default [`ScaffoldFabPosition::End`]).
    pub fn fab_position(mut self, position: ScaffoldFabPosition) -> Self {
        self.fab_position = position;
        self
    }

    /// Sets minimum distances from the scaffold's edges to usable content.
    /// Space occupied by bars and the rail counts toward those distances.
    pub fn content_insets(mut self, insets: ScaffoldInsets) -> Self {
        self.content_insets = insets.normalized();
        self
    }
}

impl RenderOnce for Scaffold {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let direction = current_material_theme(window, cx).layout.direction;
        let position = self.fab_position;
        let insets = self.content_insets;
        let (fab, snackbar, overlay) = match (position, self.fab) {
            (ScaffoldFabPosition::EndOverlay, Some(fab)) => {
                (None, None, Some((fab, self.snackbar)))
            }
            (_, fab) => (fab, self.snackbar, None),
        };
        let needs_layout = overlay.is_some()
            || insets != ScaffoldInsets::default()
            || matches!(self.content, Content::Builder(_));
        let geometry = Rc::new(ScaffoldGeometry::default());
        let body_geometry = geometry.clone();
        let body = div()
            .id("scaffold-content")
            .test_support()
            .relative()
            .flex()
            .flex_col()
            .flex_1()
            .min_w_0()
            .min_h_0();
        let body =
            match self.content {
                Content::Element(content) if !needs_layout => body
                    .child(body_wrapper().child(content))
                    .children(standard_overlay(
                        fab,
                        snackbar,
                        position,
                        insets.resolve(direction),
                        direction,
                    )),
                content => body.child(
                    laid_out_slot(move |bounds, window, cx| {
                        let layout = body_geometry.resolve(bounds, insets, direction);
                        let padding = layout.padding.resolve(direction);
                        let content = match content {
                            Content::Element(content) => body_wrapper()
                                .pt(padding.top)
                                .pr(padding.right)
                                .pb(padding.bottom)
                                .pl(padding.left)
                                .child(content),
                            Content::Builder(build) => {
                                body_wrapper().child(build(layout, window, cx))
                            }
                        };
                        div().relative().flex().flex_col().child(content).children(
                            standard_overlay(fab, snackbar, position, padding, direction),
                        )
                    })
                    .size_full(),
                ),
            };
        let column = match self.bottom_sheet {
            Some(sheet) => div()
                .flex_1()
                .min_w_0()
                .min_h_0()
                .child(
                    laid_out_slot(move |bounds, _, _| {
                        div()
                            .flex()
                            .flex_col()
                            .child(body)
                            .child(div().flex_none().child(sheet.in_scaffold(bounds.size)))
                    })
                    .size_full(),
                )
                .into_any_element(),
            None => body.into_any_element(),
        };
        let root_geometry = geometry.clone();
        div()
            .id(self.id)
            .test_support()
            .size_full()
            .min_h_0()
            .min_w_0()
            .flex()
            .flex_col()
            .when(needs_layout, move |root| {
                root.relative().child(
                    canvas(
                        move |bounds, _, _| root_geometry.root.set(Some(bounds)),
                        |_, (), _, _| {},
                    )
                    .absolute()
                    .inset_0(),
                )
            })
            .children(self.app_bar.map(|bar| div().flex_none().child(bar)))
            .child(
                div()
                    .flex_1()
                    .min_h_0()
                    .flex()
                    .when(direction == Direction::Rtl, Styled::flex_row_reverse)
                    .children(self.rail.map(|rail| div().flex_none().h_full().child(rail)))
                    .child(column),
            )
            .children(self.bottom_bar.map(|bar| div().flex_none().child(bar)))
            .children(overlay.map(move |(fab, snackbar)| {
                laid_out_slot(move |root, _, _| {
                    let layout = geometry
                        .layout
                        .get()
                        .unwrap_or_else(|| geometry.resolve(root, insets, direction));
                    end_overlay(fab, snackbar, root, layout, insets.bottom)
                })
                .absolute()
                .inset_0()
            }))
    }
}

fn body_wrapper() -> Div {
    div().flex_1().min_h_0().min_w_0().flex().flex_col()
}

#[derive(Default)]
struct ScaffoldGeometry {
    root: Cell<Option<Bounds<Pixels>>>,
    layout: Cell<Option<ScaffoldContentLayout>>,
}

impl ScaffoldGeometry {
    fn resolve(
        &self,
        bounds: Bounds<Pixels>,
        requested: ScaffoldInsets,
        direction: Direction,
    ) -> ScaffoldContentLayout {
        let root = self.root.get().unwrap_or(bounds);
        let occupied = ScaffoldInsets {
            top: (bounds.top() - root.top()).max(px(0.)),
            start: if direction == Direction::Rtl {
                (root.right() - bounds.right()).max(px(0.))
            } else {
                (bounds.left() - root.left()).max(px(0.))
            },
            bottom: (root.bottom() - bounds.bottom()).max(px(0.)),
            end: px(0.),
        };
        let top = (requested.top - occupied.top)
            .max(px(0.))
            .min(bounds.size.height);
        let start = (requested.start - occupied.start)
            .max(px(0.))
            .min(bounds.size.width);
        let padding = ScaffoldInsets {
            top,
            start,
            bottom: (requested.bottom - occupied.bottom)
                .max(px(0.))
                .min(bounds.size.height - top),
            end: requested.end.min(bounds.size.width - start),
        };
        let layout = ScaffoldContentLayout {
            bounds,
            occupied,
            padding,
            direction,
        };
        self.layout.set(Some(layout));
        layout
    }
}

// Resolve the body against the outer flex layout before building its contents.
// The resulting child is laid out, prepainted, and painted exactly once.
fn laid_out_slot(
    build: impl FnOnce(Bounds<Pixels>, &mut Window, &mut App) -> Div + 'static,
) -> Canvas<AnyElement> {
    canvas(
        move |bounds, window, cx| {
            let mut child = build(bounds, window, cx)
                .w(bounds.size.width)
                .h(bounds.size.height)
                .into_any_element();
            child.layout_as_root(bounds.size.map(AvailableSpace::Definite), window, cx);
            child.prepaint_at(bounds.origin, window, cx);
            child
        },
        |_, mut child, window, cx| child.paint(window, cx),
    )
}

fn fab_row(position: ScaffoldFabPosition, direction: Direction) -> Div {
    let row = div().flex();
    match (position, direction) {
        (ScaffoldFabPosition::Center, _) => row.justify_center(),
        (ScaffoldFabPosition::Start, Direction::Ltr)
        | (ScaffoldFabPosition::End | ScaffoldFabPosition::EndOverlay, Direction::Rtl) => {
            row.justify_start()
        }
        _ => row.justify_end(),
    }
}

fn standard_overlay(
    fab: Option<AnyElement>,
    snackbar: Option<AnyElement>,
    position: ScaffoldFabPosition,
    padding: Edges<Pixels>,
    direction: Direction,
) -> Option<Div> {
    (fab.is_some() || snackbar.is_some()).then(|| {
        div()
            .absolute()
            .left(padding.left)
            .right(padding.right)
            .bottom(padding.bottom)
            .min_w_0()
            .flex()
            .flex_col()
            .gap(px(8.))
            .p(px(16.))
            .children(fab.map(|fab| fab_row(position, direction).child(fab)))
            .children(snackbar)
    })
}

fn end_overlay(
    fab: AnyElement,
    snackbar: Option<AnyElement>,
    root: Bounds<Pixels>,
    layout: ScaffoldContentLayout,
    bottom_inset: Pixels,
) -> Div {
    let padding = layout.padding.resolve(layout.direction);
    let bottom = bottom_inset.min(root.size.height);
    let fab = fab_row(ScaffoldFabPosition::EndOverlay, layout.direction)
        .items_end()
        .pb(px(16.))
        .when(snackbar.is_some(), |fab| {
            // Reserve the taller of the bottom bar and the FAB band. The
            // button stays at the bottom of the band even with a tall bar.
            fab.min_h((layout.occupied.bottom + px(16.) - bottom).max(px(0.)))
                .pt(px(8.))
        })
        .child(fab);
    div().relative().child(
        div()
            .absolute()
            .left(layout.bounds.left() - root.left() + padding.left)
            .right(root.right() - layout.bounds.right() + padding.right)
            .bottom(bottom)
            .min_w_0()
            .flex()
            .flex_col()
            .px(px(16.))
            .children(snackbar)
            .child(fab),
    )
}