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]
18 Standard,
19 Modal,
20}
21
22pub enum NavigationDrawerEntry {
24 Item(Box<NavigationItem>),
25 Header(SharedString),
26 Divider,
27}
28
29impl NavigationDrawerEntry {
30 pub fn item(item: NavigationItem) -> Self {
32 Self::Item(Box::new(item))
33 }
34}
35
36impl From<NavigationItem> for NavigationDrawerEntry {
37 fn from(item: NavigationItem) -> Self {
38 Self::Item(Box::new(item))
39 }
40}
41
42pub struct NavigationDrawerSection {
44 pub header: Option<SharedString>,
45 pub items: Vec<NavigationItem>,
46}
47
48impl NavigationDrawerSection {
49 pub fn new(items: impl IntoIterator<Item = NavigationItem>) -> Self {
50 Self {
51 header: None,
52 items: items.into_iter().collect(),
53 }
54 }
55
56 pub fn header(mut self, title: impl Into<SharedString>) -> Self {
57 self.header = Some(title.into());
58 self
59 }
60}
61
62#[derive(Clone, Copy, Debug, Default, PartialEq)]
64pub struct NavigationDrawerColors {
65 pub container: Option<Hsla>,
66 pub content: Option<Hsla>,
67 pub header: Option<Hsla>,
68 pub divider: Option<Hsla>,
69 pub active_indicator: Option<Hsla>,
70 pub active_content: Option<Hsla>,
71 pub inactive_content: Option<Hsla>,
72 pub scrim: Option<Hsla>,
73 pub scrim_opacity: Option<f32>,
74}
75
76pub(super) type DismissRequest = Rc<dyn Fn(DialogCloseReason, &mut Window, &mut App) -> bool>;
77pub(super) type Closed = Rc<dyn Fn(DialogCloseReason, &mut Window, &mut App)>;
78
79#[derive(IntoElement)]
82pub struct NavigationDrawer {
83 pub(super) id: ElementId,
84 pub(super) items: NavigationItems,
85 pub(super) entries: Vec<NavigationDrawerEntry>,
86 pub(super) dividers_before: HashSet<SharedString>,
87 pub(super) mode: NavigationDrawerMode,
88 pub(super) width: Pixels,
89 pub(super) headline: Option<SharedString>,
90 pub(super) header: Option<AnyElement>,
91 pub(super) footer: Option<AnyElement>,
92 pub(super) shape: Option<CornerShape>,
93 pub(super) colors: NavigationDrawerColors,
94 pub(super) modal_state: Option<Entity<DialogState>>,
95 pub(super) modal_properties: DialogProperties,
96 pub(super) on_dismiss_request: Option<DismissRequest>,
97 pub(super) on_closed: Option<Closed>,
98}
99
100impl NavigationDrawer {
101 pub const DEFAULT_WIDTH: Pixels = px(360.);
102
103 pub fn new(id: impl Into<ElementId>, items: NavigationItems) -> Self {
104 Self {
105 id: id.into(),
106 items,
107 entries: Vec::new(),
108 dividers_before: HashSet::new(),
109 mode: NavigationDrawerMode::default(),
110 width: Self::DEFAULT_WIDTH,
111 headline: None,
112 header: None,
113 footer: None,
114 shape: None,
115 colors: NavigationDrawerColors::default(),
116 modal_state: None,
117 modal_properties: DialogProperties::default(),
118 on_dismiss_request: None,
119 on_closed: None,
120 }
121 }
122
123 pub fn entries(mut self, entries: impl IntoIterator<Item = NavigationDrawerEntry>) -> Self {
125 self.entries.extend(entries);
126 self
127 }
128
129 pub fn section(
131 mut self,
132 header: impl Into<SharedString>,
133 items: impl IntoIterator<Item = NavigationItem>,
134 ) -> Self {
135 self.entries
136 .push(NavigationDrawerEntry::Header(header.into()));
137 self.entries
138 .extend(items.into_iter().map(NavigationDrawerEntry::item));
139 self
140 }
141
142 pub fn push_section(mut self, section: NavigationDrawerSection) -> Self {
144 if let Some(header) = section.header {
145 self.entries.push(NavigationDrawerEntry::Header(header));
146 }
147 self.entries
148 .extend(section.items.into_iter().map(NavigationDrawerEntry::item));
149 self
150 }
151
152 pub fn divider(mut self) -> Self {
154 self.entries.push(NavigationDrawerEntry::Divider);
155 self
156 }
157
158 pub fn divider_before(mut self, item_id: impl Into<SharedString>) -> Self {
160 self.dividers_before.insert(item_id.into());
161 self
162 }
163
164 pub fn mode(mut self, mode: NavigationDrawerMode) -> Self {
166 self.mode = mode;
167 self
168 }
169
170 pub fn width(mut self, width: Pixels) -> Self {
172 let value = f32::from(width);
173 if value.is_finite() && value > 0. {
174 self.width = width.max(px(200.));
175 }
176 self
177 }
178
179 pub fn headline(mut self, headline: impl Into<SharedString>) -> Self {
181 self.headline = Some(headline.into());
182 self
183 }
184
185 pub fn header(mut self, header: impl IntoElement) -> Self {
187 self.header = Some(header.into_any_element());
188 self
189 }
190
191 pub fn footer(mut self, footer: impl IntoElement) -> Self {
193 self.footer = Some(footer.into_any_element());
194 self
195 }
196
197 pub fn shape(mut self, shape: CornerShape) -> Self {
199 self.shape = Some(shape);
200 self
201 }
202
203 pub fn colors(mut self, colors: NavigationDrawerColors) -> Self {
205 self.colors = colors;
206 self
207 }
208
209 pub fn modal_state(mut self, state: &Entity<DialogState>) -> Self {
211 self.modal_state = Some(state.clone());
212 self
213 }
214
215 pub fn modal_properties(mut self, properties: DialogProperties) -> Self {
217 self.modal_properties = properties;
218 self
219 }
220
221 pub fn on_dismiss_request(
223 mut self,
224 callback: impl Fn(DialogCloseReason, &mut Window, &mut App) -> bool + 'static,
225 ) -> Self {
226 self.on_dismiss_request = Some(Rc::new(callback));
227 self
228 }
229
230 pub fn on_closed(
232 mut self,
233 callback: impl Fn(DialogCloseReason, &mut Window, &mut App) + 'static,
234 ) -> Self {
235 self.on_closed = Some(Rc::new(callback));
236 self
237 }
238}