gpui_material_components/navigation/
rail.rs1use 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#[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) footer: Option<AnyElement>,
34 pub(super) expanded_width: Pixels,
35 pub(super) modal: Option<Entity<DialogState>>,
36 pub(super) modal_options: RailModalOptions,
37 pub(super) destination_content: Option<Destinations>,
38 pub(super) dividers: HashMap<SharedString, AnyElement>,
39 pub(super) arrangement: NavigationRailArrangement,
40 pub(super) destination_spacing: Pixels,
41 pub(super) content_padding: Option<LogicalEdges>,
42 pub(super) header_spacing: Pixels,
43 pub(super) expanded_header_top_padding: Pixels,
44 pub(super) shapes: NavigationRailShapes,
45 pub(super) colors: NavigationRailColors,
46 pub(super) dismiss: Option<DismissRequest>,
47 pub(super) closed: Option<Closed>,
48}
49
50impl NavigationRail {
51 pub fn new(id: impl Into<ElementId>, items: NavigationItems) -> Self {
53 Self {
54 id: id.into(),
55 items,
56 mode: RailMode::Collapsed,
57 state: None,
58 title: None,
59 header: None,
60 fab: None,
61 footer: None,
62 expanded_width: px(240.),
63 modal: None,
64 modal_options: RailModalOptions::default(),
65 destination_content: None,
66 dividers: HashMap::new(),
67 arrangement: NavigationRailArrangement::default(),
68 destination_spacing: px(4.),
69 content_padding: None,
70 header_spacing: px(4.),
71 expanded_header_top_padding: px(0.),
72 shapes: NavigationRailShapes::default(),
73 colors: NavigationRailColors::default(),
74 dismiss: None,
75 closed: None,
76 }
77 }
78
79 pub fn mode(mut self, mode: RailMode) -> Self {
81 self.mode = mode;
82 self
83 }
84
85 pub fn state(mut self, state: &Entity<NavigationRailState>) -> Self {
87 self.state = Some(state.clone());
88 self
89 }
90
91 pub fn title(mut self, title: impl Into<SharedString>) -> Self {
93 self.title = Some(title.into());
94 self
95 }
96
97 pub fn headline(mut self, headline: impl Into<SharedString>) -> Self {
99 self.title = Some(headline.into());
100 self
101 }
102
103 pub fn header(mut self, header: impl IntoElement) -> Self {
105 self.header = Some(header.into_any_element());
106 self
107 }
108
109 pub fn fab(mut self, fab: impl IntoElement) -> Self {
111 self.fab = Some(fab.into_any_element());
112 self
113 }
114
115 pub fn footer(mut self, footer: impl IntoElement) -> Self {
117 self.footer = Some(footer.into_any_element());
118 self
119 }
120
121 pub fn expanded_width(mut self, width: Pixels) -> Self {
123 if f32::from(width).is_finite() {
124 self.expanded_width = width.clamp(px(220.), px(360.));
125 }
126 self
127 }
128
129 pub fn destination_content<E: IntoElement + 'static>(
131 mut self,
132 build: impl FnOnce(RailMode) -> E + 'static,
133 ) -> Self {
134 self.destination_content = Some(Box::new(move |mode| build(mode).into_any_element()));
135 self
136 }
137
138 pub fn divider_after(
140 mut self,
141 destination: impl Into<SharedString>,
142 divider: impl IntoElement,
143 ) -> Self {
144 self.dividers
145 .insert(destination.into(), divider.into_any_element());
146 self
147 }
148
149 pub fn arrangement(mut self, arrangement: NavigationRailArrangement) -> Self {
151 self.arrangement = arrangement;
152 self
153 }
154
155 pub fn destination_spacing(mut self, spacing: Pixels) -> Self {
157 self.destination_spacing = length(spacing);
158 self
159 }
160
161 pub fn content_padding(mut self, padding: LogicalEdges) -> Self {
163 self.content_padding = Some(padding.sanitize());
164 self
165 }
166
167 pub fn header_spacing(mut self, spacing: Pixels) -> Self {
169 self.header_spacing = length(spacing);
170 self
171 }
172
173 pub fn expanded_header_top_padding(mut self, padding: Pixels) -> Self {
175 self.expanded_header_top_padding = length(padding);
176 self
177 }
178
179 pub fn shapes(mut self, shapes: NavigationRailShapes) -> Self {
181 self.shapes = shapes;
182 self
183 }
184
185 pub fn colors(mut self, colors: NavigationRailColors) -> Self {
187 self.colors = colors;
188 self
189 }
190
191 pub fn modal_state(mut self, state: &Entity<DialogState>) -> Self {
193 self.modal = Some(state.clone());
194 self
195 }
196
197 pub fn modal_properties(mut self, properties: DialogProperties) -> Self {
199 self.modal_options.properties = properties;
200 self
201 }
202
203 pub fn modal_options(mut self, options: RailModalOptions) -> Self {
205 self.modal_options = options;
206 self
207 }
208
209 pub fn on_dismiss_request(
211 mut self,
212 callback: impl Fn(DialogCloseReason, &mut Window, &mut App) -> bool + 'static,
213 ) -> Self {
214 self.dismiss = Some(Rc::new(callback));
215 self
216 }
217
218 pub fn on_closed(
220 mut self,
221 callback: impl Fn(DialogCloseReason, &mut Window, &mut App) + 'static,
222 ) -> Self {
223 self.closed = Some(Rc::new(callback));
224 self
225 }
226}
227
228pub(super) fn length(value: Pixels) -> Pixels {
229 if f32::from(value).is_finite() {
230 value.max(px(0.))
231 } else {
232 px(0.)
233 }
234}