gpui_material_components/navigation/
app_bar.rs1use gpui::{
2 AnyElement, App, ElementId, InteractiveElement, IntoElement, ParentElement, RenderOnce, Role,
3 ScrollHandle, SharedString, StatefulInteractiveElement, Styled, TextAlign, Window, div,
4 prelude::FluentBuilder, px,
5};
6use gpui_base::TestSupportExt;
7use gpui_material_foundation::{color, text_style, window_material_theme};
8use gpui_material_theme::{ColorRole, Direction, TypeRole, TypographyEmphasis};
9
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
11pub enum AppBarVariant {
12 #[default]
13 Small,
14 Centered,
15 MediumFlexible,
16 LargeFlexible,
17}
18
19#[derive(IntoElement)]
22pub struct AppBar {
23 id: ElementId,
24 title: SharedString,
25 subtitle: Option<SharedString>,
26 variant: AppBarVariant,
27 navigation: Option<AnyElement>,
28 actions: Option<AnyElement>,
29 search: Option<AnyElement>,
30 scroll: Option<ScrollHandle>,
31 font_scale: f32,
32}
33
34impl AppBar {
35 pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
36 Self {
37 id: id.into(),
38 title: title.into(),
39 subtitle: None,
40 variant: AppBarVariant::Small,
41 navigation: None,
42 actions: None,
43 search: None,
44 scroll: None,
45 font_scale: 1.,
46 }
47 }
48
49 pub fn variant(mut self, variant: AppBarVariant) -> Self {
50 self.variant = variant;
51 self
52 }
53
54 pub fn subtitle(mut self, subtitle: impl Into<SharedString>) -> Self {
55 self.subtitle = Some(subtitle.into());
56 self
57 }
58
59 pub fn navigation(mut self, element: impl IntoElement) -> Self {
60 self.navigation = Some(element.into_any_element());
61 self
62 }
63
64 pub fn actions(mut self, element: impl IntoElement) -> Self {
65 self.actions = Some(element.into_any_element());
66 self
67 }
68
69 pub fn search(mut self, element: impl IntoElement) -> Self {
71 self.search = Some(element.into_any_element());
72 self
73 }
74
75 pub fn track_scroll(mut self, handle: &ScrollHandle) -> Self {
76 self.scroll = Some(handle.clone());
77 self
78 }
79
80 pub fn font_scale(mut self, scale: f32) -> Self {
81 self.font_scale = if scale.is_finite() && scale > 0. {
82 scale
83 } else {
84 1.
85 };
86 self
87 }
88}
89
90impl RenderOnce for AppBar {
91 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
92 let theme = window_material_theme(window, cx);
93 let rtl = theme.layout.direction == Direction::Rtl;
94 let centered = self.variant == AppBarVariant::Centered;
95 let scroll = self
96 .scroll
97 .as_ref()
98 .map_or(0., |scroll| f32::from(-scroll.offset().y))
99 .max(0.);
100 let expansion = match (self.variant, self.subtitle.is_some()) {
102 (AppBarVariant::MediumFlexible, false) => 48.,
103 (AppBarVariant::MediumFlexible, true) => 72.,
104 (AppBarVariant::LargeFlexible, false) => 56.,
105 (AppBarVariant::LargeFlexible, true) => 88.,
106 _ => 0.,
107 } * self.font_scale.max(1.);
108 let remaining = (expansion - scroll).max(0.);
109 let expanded = remaining > 0. && self.search.is_none();
110 let color_for = |role| color(theme.colors.color(role));
111 let title = |role: TypeRole, subtitle_role: TypeRole| {
112 let align = if centered {
113 TextAlign::Center
114 } else if rtl {
115 TextAlign::Right
116 } else {
117 TextAlign::Left
118 };
119 div()
120 .flex()
121 .flex_col()
122 .min_w_0()
123 .text_align(align)
124 .child(
125 text_style(
126 div().truncate(),
127 role.style(TypographyEmphasis::Baseline),
128 self.font_scale,
129 window,
130 cx,
131 )
132 .text_color(color_for(ColorRole::OnSurface))
133 .child(self.title.clone()),
134 )
135 .children(self.subtitle.clone().map(|subtitle| {
136 text_style(
137 div().truncate(),
138 subtitle_role.style(TypographyEmphasis::Baseline),
139 self.font_scale,
140 window,
141 cx,
142 )
143 .text_color(color_for(ColorRole::OnSurfaceVariant))
144 .child(subtitle)
145 }))
146 };
147 let top_title = if expanded && remaining > expansion / 2. {
148 None
149 } else {
150 Some(title(TypeRole::TitleLarge, TypeRole::LabelMedium))
151 };
152 let expanded_title = (expanded && remaining > expansion / 2.).then(|| {
153 title(
154 if self.variant == AppBarVariant::LargeFlexible {
155 TypeRole::DisplaySmall
156 } else {
157 TypeRole::HeadlineMedium
158 },
159 if self.variant == AppBarVariant::LargeFlexible {
160 TypeRole::TitleMedium
161 } else {
162 TypeRole::LabelLarge
163 },
164 )
165 });
166 let background = crate::motion::mix(
167 color_for(ColorRole::Surface),
168 color_for(ColorRole::SurfaceContainer),
169 (scroll / expansion.max(24.)).min(1.),
170 );
171 let has_search = self.search.is_some();
172 let center_slots = centered && !has_search;
173 div()
174 .id(self.id)
175 .test_support()
176 .role(Role::Toolbar)
177 .aria_label(self.title)
178 .w_full()
179 .flex_none()
180 .flex()
181 .flex_col()
182 .bg(background)
183 .child(
184 div()
185 .flex()
186 .items_center()
187 .min_h(px(64.))
188 .px(px(4.))
189 .gap(px(4.))
190 .when(rtl, Styled::flex_row_reverse)
191 .child(
192 div()
193 .flex()
194 .min_w_0()
195 .when(center_slots, |lane| lane.flex_1())
196 .children(self.navigation),
197 )
198 .child(
199 div()
200 .min_w_0()
201 .when_else(center_slots, |lane| lane.w_1_2(), |lane| lane.flex_1())
202 .children(self.search)
203 .children(top_title.filter(|_| !has_search)),
204 )
205 .child(
206 div()
207 .flex()
208 .justify_end()
209 .min_w_0()
210 .when(center_slots, |lane| lane.flex_1())
211 .when(rtl, Styled::flex_row_reverse)
212 .children(self.actions),
213 ),
214 )
215 .when(expanded, |bar| {
216 bar.child(
217 div()
218 .h(px(remaining))
219 .overflow_hidden()
220 .flex()
221 .flex_col()
222 .justify_end()
223 .px(px(16.))
224 .pb(px(4.))
225 .children(expanded_title),
226 )
227 })
228 }
229}