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) 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 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 pub fn mode(mut self, mode: RailMode) -> Self {
79 self.mode = mode;
80 self
81 }
82
83 pub fn state(mut self, state: &Entity<NavigationRailState>) -> Self {
85 self.state = Some(state.clone());
86 self
87 }
88
89 pub fn title(mut self, title: impl Into<SharedString>) -> Self {
91 self.title = Some(title.into());
92 self
93 }
94
95 pub fn headline(mut self, headline: impl Into<SharedString>) -> Self {
97 self.title = Some(headline.into());
98 self
99 }
100
101 pub fn header(mut self, header: impl IntoElement) -> Self {
103 self.header = Some(header.into_any_element());
104 self
105 }
106
107 pub fn fab(mut self, fab: impl IntoElement) -> Self {
109 self.fab = Some(fab.into_any_element());
110 self
111 }
112
113 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 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 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 pub fn arrangement(mut self, arrangement: NavigationRailArrangement) -> Self {
143 self.arrangement = arrangement;
144 self
145 }
146
147 pub fn destination_spacing(mut self, spacing: Pixels) -> Self {
149 self.destination_spacing = length(spacing);
150 self
151 }
152
153 pub fn content_padding(mut self, padding: LogicalEdges) -> Self {
155 self.content_padding = Some(padding.sanitize());
156 self
157 }
158
159 pub fn header_spacing(mut self, spacing: Pixels) -> Self {
161 self.header_spacing = length(spacing);
162 self
163 }
164
165 pub fn expanded_header_top_padding(mut self, padding: Pixels) -> Self {
167 self.expanded_header_top_padding = length(padding);
168 self
169 }
170
171 pub fn shapes(mut self, shapes: NavigationRailShapes) -> Self {
173 self.shapes = shapes;
174 self
175 }
176
177 pub fn colors(mut self, colors: NavigationRailColors) -> Self {
179 self.colors = colors;
180 self
181 }
182
183 pub fn modal_state(mut self, state: &Entity<DialogState>) -> Self {
185 self.modal = Some(state.clone());
186 self
187 }
188
189 pub fn modal_properties(mut self, properties: DialogProperties) -> Self {
191 self.modal_options.properties = properties;
192 self
193 }
194
195 pub fn modal_options(mut self, options: RailModalOptions) -> Self {
197 self.modal_options = options;
198 self
199 }
200
201 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 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}