Skip to main content

gpui_material_components/navigation/
rail.rs

1use gpui::{
2    AnyElement, App, ElementId, Entity, IntoElement, ParentElement, Pixels, RenderOnce,
3    SharedString, Styled, Window, div, prelude::FluentBuilder, px,
4};
5use gpui_material_foundation::{animate, color, window_material_theme};
6use gpui_material_theme::{ColorRole, MotionKind, MotionSpeed, TypeRole};
7
8use crate::{DialogState, Sheet, SheetEdge, SheetMode, Text};
9
10use super::items::{NavigationItems, Presentation};
11
12#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
13pub enum RailMode {
14    #[default]
15    Collapsed,
16    Expanded,
17    Hidden,
18}
19
20/// Material wide navigation rail. An optional modal uses the window's existing
21/// sheet focus scope and restores the trigger on dismissal.
22#[derive(IntoElement)]
23pub struct NavigationRail {
24    id: ElementId,
25    items: NavigationItems,
26    mode: RailMode,
27    title: Option<SharedString>,
28    header: Option<AnyElement>,
29    fab: Option<AnyElement>,
30    expanded_width: Pixels,
31    modal: Option<Entity<DialogState>>,
32}
33
34impl NavigationRail {
35    pub fn new(id: impl Into<ElementId>, items: NavigationItems) -> Self {
36        Self {
37            id: id.into(),
38            items,
39            mode: RailMode::Collapsed,
40            title: None,
41            header: None,
42            fab: None,
43            expanded_width: px(240.),
44            modal: None,
45        }
46    }
47
48    pub fn mode(mut self, mode: RailMode) -> Self {
49        self.mode = mode;
50        self
51    }
52
53    pub fn title(mut self, title: impl Into<SharedString>) -> Self {
54        self.title = Some(title.into());
55        self
56    }
57
58    /// Commands such as the expand/collapse toggle, before destinations.
59    pub fn header(mut self, header: impl IntoElement) -> Self {
60        self.header = Some(header.into_any_element());
61        self
62    }
63
64    pub fn fab(mut self, fab: impl IntoElement) -> Self {
65        self.fab = Some(fab.into_any_element());
66        self
67    }
68
69    pub fn expanded_width(mut self, width: Pixels) -> Self {
70        self.expanded_width = width.clamp(px(220.), px(360.));
71        self
72    }
73
74    /// Uses DialogState::open and DialogState::close to control the modal
75    /// expansion. A closed modal leaves the configured docked or hidden mode.
76    pub fn modal(mut self, state: &Entity<DialogState>) -> Self {
77        self.modal = Some(state.clone());
78        self
79    }
80}
81
82impl RenderOnce for NavigationRail {
83    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
84        let open_modal = self.modal.filter(|state| state.read(cx).is_open());
85        let expanded = open_modal.is_some() || self.mode == RailMode::Expanded;
86        let docked_width = match self.mode {
87            RailMode::Collapsed => px(96.),
88            RailMode::Expanded => self.expanded_width,
89            RailMode::Hidden => px(0.),
90        };
91        let theme = window_material_theme(window, cx);
92        let background = color(theme.colors.color(ColorRole::Surface));
93        let motion = theme.motion.spec(
94            MotionKind::Spatial,
95            MotionSpeed::Default,
96            cx.reduce_motion(),
97        );
98        let width = px(animate(
99            (self.id.clone(), "rail-width"),
100            docked_width.into(),
101            motion,
102            window,
103            cx,
104        ))
105        .max(px(0.));
106        let show_docked = self.mode != RailMode::Hidden;
107        let title = self.title.unwrap_or_else(|| "Navigation".into());
108        let mut docked = div()
109            .w(width)
110            .h_full()
111            .flex_none()
112            .overflow_hidden()
113            .bg(background);
114        if !show_docked && open_modal.is_none() {
115            return docked.into_any_element();
116        }
117        let panel = div()
118            .flex()
119            .flex_col()
120            .h_full()
121            .min_h_0()
122            .gap(px(4.))
123            .when(open_modal.is_none(), |panel| {
124                panel.pt(px(4.)).pb(px(8.)).px(px(4.))
125            })
126            .children(self.header)
127            .children(self.fab)
128            .when(expanded && open_modal.is_none(), |panel| {
129                panel.child(
130                    div()
131                        .px(px(12.))
132                        .child(Text::new(title.clone()).role(TypeRole::TitleMedium).wrap()),
133                )
134            })
135            .child(
136                self.items
137                    .render_row(Presentation::Rail(expanded), window, cx),
138            );
139        if let Some(state) = open_modal {
140            docked = docked.child(
141                Sheet::new(self.id, &state, title, panel)
142                    .mode(SheetMode::Modal)
143                    .edge(SheetEdge::Start)
144                    .extent(self.expanded_width),
145            );
146        } else {
147            docked = docked.child(panel);
148        }
149        docked.into_any_element()
150    }
151}