Skip to main content

gpui_material_components/navigation/
rail.rs

1use std::{collections::HashMap, rc::Rc};
2
3use gpui::{AnyElement, App, ElementId, Entity, IntoElement, Pixels, SharedString, Window, px};
4use gpui_material_theme::LogicalEdges;
5
6use crate::{DialogCloseReason, DialogProperties, DialogState};
7
8use super::items::NavigationItems;
9
10mod render;
11mod state;
12mod types;
13
14pub use state::NavigationRailState;
15pub(in crate::navigation) use types::RailPresentation;
16pub use types::{
17    NavigationRailArrangement, NavigationRailColors, NavigationRailShapes, RailModalOptions,
18    RailMode,
19};
20
21use types::{Closed, Destinations, DismissRequest};
22
23/// Material wide navigation rail. The optional modal uses the side-sheet session.
24#[derive(IntoElement)]
25pub struct NavigationRail {
26    pub(super) id: ElementId,
27    pub(super) items: NavigationItems,
28    pub(super) mode: RailMode,
29    pub(super) state: Option<Entity<NavigationRailState>>,
30    pub(super) title: Option<SharedString>,
31    pub(super) header: Option<AnyElement>,
32    pub(super) fab: Option<AnyElement>,
33    pub(super) expanded_width: Pixels,
34    pub(super) modal: Option<Entity<DialogState>>,
35    pub(super) modal_options: RailModalOptions,
36    pub(super) destination_content: Option<Destinations>,
37    pub(super) dividers: HashMap<SharedString, AnyElement>,
38    pub(super) arrangement: NavigationRailArrangement,
39    pub(super) destination_spacing: Pixels,
40    pub(super) content_padding: Option<LogicalEdges>,
41    pub(super) header_spacing: Pixels,
42    pub(super) expanded_header_top_padding: Pixels,
43    pub(super) shapes: NavigationRailShapes,
44    pub(super) colors: NavigationRailColors,
45    pub(super) dismiss: Option<DismissRequest>,
46    pub(super) closed: Option<Closed>,
47}
48
49impl NavigationRail {
50    /// Creates a collapsed navigation rail with the given element ID and controlled [`NavigationItems`].
51    pub fn new(id: impl Into<ElementId>, items: NavigationItems) -> Self {
52        Self {
53            id: id.into(),
54            items,
55            mode: RailMode::Collapsed,
56            state: None,
57            title: None,
58            header: None,
59            fab: None,
60            expanded_width: px(240.),
61            modal: None,
62            modal_options: RailModalOptions::default(),
63            destination_content: None,
64            dividers: HashMap::new(),
65            arrangement: NavigationRailArrangement::default(),
66            destination_spacing: px(4.),
67            content_padding: None,
68            header_spacing: px(4.),
69            expanded_header_top_padding: px(0.),
70            shapes: NavigationRailShapes::default(),
71            colors: NavigationRailColors::default(),
72            dismiss: None,
73            closed: None,
74        }
75    }
76
77    /// Sets the docked rail mode (default [`RailMode::Collapsed`]). Ignored when [`Self::state`] is bound.
78    pub fn mode(mut self, mode: RailMode) -> Self {
79        self.mode = mode;
80        self
81    }
82
83    /// Uses retained presentation and transitions in place of the supplied mode.
84    pub fn state(mut self, state: &Entity<NavigationRailState>) -> Self {
85        self.state = Some(state.clone());
86        self
87    }
88
89    /// Sets the accessible title used for the rail and its optional modal sheet (default `"Navigation"`).
90    pub fn title(mut self, title: impl Into<SharedString>) -> Self {
91        self.title = Some(title.into());
92        self
93    }
94
95    /// Sets the rail headline displayed in expanded mode and used as the modal sheet's accessible name.
96    pub fn headline(mut self, headline: impl Into<SharedString>) -> Self {
97        self.title = Some(headline.into());
98        self
99    }
100
101    /// Places a custom header element at the top of the rail (such as a menu toggle button).
102    pub fn header(mut self, header: impl IntoElement) -> Self {
103        self.header = Some(header.into_any_element());
104        self
105    }
106
107    /// Places a floating action button below the header.
108    pub fn fab(mut self, fab: impl IntoElement) -> Self {
109        self.fab = Some(fab.into_any_element());
110        self
111    }
112
113    /// Sets the expanded rail width in logical pixels, clamped to `220..=360` px (default 240 px).
114    pub fn expanded_width(mut self, width: Pixels) -> Self {
115        if f32::from(width).is_finite() {
116            self.expanded_width = width.clamp(px(220.), px(360.));
117        }
118        self
119    }
120
121    /// Replaces the standard destination list. The caller owns its navigation semantics.
122    pub fn destination_content<E: IntoElement + 'static>(
123        mut self,
124        build: impl FnOnce(RailMode) -> E + 'static,
125    ) -> Self {
126        self.destination_content = Some(Box::new(move |mode| build(mode).into_any_element()));
127        self
128    }
129
130    /// Places a divider or separator after one standard destination.
131    pub fn divider_after(
132        mut self,
133        destination: impl Into<SharedString>,
134        divider: impl IntoElement,
135    ) -> Self {
136        self.dividers
137            .insert(destination.into(), divider.into_any_element());
138        self
139    }
140
141    /// Sets how destinations are vertically distributed within the rail (default [`NavigationRailArrangement::Start`]).
142    pub fn arrangement(mut self, arrangement: NavigationRailArrangement) -> Self {
143        self.arrangement = arrangement;
144        self
145    }
146
147    /// Sets the vertical gap between destinations in logical pixels (default 4 px).
148    pub fn destination_spacing(mut self, spacing: Pixels) -> Self {
149        self.destination_spacing = length(spacing);
150        self
151    }
152
153    /// Overrides the logical inner padding of the rail container.
154    pub fn content_padding(mut self, padding: LogicalEdges) -> Self {
155        self.content_padding = Some(padding.sanitize());
156        self
157    }
158
159    /// Sets the vertical spacing below the header and FAB slots (default 4 px).
160    pub fn header_spacing(mut self, spacing: Pixels) -> Self {
161        self.header_spacing = length(spacing);
162        self
163    }
164
165    /// Sets extra top padding above the header when the rail is expanded.
166    pub fn expanded_header_top_padding(mut self, padding: Pixels) -> Self {
167        self.expanded_header_top_padding = length(padding);
168        self
169    }
170
171    /// Overrides corner shapes for collapsed, expanded, and modal presentations.
172    pub fn shapes(mut self, shapes: NavigationRailShapes) -> Self {
173        self.shapes = shapes;
174        self
175    }
176
177    /// Overrides container, content, and modal scrim colors.
178    pub fn colors(mut self, colors: NavigationRailColors) -> Self {
179        self.colors = colors;
180        self
181    }
182
183    /// Attaches a caller-owned [`DialogState`] for modal visibility and focus restoration.
184    pub fn modal_state(mut self, state: &Entity<DialogState>) -> Self {
185        self.modal = Some(state.clone());
186        self
187    }
188
189    /// Configures Escape and outside-click dismissal rules for the modal rail sheet.
190    pub fn modal_properties(mut self, properties: DialogProperties) -> Self {
191        self.modal_options.properties = properties;
192        self
193    }
194
195    /// Configures side-sheet presentation and dismissal policy for the modal rail.
196    pub fn modal_options(mut self, options: RailModalOptions) -> Self {
197        self.modal_options = options;
198        self
199    }
200
201    /// Intercepts user-initiated modal dismissal requests; return `false` to veto closing.
202    pub fn on_dismiss_request(
203        mut self,
204        callback: impl Fn(DialogCloseReason, &mut Window, &mut App) -> bool + 'static,
205    ) -> Self {
206        self.dismiss = Some(Rc::new(callback));
207        self
208    }
209
210    /// Invoked once when an accepted modal dismissal closes the rail sheet.
211    pub fn on_closed(
212        mut self,
213        callback: impl Fn(DialogCloseReason, &mut Window, &mut App) + 'static,
214    ) -> Self {
215        self.closed = Some(Rc::new(callback));
216        self
217    }
218}
219
220pub(super) fn length(value: Pixels) -> Pixels {
221    if f32::from(value).is_finite() {
222        value.max(px(0.))
223    } else {
224        px(0.)
225    }
226}