gpui_material_components/navigation/
bottom_app_bar.rs1use gpui::{
2 AnyElement, App, ElementId, Entity, Hsla, InteractiveElement, IntoElement, ParentElement,
3 Pixels, RenderOnce, Role, ScrollHandle, SharedString, StatefulInteractiveElement, Styled,
4 Window, div, prelude::FluentBuilder, px,
5};
6use gpui_base::TestSupportExt;
7use gpui_material_foundation::{Surface, animate, current_material_theme};
8use gpui_material_theme::{
9 ColorRole, CornerRole, Direction, Elevation, MotionKind, MotionSpec, MotionSpeed,
10};
11
12use super::app_bar::{AppBarScrollBehavior, AppBarScrollState};
13
14#[derive(Clone, Copy, Debug, Default, PartialEq)]
16pub struct BottomAppBarColors {
17 pub container: Option<Hsla>,
18 pub content: Option<Hsla>,
19}
20
21#[derive(Clone, Copy, Debug, PartialEq)]
24pub struct BottomAppBarPadding {
25 pub start: Pixels,
26 pub end: Pixels,
27 pub top: Pixels,
28 pub bottom: Pixels,
29}
30
31impl Default for BottomAppBarPadding {
32 fn default() -> Self {
33 Self {
34 start: px(4.),
35 end: px(16.),
36 top: px(12.),
37 bottom: px(12.),
38 }
39 }
40}
41
42#[derive(IntoElement)]
45pub struct BottomAppBar {
46 id: ElementId,
47 actions: Vec<AnyElement>,
48 fab: Option<AnyElement>,
49 height: Pixels,
50 colors: BottomAppBarColors,
51 padding: BottomAppBarPadding,
52 elevation: Elevation,
53 scroll: Option<ScrollHandle>,
54 scroll_behavior: Option<(AppBarScrollBehavior, Entity<AppBarScrollState>)>,
55 accessible_label: SharedString,
56}
57
58impl BottomAppBar {
59 pub const DEFAULT_HEIGHT: Pixels = px(80.);
60
61 pub fn new(id: impl Into<ElementId>) -> Self {
62 Self {
63 id: id.into(),
64 actions: Vec::new(),
65 fab: None,
66 height: Self::DEFAULT_HEIGHT,
67 colors: BottomAppBarColors::default(),
68 padding: BottomAppBarPadding::default(),
69 elevation: Elevation::Level2,
70 scroll: None,
71 scroll_behavior: None,
72 accessible_label: "Bottom app bar".into(),
73 }
74 }
75
76 pub fn action(mut self, action: impl IntoElement) -> Self {
78 self.actions.push(action.into_any_element());
79 self
80 }
81
82 pub fn actions(mut self, actions: impl IntoIterator<Item = AnyElement>) -> Self {
84 self.actions.extend(actions);
85 self
86 }
87
88 pub fn fab(mut self, fab: impl IntoElement) -> Self {
90 self.fab = Some(fab.into_any_element());
91 self
92 }
93
94 pub fn height(mut self, height: Pixels) -> Self {
96 let value = f32::from(height);
97 if value.is_finite() && value > 0. {
98 self.height = height;
99 }
100 self
101 }
102
103 pub fn colors(mut self, colors: BottomAppBarColors) -> Self {
105 self.colors = colors;
106 self
107 }
108
109 pub fn padding(mut self, padding: BottomAppBarPadding) -> Self {
111 self.padding = padding;
112 self
113 }
114
115 pub fn elevation(mut self, elevation: Elevation) -> Self {
117 self.elevation = elevation;
118 self
119 }
120
121 pub fn track_scroll(mut self, scroll: &ScrollHandle) -> Self {
123 self.scroll = Some(scroll.clone());
124 self
125 }
126
127 pub fn scroll_behavior(
129 mut self,
130 behavior: AppBarScrollBehavior,
131 state: &Entity<AppBarScrollState>,
132 ) -> Self {
133 self.scroll_behavior = Some((behavior, state.clone()));
134 self
135 }
136
137 pub fn accessible_label(mut self, label: impl Into<SharedString>) -> Self {
139 self.accessible_label = label.into();
140 self
141 }
142}
143
144fn sanitize_padding(value: Pixels) -> Pixels {
145 let raw = f32::from(value);
146 if raw.is_finite() && raw >= 0. {
147 value
148 } else {
149 px(0.)
150 }
151}
152
153impl RenderOnce for BottomAppBar {
154 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
155 let theme = current_material_theme(window, cx);
156 let direction = theme.layout.direction;
157 let rtl = direction == Direction::Rtl;
158 let full_height = f32::from(self.height);
159
160 let collapsed_fraction = if let Some((behavior, state)) = &self.scroll_behavior {
161 let handle_offset = self
162 .scroll
163 .as_ref()
164 .map(|handle| (-f32::from(handle.offset().y)).max(0.));
165 let position = state.update(cx, |state, _| {
166 state.prepare(handle_offset, full_height, Some(*behavior), true)
167 });
168 let motion = if position.settling && !cx.reduce_motion() {
169 theme
170 .motion
171 .spec(MotionKind::Spatial, MotionSpeed::Default, false)
172 } else {
173 MotionSpec::Instant
174 };
175 let fraction = animate(
176 (self.id.clone(), "bottom-app-bar-collapse"),
177 position.target,
178 motion,
179 window,
180 cx,
181 )
182 .clamp(0., 1.);
183 state.update(cx, |state, cx| {
184 if state.display(position, fraction) {
185 cx.notify();
186 }
187 });
188 fraction
189 } else {
190 0.
191 };
192
193 let visible_height = px((full_height * (1. - collapsed_fraction)).max(0.));
194 let scope = window
195 .use_keyed_state((self.id.clone(), "bottom-app-bar-focus"), cx, |_, cx| {
196 cx.focus_handle().tab_stop(false)
197 })
198 .read(cx)
199 .clone();
200
201 if visible_height <= px(0.5) {
202 if scope.contains_focused(window, cx) {
203 let focus = window.focused(cx);
204 window.defer(cx, move |window, cx| {
205 if window
206 .focused(cx)
207 .is_none_or(|current| Some(current) == focus)
208 {
209 window.focus_next(cx);
210 }
211 });
212 }
213 return div()
214 .id(self.id)
215 .test_support()
216 .w_full()
217 .h(px(0.))
218 .overflow_hidden()
219 .into_any_element();
220 }
221
222 let (left, right) = theme.layout.horizontal(
223 sanitize_padding(self.padding.start),
224 sanitize_padding(self.padding.end),
225 );
226 let top = sanitize_padding(self.padding.top);
227 let bottom = sanitize_padding(self.padding.bottom);
228
229 let actions_row = div()
230 .id((self.id.clone(), "actions"))
231 .test_support()
232 .flex()
233 .items_center()
234 .gap(px(4.))
235 .when(rtl, Styled::flex_row_reverse)
236 .children(self.actions);
237
238 let bar_row = div()
239 .w_full()
240 .h(self.height)
241 .pl(left)
242 .pr(right)
243 .pt(top)
244 .pb(bottom)
245 .flex()
246 .items_center()
247 .justify_between()
248 .when(rtl, Styled::flex_row_reverse)
249 .child(actions_row)
250 .children(self.fab.map(|fab| {
251 div()
252 .id((self.id.clone(), "fab-slot"))
253 .test_support()
254 .flex_none()
255 .child(fab)
256 }));
257
258 let surface = Surface::new(ColorRole::SurfaceContainer)
259 .shape(CornerRole::None.shape())
260 .tonal_elevation(self.elevation)
261 .shadow_elevation(self.elevation)
262 .when_some(self.colors.container, Surface::container_color)
263 .when_some(self.colors.content, Surface::content_color)
264 .w_full()
265 .h(visible_height)
266 .overflow_hidden()
267 .child(bar_row);
268
269 div()
270 .id(self.id)
271 .test_support()
272 .role(Role::Toolbar)
273 .aria_label(self.accessible_label)
274 .track_focus(&scope)
275 .w_full()
276 .h(visible_height)
277 .flex_none()
278 .overflow_hidden()
279 .child(surface)
280 .into_any_element()
281 }
282}