Skip to main content

gpui_material_components/navigation/
app_bar.rs

1use 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/// Top app bar with caller-owned navigation, headings, actions, and optional search.
17#[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    /// Creates a small top app bar with the given element ID and title.
42    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    /// Sets the app bar layout variant (default [`AppBarVariant::Small`]).
70    pub fn variant(mut self, variant: AppBarVariant) -> Self {
71        self.variant = variant;
72        self
73    }
74
75    /// Sets the secondary subtitle text displayed beneath the title.
76    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    /// Replaces the visual title while keeping the constructor's accessible name.
83    pub fn title_content(self, content: impl IntoElement + 'static) -> Self {
84        self.title_for_rows(move |_| content)
85    }
86
87    /// Replaces the subtitle with custom content.
88    pub fn subtitle_content(self, content: impl IntoElement + 'static) -> Self {
89        self.subtitle_for_rows(move |_| content)
90    }
91
92    /// Builds the title for the currently displayed row.
93    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    /// Builds the subtitle for the currently displayed row.
102    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    /// Overrides the logical horizontal alignment of the title.
111    pub fn title_alignment(mut self, alignment: AppBarAlignment) -> Self {
112        self.title.alignment = Some(alignment);
113        self
114    }
115
116    /// Overrides the logical horizontal alignment of the subtitle.
117    pub fn subtitle_alignment(mut self, alignment: AppBarAlignment) -> Self {
118        self.subtitle.alignment = Some(alignment);
119        self
120    }
121
122    /// Sets the maximum line count for plain title text (clamped to at least 1).
123    pub fn title_max_lines(mut self, lines: usize) -> Self {
124        self.title.max_lines = Some(lines.max(1));
125        self
126    }
127
128    /// Sets the maximum line count for plain subtitle text (clamped to at least 1).
129    pub fn subtitle_max_lines(mut self, lines: usize) -> Self {
130        self.subtitle.max_lines = Some(lines.max(1));
131        self
132    }
133
134    /// Places a leading navigation control (such as a back or menu button) at the start of the bar.
135    pub fn navigation(mut self, element: impl IntoElement) -> Self {
136        self.navigation = Some(element.into_any_element());
137        self
138    }
139
140    /// Places trailing action controls (such as an [`AppBarRow`]) at the end of the bar.
141    pub fn actions(mut self, element: impl IntoElement) -> Self {
142        self.actions = Some(element.into_any_element());
143        self
144    }
145
146    /// Supplies the search control that replaces the heading.
147    pub fn search(mut self, element: impl IntoElement) -> Self {
148        self.search = Some(element.into_any_element());
149        self
150    }
151
152    /// Places a brand logo element alongside the title.
153    pub fn logo(mut self, element: impl IntoElement) -> Self {
154        self.logo = Some(element.into_any_element());
155        self
156    }
157
158    /// Adds presentation content below the expanded heading in TwoRows.
159    pub fn second_row(mut self, element: impl IntoElement) -> Self {
160        self.second_row = Some(element.into_any_element());
161        self
162    }
163
164    /// Overrides container, heading, navigation, and action colors.
165    pub fn colors(mut self, colors: AppBarColors) -> Self {
166        self.colors = colors;
167        self
168    }
169
170    /// Sets explicit logical padding applied to each app bar row.
171    pub fn content_padding(mut self, padding: LogicalEdges) -> Self {
172        self.padding = Some(padding.sanitize());
173        self
174    }
175
176    /// Overrides the collapsed height of the top row in logical pixels (default 64 px).
177    pub fn collapsed_height(mut self, height: Pixels) -> Self {
178        self.collapsed_height = length(height).filter(|height| *height > 0.);
179        self
180    }
181
182    /// Overrides the fully expanded height of a two-row bar in logical pixels.
183    pub fn expanded_height(mut self, height: Pixels) -> Self {
184        self.expanded_height = length(height).filter(|height| *height > 0.);
185        self
186    }
187
188    /// Links a GPUI [`ScrollHandle`] to drive scroll-under color and collapse behavior.
189    pub fn track_scroll(mut self, handle: &ScrollHandle) -> Self {
190        self.scroll = Some(handle.clone());
191        self
192    }
193
194    /// Binds a caller-owned [`AppBarScrollState`] entity for reading or controlling scroll collapse.
195    pub fn scroll_state(mut self, state: &Entity<AppBarScrollState>) -> Self {
196        self.scroll_state = Some(state.clone());
197        self
198    }
199
200    /// Sets how scrolling affects the app bar's height (default [`AppBarScrollBehavior::Pinned`]).
201    pub fn scroll_behavior(mut self, behavior: AppBarScrollBehavior) -> Self {
202        self.scroll_behavior = Some(behavior);
203        self
204    }
205
206    /// Enables or disables scroll-driven collapse updates (default `true`).
207    pub fn scroll_enabled(mut self, enabled: bool) -> Self {
208        self.scroll_enabled = enabled;
209        self
210    }
211
212    /// Overrides motion when the caller asks the linked scroll state to settle.
213    pub fn scroll_motion(mut self, motion: MotionSpec) -> Self {
214        self.scroll_motion = Some(motion);
215        self
216    }
217
218    /// Multiplies the heading typography scale by a positive finite factor (default `1.0`).
219    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}