1use std::{collections::HashMap, rc::Rc};
2
3use gpui::{
4 AnyElement, App, ElementId, Entity, Hsla, IntoElement, RenderOnce, SharedString, Window,
5};
6use gpui_material_foundation::InteractionSource;
7
8use super::rail::{NavigationRailPadding, RailPresentation};
9use super::tabs::TabVariant;
10use crate::{Badge, Icon};
11
12mod render;
13
14use render::RowState;
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum NavigationItemIconPosition {
19 Top,
20 Start,
21}
22
23#[derive(Clone, Copy, Debug, Default, PartialEq)]
25pub struct NavigationItemColors {
26 pub selected_icon: Option<Hsla>,
27 pub selected_label_top: Option<Hsla>,
28 pub selected_label_start: Option<Hsla>,
29 pub selected_indicator: Option<Hsla>,
30 pub unselected_icon: Option<Hsla>,
31 pub unselected_label: Option<Hsla>,
32 pub disabled_icon: Option<Hsla>,
33 pub disabled_label: Option<Hsla>,
34}
35
36pub struct NavigationItem {
38 id: SharedString,
39 label: SharedString,
40 icon: Option<Icon>,
41 icon_position: Option<NavigationItemIconPosition>,
42 label_content: Option<AnyElement>,
43 badge: Option<Badge>,
44 indicator_padding: Option<(NavigationRailPadding, NavigationRailPadding)>,
45 colors: NavigationItemColors,
46 interaction_source: Option<Entity<InteractionSource>>,
47 disabled: bool,
48}
49
50impl NavigationItem {
51 pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
52 Self {
53 id: id.into(),
54 label: label.into(),
55 icon: None,
56 icon_position: None,
57 label_content: None,
58 badge: None,
59 indicator_padding: None,
60 colors: NavigationItemColors::default(),
61 interaction_source: None,
62 disabled: false,
63 }
64 }
65
66 pub fn icon(mut self, icon: Icon) -> Self {
67 self.icon = Some(icon);
68 self
69 }
70
71 pub fn disabled(mut self, disabled: bool) -> Self {
72 self.disabled = disabled;
73 self
74 }
75
76 pub fn icon_position(mut self, position: NavigationItemIconPosition) -> Self {
78 self.icon_position = Some(position);
79 self
80 }
81
82 pub fn label_content(mut self, content: impl IntoElement) -> Self {
84 self.label_content = Some(content.into_any_element());
85 self
86 }
87
88 pub fn badge(mut self, badge: Badge) -> Self {
91 self.badge = Some(badge);
92 self
93 }
94
95 pub fn indicator_padding(
97 mut self,
98 collapsed: NavigationRailPadding,
99 expanded: NavigationRailPadding,
100 ) -> Self {
101 self.indicator_padding = Some((collapsed, expanded));
102 self
103 }
104
105 pub fn colors(mut self, colors: NavigationItemColors) -> Self {
107 self.colors = colors;
108 self
109 }
110
111 pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
113 self.interaction_source = Some(source.clone());
114 self
115 }
116}
117
118type Select = Rc<dyn Fn(&SharedString, &mut Window, &mut App)>;
119
120pub struct NavigationItems {
123 id: ElementId,
124 label: SharedString,
125 items: Vec<NavigationItem>,
126 selected: Option<SharedString>,
127 on_select: Option<Select>,
128 font_scale: f32,
129 state: Option<Entity<RowState>>,
130 rail_dividers: HashMap<SharedString, AnyElement>,
131 retain_roving_stop: bool,
132}
133
134impl NavigationItems {
135 pub fn new(
136 id: impl Into<ElementId>,
137 label: impl Into<SharedString>,
138 items: impl IntoIterator<Item = NavigationItem>,
139 ) -> Self {
140 Self {
141 id: id.into(),
142 label: label.into(),
143 items: items.into_iter().collect(),
144 selected: None,
145 on_select: None,
146 font_scale: 1.,
147 state: None,
148 rail_dividers: HashMap::new(),
149 retain_roving_stop: false,
150 }
151 }
152
153 pub fn selected(mut self, id: impl Into<SharedString>) -> Self {
154 self.selected = Some(id.into());
155 self
156 }
157
158 pub fn on_select(
161 mut self,
162 handler: impl Fn(&SharedString, &mut Window, &mut App) + 'static,
163 ) -> Self {
164 self.on_select = Some(Rc::new(handler));
165 self
166 }
167
168 pub fn font_scale(mut self, scale: f32) -> Self {
169 self.font_scale = if scale.is_finite() && scale > 0. {
170 scale
171 } else {
172 1.
173 };
174 self
175 }
176
177 pub(super) fn with_rail_dividers(
178 mut self,
179 dividers: HashMap<SharedString, AnyElement>,
180 ) -> Self {
181 self.rail_dividers = dividers;
182 self
183 }
184
185 pub(super) fn retain(mut self, window: &mut Window, cx: &mut App) -> Self {
189 if self.state.is_none() {
190 self.state = Some(self.row_state(window, cx));
191 }
192 self
193 }
194
195 pub(super) fn id(&self) -> &ElementId {
196 &self.id
197 }
198
199 pub(super) fn retain_roving_stop(mut self) -> Self {
200 self.retain_roving_stop = true;
201 self
202 }
203
204 fn row_state(&self, window: &mut Window, cx: &mut App) -> Entity<RowState> {
205 window.use_keyed_state((self.id.clone(), "navigation-state"), cx, |_, _| {
206 RowState::default()
207 })
208 }
209}
210
211#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
212pub enum NavigationBarLayout {
213 #[default]
214 Vertical,
215 Horizontal,
216}
217
218#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
220pub enum NavigationBarArrangement {
221 #[default]
222 EqualWeight,
223 Centered,
224}
225
226#[derive(Clone, Copy, Debug, Default, PartialEq)]
228pub struct NavigationBarColors {
229 pub container: Option<Hsla>,
230 pub content: Option<Hsla>,
231}
232
233#[derive(IntoElement)]
235pub struct NavigationBar {
236 items: NavigationItems,
237 layout: NavigationBarLayout,
238 arrangement: NavigationBarArrangement,
239 colors: NavigationBarColors,
240}
241
242impl NavigationBar {
243 pub fn new(items: NavigationItems) -> Self {
244 Self {
245 items,
246 layout: NavigationBarLayout::Vertical,
247 arrangement: NavigationBarArrangement::default(),
248 colors: NavigationBarColors::default(),
249 }
250 }
251
252 pub fn layout(mut self, layout: NavigationBarLayout) -> Self {
253 self.layout = layout;
254 self
255 }
256
257 pub fn arrangement(mut self, arrangement: NavigationBarArrangement) -> Self {
258 self.arrangement = arrangement;
259 self
260 }
261
262 pub fn colors(mut self, colors: NavigationBarColors) -> Self {
263 self.colors = colors;
264 self
265 }
266}
267
268impl RenderOnce for NavigationBar {
269 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
270 self.items.render_row(
271 Presentation::Bar {
272 layout: self.layout,
273 arrangement: self.arrangement,
274 colors: self.colors,
275 },
276 window,
277 cx,
278 )
279 }
280}
281
282#[derive(Clone, Copy, PartialEq)]
283pub(super) enum Presentation {
284 Tabs(TabVariant, bool),
285 Bar {
286 layout: NavigationBarLayout,
287 arrangement: NavigationBarArrangement,
288 colors: NavigationBarColors,
289 },
290 Rail(RailPresentation),
291}
292
293impl Presentation {
294 fn is_tab(self) -> bool {
295 matches!(self, Self::Tabs(..))
296 }
297
298 fn is_rail(self) -> bool {
299 matches!(self, Self::Rail(_))
300 }
301
302 fn horizontal_label(self) -> bool {
303 matches!(
304 self,
305 Self::Bar {
306 layout: NavigationBarLayout::Horizontal,
307 ..
308 } | Self::Rail(RailPresentation { expanded: true, .. })
309 )
310 }
311
312 fn bar_colors(self) -> Option<NavigationBarColors> {
313 match self {
314 Self::Bar { colors, .. } => Some(colors),
315 _ => None,
316 }
317 }
318
319 fn rail_options(self) -> Option<RailPresentation> {
320 match self {
321 Self::Rail(options) => Some(options),
322 _ => None,
323 }
324 }
325
326 fn item_colors(self) -> Option<NavigationBarColors> {
327 match self {
328 Self::Rail(options) => Some(NavigationBarColors {
329 content: options.content,
330 container: None,
331 }),
332 _ => self.bar_colors(),
333 }
334 }
335
336 fn centered(self) -> bool {
337 matches!(
338 self,
339 Self::Bar {
340 arrangement: NavigationBarArrangement::Centered,
341 ..
342 }
343 )
344 }
345
346 fn is_scrollable(self) -> bool {
347 !matches!(self, Self::Tabs(_, false))
348 }
349
350 fn minimum_height(self, has_icons: bool) -> f32 {
351 match self {
352 Self::Tabs(..) if has_icons => 64.,
353 Self::Tabs(..) => 48.,
354 Self::Bar { .. } | Self::Rail(_) => 64.,
355 }
356 }
357}