Skip to main content

gpui_material_components/navigation/
scaffold.rs

1use gpui::{
2    AnyElement, App, ElementId, InteractiveElement, IntoElement, ParentElement, RenderOnce, Styled,
3    Window, div, prelude::FluentBuilder, px,
4};
5use gpui_base::TestSupportExt;
6use gpui_material_foundation::window_material_theme;
7use gpui_material_theme::Direction;
8
9/// Arranges the top app bar, leading rail, content and bottom bar. The floating
10/// action and snackbar are anchored within the content region above bottom
11/// navigation. Give the scaffold a bounded size and give its content a scroll
12/// viewport if needed.
13#[derive(IntoElement)]
14pub struct Scaffold {
15    id: ElementId,
16    content: AnyElement,
17    app_bar: Option<AnyElement>,
18    rail: Option<AnyElement>,
19    bottom_bar: Option<AnyElement>,
20    fab: Option<AnyElement>,
21    snackbar: Option<AnyElement>,
22}
23
24impl Scaffold {
25    pub fn new(id: impl Into<ElementId>, content: impl IntoElement) -> Self {
26        Self {
27            id: id.into(),
28            content: content.into_any_element(),
29            app_bar: None,
30            rail: None,
31            bottom_bar: None,
32            fab: None,
33            snackbar: None,
34        }
35    }
36
37    pub fn app_bar(mut self, element: impl IntoElement) -> Self {
38        self.app_bar = Some(element.into_any_element());
39        self
40    }
41
42    pub fn rail(mut self, element: impl IntoElement) -> Self {
43        self.rail = Some(element.into_any_element());
44        self
45    }
46
47    pub fn bottom_bar(mut self, element: impl IntoElement) -> Self {
48        self.bottom_bar = Some(element.into_any_element());
49        self
50    }
51
52    pub fn fab(mut self, element: impl IntoElement) -> Self {
53        self.fab = Some(element.into_any_element());
54        self
55    }
56
57    pub fn snackbar(mut self, element: impl IntoElement) -> Self {
58        self.snackbar = Some(element.into_any_element());
59        self
60    }
61}
62
63impl RenderOnce for Scaffold {
64    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
65        let rtl = window_material_theme(window, cx).layout.direction == Direction::Rtl;
66        let has_snackbar = self.snackbar.is_some();
67        div()
68            .id(self.id)
69            .test_support()
70            .size_full()
71            .min_h_0()
72            .min_w_0()
73            .flex()
74            .flex_col()
75            .children(self.app_bar.map(|bar| div().flex_none().child(bar)))
76            .child(
77                div()
78                    .flex_1()
79                    .min_h_0()
80                    .flex()
81                    .when(rtl, Styled::flex_row_reverse)
82                    .children(self.rail.map(|rail| div().flex_none().h_full().child(rail)))
83                    .child(
84                        div()
85                            .id("scaffold-content")
86                            .test_support()
87                            .relative()
88                            .flex()
89                            .flex_col()
90                            .flex_1()
91                            .min_w_0()
92                            .min_h_0()
93                            .child(
94                                div()
95                                    .flex_1()
96                                    .min_h_0()
97                                    .min_w_0()
98                                    .flex()
99                                    .flex_col()
100                                    .child(self.content),
101                            )
102                            .when(self.fab.is_some() || has_snackbar, |content| {
103                                content.child(
104                                    div()
105                                        .absolute()
106                                        .left_0()
107                                        .right_0()
108                                        .bottom_0()
109                                        .min_w_0()
110                                        .flex()
111                                        .flex_col()
112                                        .gap(px(8.))
113                                        .p(px(16.))
114                                        .children(self.fab.map(|fab| {
115                                            div()
116                                                .flex()
117                                                .when_else(
118                                                    rtl,
119                                                    Styled::justify_start,
120                                                    Styled::justify_end,
121                                                )
122                                                .child(fab)
123                                        }))
124                                        .children(self.snackbar),
125                                )
126                            }),
127                    ),
128            )
129            .children(self.bottom_bar.map(|bar| div().flex_none().child(bar)))
130    }
131}