gpui_material_components/navigation/
app_bar.rs1use gpui::{AnyElement, ElementId, Entity, IntoElement, Pixels, ScrollHandle, SharedString};
2use gpui_material_theme::{LogicalEdges, MotionSpec};
3
4mod actions;
5mod render;
6mod scroll;
7mod types;
8
9pub use actions::{AppBarColumn, AppBarItem, AppBarMenuState, AppBarRow};
10pub(super) use scroll::length;
11pub use scroll::{AppBarScrollBehavior, AppBarScrollState};
12pub use types::{AppBarAlignment, AppBarColors, AppBarVariant};
13
14use types::Heading;
15
16#[derive(IntoElement)]
18pub struct AppBar {
19 id: ElementId,
20 title: Heading,
21 subtitle: Heading,
22 variant: AppBarVariant,
23 navigation: Option<AnyElement>,
24 actions: Option<AnyElement>,
25 search: Option<AnyElement>,
26 logo: Option<AnyElement>,
27 second_row: Option<AnyElement>,
28 colors: AppBarColors,
29 padding: Option<LogicalEdges>,
30 collapsed_height: Option<f32>,
31 expanded_height: Option<f32>,
32 scroll: Option<ScrollHandle>,
33 scroll_state: Option<Entity<AppBarScrollState>>,
34 pub(super) scroll_behavior: Option<AppBarScrollBehavior>,
35 pub(super) scroll_enabled: bool,
36 pub(super) scroll_motion: Option<MotionSpec>,
37 pub(super) font_scale: f32,
38}
39
40impl AppBar {
41 pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
43 Self {
44 id: id.into(),
45 title: Heading {
46 text: Some(title.into()),
47 ..Heading::default()
48 },
49 subtitle: Heading::default(),
50 variant: AppBarVariant::Small,
51 navigation: None,
52 actions: None,
53 search: None,
54 logo: None,
55 second_row: None,
56 colors: AppBarColors::default(),
57 padding: None,
58 collapsed_height: None,
59 expanded_height: None,
60 scroll: None,
61 scroll_state: None,
62 scroll_behavior: None,
63 scroll_enabled: true,
64 scroll_motion: None,
65 font_scale: 1.,
66 }
67 }
68
69 pub fn variant(mut self, variant: AppBarVariant) -> Self {
71 self.variant = variant;
72 self
73 }
74
75 pub fn subtitle(mut self, subtitle: impl Into<SharedString>) -> Self {
77 self.subtitle.text = Some(subtitle.into());
78 self.subtitle.content = None;
79 self
80 }
81
82 pub fn title_content(self, content: impl IntoElement + 'static) -> Self {
84 self.title_for_rows(move |_| content)
85 }
86
87 pub fn subtitle_content(self, content: impl IntoElement + 'static) -> Self {
89 self.subtitle_for_rows(move |_| content)
90 }
91
92 pub fn title_for_rows<E: IntoElement>(
94 mut self,
95 build: impl FnOnce(bool) -> E + 'static,
96 ) -> Self {
97 self.title.content = Some(Box::new(move |expanded| build(expanded).into_any_element()));
98 self
99 }
100
101 pub fn subtitle_for_rows<E: IntoElement>(
103 mut self,
104 build: impl FnOnce(bool) -> E + 'static,
105 ) -> Self {
106 self.subtitle.content = Some(Box::new(move |expanded| build(expanded).into_any_element()));
107 self
108 }
109
110 pub fn title_alignment(mut self, alignment: AppBarAlignment) -> Self {
112 self.title.alignment = Some(alignment);
113 self
114 }
115
116 pub fn subtitle_alignment(mut self, alignment: AppBarAlignment) -> Self {
118 self.subtitle.alignment = Some(alignment);
119 self
120 }
121
122 pub fn title_max_lines(mut self, lines: usize) -> Self {
124 self.title.max_lines = Some(lines.max(1));
125 self
126 }
127
128 pub fn subtitle_max_lines(mut self, lines: usize) -> Self {
130 self.subtitle.max_lines = Some(lines.max(1));
131 self
132 }
133
134 pub fn navigation(mut self, element: impl IntoElement) -> Self {
136 self.navigation = Some(element.into_any_element());
137 self
138 }
139
140 pub fn actions(mut self, element: impl IntoElement) -> Self {
142 self.actions = Some(element.into_any_element());
143 self
144 }
145
146 pub fn search(mut self, element: impl IntoElement) -> Self {
148 self.search = Some(element.into_any_element());
149 self
150 }
151
152 pub fn logo(mut self, element: impl IntoElement) -> Self {
154 self.logo = Some(element.into_any_element());
155 self
156 }
157
158 pub fn second_row(mut self, element: impl IntoElement) -> Self {
160 self.second_row = Some(element.into_any_element());
161 self
162 }
163
164 pub fn colors(mut self, colors: AppBarColors) -> Self {
166 self.colors = colors;
167 self
168 }
169
170 pub fn content_padding(mut self, padding: LogicalEdges) -> Self {
172 self.padding = Some(padding.sanitize());
173 self
174 }
175
176 pub fn collapsed_height(mut self, height: Pixels) -> Self {
178 self.collapsed_height = length(height).filter(|height| *height > 0.);
179 self
180 }
181
182 pub fn expanded_height(mut self, height: Pixels) -> Self {
184 self.expanded_height = length(height).filter(|height| *height > 0.);
185 self
186 }
187
188 pub fn track_scroll(mut self, handle: &ScrollHandle) -> Self {
190 self.scroll = Some(handle.clone());
191 self
192 }
193
194 pub fn scroll_state(mut self, state: &Entity<AppBarScrollState>) -> Self {
196 self.scroll_state = Some(state.clone());
197 self
198 }
199
200 pub fn scroll_behavior(mut self, behavior: AppBarScrollBehavior) -> Self {
202 self.scroll_behavior = Some(behavior);
203 self
204 }
205
206 pub fn scroll_enabled(mut self, enabled: bool) -> Self {
208 self.scroll_enabled = enabled;
209 self
210 }
211
212 pub fn scroll_motion(mut self, motion: MotionSpec) -> Self {
214 self.scroll_motion = Some(motion);
215 self
216 }
217
218 pub fn font_scale(mut self, scale: f32) -> Self {
220 self.font_scale = if scale.is_finite() && scale > 0. {
221 scale
222 } else {
223 1.
224 };
225 self
226 }
227}