gpui_material_components/navigation/app_bar/
render.rs1use gpui::{
2 App, Div, InteractiveElement, IntoElement, ParentElement, RenderOnce, Role,
3 StatefulInteractiveElement, Styled, Window, div, prelude::FluentBuilder, px,
4};
5use gpui_base::TestSupportExt;
6use gpui_material_foundation::{animate, color, current_material_theme, resolve_edges};
7use gpui_material_theme::{
8 ColorRole, Direction, LogicalEdges, MotionKind, MotionSpec, MotionSpeed, TypeRole,
9};
10
11use super::{
12 AppBar, AppBarAlignment, AppBarScrollState, AppBarVariant,
13 scroll::length,
14 types::{HeadingStyle, aligned_content},
15};
16
17fn apply_edges(row: Div, edges: LogicalEdges, direction: Direction) -> Div {
18 let resolved = resolve_edges(edges, direction);
19 row.pl(resolved.left)
20 .pr(resolved.right)
21 .pt(resolved.top)
22 .pb(resolved.bottom)
23}
24
25impl AppBar {
26 fn scroll_position(&self, limit: f32, window: &mut Window, cx: &mut App) -> (f32, f32) {
27 if self.scroll.is_none() && self.scroll_state.is_none() {
28 return (0., 0.);
29 }
30 let state = self.scroll_state.clone().unwrap_or_else(|| {
31 window.use_keyed_state((self.id.clone(), "app-bar-scroll"), cx, |_, _| {
32 AppBarScrollState::new()
33 })
34 });
35 let content_offset = self
36 .scroll
37 .as_ref()
38 .map(|handle| length(-handle.offset().y).unwrap_or(0.));
39 let position = state.update(cx, |state, _| {
40 state.prepare(
41 content_offset,
42 limit,
43 self.scroll_behavior,
44 self.scroll_enabled,
45 )
46 });
47 let motion = if position.settling && !cx.reduce_motion() {
48 self.scroll_motion.unwrap_or_else(|| {
49 current_material_theme(window, cx).motion.spec(
50 MotionKind::Spatial,
51 MotionSpeed::Default,
52 false,
53 )
54 })
55 } else {
56 MotionSpec::Instant
57 };
58 let fraction = animate(
59 (self.id.clone(), "app-bar-height"),
60 position.target,
61 motion,
62 window,
63 cx,
64 )
65 .clamp(0., 1.);
66 if state.update(cx, |state, _| state.display(position, fraction))
67 && self.scroll_state.is_some()
68 {
69 let state = state.downgrade();
70 window.defer(cx, move |_, cx| {
71 let _ = state.update(cx, |_, cx| cx.notify());
72 });
73 }
74 (fraction, position.overlap)
75 }
76}
77
78impl RenderOnce for AppBar {
79 fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
80 let theme = current_material_theme(window, cx);
81 let collapsed = self
82 .collapsed_height
83 .unwrap_or_else(|| theme.layout.density.interactive_size(64.));
84 let two_rows = self.variant.two_rows();
85 let (expanded_title_role, expanded_subtitle_role) = self.variant.expanded_roles();
86 let text_scale = theme.typography.text_scale();
87 let default_expansion = if two_rows {
88 (self.variant.expansion(self.subtitle.has_content())
89 + self.title.additional_height(expanded_title_role)
90 + self.subtitle.additional_height(expanded_subtitle_role))
91 * (self.font_scale * text_scale).max(1.)
92 } else {
93 0.
94 };
95 let limit = self
96 .expanded_height
97 .map_or(default_expansion, |height| (height - collapsed).max(0.));
98 let (fraction, overlap) = self.scroll_position(limit, window, cx);
99 let remaining = limit * (1. - fraction);
100 let direction = theme.layout.direction;
101 let rtl = direction == Direction::Rtl;
102 let centered = self.variant == AppBarVariant::Centered;
103 let has_search = self.search.is_some();
104 let expanded_row = two_rows && remaining > 0. && !has_search;
105 let expanded_heading = expanded_row && remaining > limit / 2.;
106 let default_alignment = if centered {
107 AppBarAlignment::Center
108 } else {
109 AppBarAlignment::Start
110 };
111 let role = |role| color(theme.colors.color(role));
112 let surface = self
113 .colors
114 .container
115 .unwrap_or_else(|| role(ColorRole::Surface));
116 let scrolled = self
117 .colors
118 .scrolled_container
119 .unwrap_or_else(|| role(ColorRole::SurfaceContainer));
120 let background = match overlap {
121 0. => surface,
122 1. => scrolled,
123 _ => crate::control_style::mix_color(surface, scrolled, overlap),
124 };
125 let title_color = self
126 .colors
127 .title
128 .unwrap_or_else(|| role(ColorRole::OnSurface));
129 let subtitle_color = self
130 .colors
131 .subtitle
132 .unwrap_or_else(|| role(ColorRole::OnSurfaceVariant));
133 let mut navigation = div()
134 .flex()
135 .items_center()
136 .min_w_0()
137 .gap(px(4.))
138 .when(rtl, Styled::flex_row_reverse);
139 if let Some(navigation_color) = self.colors.navigation {
140 navigation = navigation.text_color(navigation_color);
141 }
142 let search_logo = has_search.then(|| self.logo.take()).flatten();
143 navigation = navigation.children(self.navigation).children(search_logo);
144 let mut actions = div()
145 .flex()
146 .justify_end()
147 .min_w_0()
148 .when(rtl, Styled::flex_row_reverse)
149 .children(self.actions);
150 if let Some(actions_color) = self.colors.actions {
151 actions = actions.text_color(actions_color);
152 }
153 let label = self.title.text.clone().unwrap_or_default();
154 let title_alignment = self
155 .title
156 .alignment
157 .unwrap_or(default_alignment)
158 .resolve(direction);
159 let heading =
160 if has_search {
161 None
162 } else if !two_rows && self.logo.is_some() {
163 Some(
164 aligned_content(title_alignment)
165 .text_color(title_color)
166 .children(self.logo.take()),
167 )
168 } else {
169 let heading_style = HeadingStyle {
170 direction,
171 alignment: default_alignment,
172 font_scale: self.font_scale,
173 color: title_color,
174 };
175 let (title_role, subtitle_role) = if expanded_heading {
176 (expanded_title_role, expanded_subtitle_role)
177 } else {
178 (TypeRole::TitleLarge, TypeRole::LabelMedium)
179 };
180 Some(
181 div()
182 .flex()
183 .flex_col()
184 .min_w_0()
185 .children(expanded_heading.then(|| self.logo.take()).flatten().map(
186 |logo| {
187 aligned_content(title_alignment)
188 .text_color(title_color)
189 .child(logo)
190 },
191 ))
192 .children(self.title.render(
193 expanded_heading,
194 title_role,
195 heading_style,
196 window,
197 cx,
198 ))
199 .children(self.subtitle.render(
200 expanded_heading,
201 subtitle_role,
202 HeadingStyle {
203 color: subtitle_color,
204 ..heading_style
205 },
206 window,
207 cx,
208 )),
209 )
210 };
211 let (top_heading, lower_heading) = if expanded_heading {
212 (None, heading)
213 } else {
214 (heading, None)
215 };
216 let center_slots = centered && !has_search;
217 let row = div()
218 .flex()
219 .items_center()
220 .min_h(px(if two_rows || has_search {
221 collapsed
222 } else {
223 collapsed + remaining
224 }))
225 .gap(px(4.))
226 .when(rtl, Styled::flex_row_reverse)
227 .child(navigation.when(center_slots, |lane| lane.flex_1()))
228 .child(
229 div()
230 .min_w_0()
231 .when_else(center_slots, |lane| lane.w_1_2(), |lane| lane.flex_1())
232 .children(self.search)
233 .children(top_heading),
234 )
235 .child(actions.when(center_slots, |lane| lane.flex_1()));
236 let primary = apply_edges(
237 row,
238 self.padding.unwrap_or(LogicalEdges::symmetric(4., 0.)),
239 direction,
240 );
241 let lower = expanded_row.then(|| {
242 let body = div()
243 .h(px(remaining))
244 .overflow_hidden()
245 .flex()
246 .flex_col()
247 .justify_end()
248 .children(lower_heading)
249 .children(
250 self.second_row
251 .filter(|_| self.variant == AppBarVariant::TwoRows && expanded_heading),
252 );
253 apply_edges(
254 body,
255 self.padding.unwrap_or(LogicalEdges::new(0., 16., 4., 16.)),
256 direction,
257 )
258 });
259 div()
260 .id(self.id)
261 .test_support()
262 .role(Role::Toolbar)
263 .aria_label(label)
264 .w_full()
265 .flex_none()
266 .flex()
267 .flex_col()
268 .bg(background)
269 .child(primary)
270 .children(lower)
271 }
272}