gpui_material_components/navigation/
drawer.rs1use std::{collections::HashSet, rc::Rc};
2
3use gpui::{
4 AnyElement, App, ElementId, Entity, Hsla, IntoElement, Pixels, SharedString, Window, px,
5};
6use gpui_material_theme::CornerShape;
7
8use crate::{DialogCloseReason, DialogProperties, DialogState};
9
10use super::items::{NavigationItem, NavigationItems};
11
12mod render;
13
14#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
16pub enum NavigationDrawerMode {
17 #[default]
19 Standard,
20 Modal,
22}
23
24pub enum NavigationDrawerEntry {
26 Item(Box<NavigationItem>),
28 Header(SharedString),
30 Divider,
32}
33
34impl NavigationDrawerEntry {
35 pub fn item(item: NavigationItem) -> Self {
37 Self::Item(Box::new(item))
38 }
39}
40
41impl From<NavigationItem> for NavigationDrawerEntry {
42 fn from(item: NavigationItem) -> Self {
43 Self::Item(Box::new(item))
44 }
45}
46
47pub struct NavigationDrawerSection {
49 pub header: Option<SharedString>,
51 pub items: Vec<NavigationItem>,
53}
54
55impl NavigationDrawerSection {
56 pub fn new(items: impl IntoIterator<Item = NavigationItem>) -> Self {
58 Self {
59 header: None,
60 items: items.into_iter().collect(),
61 }
62 }
63
64 pub fn header(mut self, title: impl Into<SharedString>) -> Self {
66 self.header = Some(title.into());
67 self
68 }
69}
70
71#[derive(Clone, Copy, Debug, Default, PartialEq)]
73pub struct NavigationDrawerColors {
74 pub container: Option<Hsla>,
76 pub content: Option<Hsla>,
78 pub header: Option<Hsla>,
80 pub divider: Option<Hsla>,
82 pub active_indicator: Option<Hsla>,
84 pub active_content: Option<Hsla>,
86 pub inactive_content: Option<Hsla>,
88 pub scrim: Option<Hsla>,
90 pub scrim_opacity: Option<f32>,
92}
93
94pub(super) type DismissRequest = Rc<dyn Fn(DialogCloseReason, &mut Window, &mut App) -> bool>;
95pub(super) type Closed = Rc<dyn Fn(DialogCloseReason, &mut Window, &mut App)>;
96
97#[derive(IntoElement)]
100pub struct NavigationDrawer {
101 pub(super) id: ElementId,
102 pub(super) items: NavigationItems,
103 pub(super) entries: Vec<NavigationDrawerEntry>,
104 pub(super) dividers_before: HashSet<SharedString>,
105 pub(super) mode: NavigationDrawerMode,
106 pub(super) width: Pixels,
107 pub(super) headline: Option<SharedString>,
108 pub(super) header: Option<AnyElement>,
109 pub(super) footer: Option<AnyElement>,
110 pub(super) shape: Option<CornerShape>,
111 pub(super) colors: NavigationDrawerColors,
112 pub(super) modal_state: Option<Entity<DialogState>>,
113 pub(super) modal_properties: DialogProperties,
114 pub(super) on_dismiss_request: Option<DismissRequest>,
115 pub(super) on_closed: Option<Closed>,
116}
117
118impl NavigationDrawer {
119 pub const DEFAULT_WIDTH: Pixels = px(360.);
121
122 pub fn new(id: impl Into<ElementId>, items: NavigationItems) -> Self {
124 Self {
125 id: id.into(),
126 items,
127 entries: Vec::new(),
128 dividers_before: HashSet::new(),
129 mode: NavigationDrawerMode::default(),
130 width: Self::DEFAULT_WIDTH,
131 headline: None,
132 header: None,
133 footer: None,
134 shape: None,
135 colors: NavigationDrawerColors::default(),
136 modal_state: None,
137 modal_properties: DialogProperties::default(),
138 on_dismiss_request: None,
139 on_closed: None,
140 }
141 }
142
143 pub fn entries(mut self, entries: impl IntoIterator<Item = NavigationDrawerEntry>) -> Self {
145 self.entries.extend(entries);
146 self
147 }
148
149 pub fn section(
151 mut self,
152 header: impl Into<SharedString>,
153 items: impl IntoIterator<Item = NavigationItem>,
154 ) -> Self {
155 self.entries
156 .push(NavigationDrawerEntry::Header(header.into()));
157 self.entries
158 .extend(items.into_iter().map(NavigationDrawerEntry::item));
159 self
160 }
161
162 pub fn push_section(mut self, section: NavigationDrawerSection) -> Self {
164 if let Some(header) = section.header {
165 self.entries.push(NavigationDrawerEntry::Header(header));
166 }
167 self.entries
168 .extend(section.items.into_iter().map(NavigationDrawerEntry::item));
169 self
170 }
171
172 pub fn divider(mut self) -> Self {
174 self.entries.push(NavigationDrawerEntry::Divider);
175 self
176 }
177
178 pub fn divider_before(mut self, item_id: impl Into<SharedString>) -> Self {
180 self.dividers_before.insert(item_id.into());
181 self
182 }
183
184 pub fn mode(mut self, mode: NavigationDrawerMode) -> Self {
186 self.mode = mode;
187 self
188 }
189
190 pub fn width(mut self, width: Pixels) -> Self {
192 let value = f32::from(width);
193 if value.is_finite() && value > 0. {
194 self.width = width.max(px(200.));
195 }
196 self
197 }
198
199 pub fn headline(mut self, headline: impl Into<SharedString>) -> Self {
201 self.headline = Some(headline.into());
202 self
203 }
204
205 pub fn header(mut self, header: impl IntoElement) -> Self {
207 self.header = Some(header.into_any_element());
208 self
209 }
210
211 pub fn footer(mut self, footer: impl IntoElement) -> Self {
213 self.footer = Some(footer.into_any_element());
214 self
215 }
216
217 pub fn shape(mut self, shape: CornerShape) -> Self {
219 self.shape = Some(shape);
220 self
221 }
222
223 pub fn colors(mut self, colors: NavigationDrawerColors) -> Self {
225 self.colors = colors;
226 self
227 }
228
229 pub fn modal_state(mut self, state: &Entity<DialogState>) -> Self {
231 self.modal_state = Some(state.clone());
232 self
233 }
234
235 pub fn modal_properties(mut self, properties: DialogProperties) -> Self {
237 self.modal_properties = properties;
238 self
239 }
240
241 pub fn on_dismiss_request(
243 mut self,
244 callback: impl Fn(DialogCloseReason, &mut Window, &mut App) -> bool + 'static,
245 ) -> Self {
246 self.on_dismiss_request = Some(Rc::new(callback));
247 self
248 }
249
250 pub fn on_closed(
252 mut self,
253 callback: impl Fn(DialogCloseReason, &mut Window, &mut App) + 'static,
254 ) -> Self {
255 self.on_closed = Some(Rc::new(callback));
256 self
257 }
258}