1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use std::{collections::HashSet, rc::Rc};
use gpui::{
AnyElement, App, ElementId, Entity, Hsla, IntoElement, Pixels, SharedString, Window, px,
};
use gpui_material_theme::CornerShape;
use crate::{DialogCloseReason, DialogProperties, DialogState};
use super::items::{NavigationItem, NavigationItems};
mod render;
/// Presentation mode for a standalone [`NavigationDrawer`].
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum NavigationDrawerMode {
/// Docked side drawer that participates in parent layout.
#[default]
Standard,
/// Modal overlay drawer presented above a scrim with focus trapping.
Modal,
}
/// Ordered entry inside a [`NavigationDrawer`] destination list.
pub enum NavigationDrawerEntry {
/// A selectable navigation destination.
Item(Box<NavigationItem>),
/// A non-interactive section heading label.
Header(SharedString),
/// A horizontal divider separating destination groups.
Divider,
}
impl NavigationDrawerEntry {
/// Creates a destination item entry.
pub fn item(item: NavigationItem) -> Self {
Self::Item(Box::new(item))
}
}
impl From<NavigationItem> for NavigationDrawerEntry {
fn from(item: NavigationItem) -> Self {
Self::Item(Box::new(item))
}
}
/// Convenience section grouping an optional heading with a list of [`NavigationItem`]s.
pub struct NavigationDrawerSection {
/// Optional section heading displayed above the items.
pub header: Option<SharedString>,
/// Ordered navigation items belonging to this section.
pub items: Vec<NavigationItem>,
}
impl NavigationDrawerSection {
/// Creates a drawer section containing the given navigation items and no heading.
pub fn new(items: impl IntoIterator<Item = NavigationItem>) -> Self {
Self {
header: None,
items: items.into_iter().collect(),
}
}
/// Sets the section heading label.
pub fn header(mut self, title: impl Into<SharedString>) -> Self {
self.header = Some(title.into());
self
}
}
/// Per-instance pigment overrides for [`NavigationDrawer`]. Omitted fields use the window theme.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct NavigationDrawerColors {
/// Background color override for the drawer surface.
pub container: Option<Hsla>,
/// Default foreground color override for drawer content.
pub content: Option<Hsla>,
/// Foreground color override for section headings and the drawer headline.
pub header: Option<Hsla>,
/// Color override for horizontal dividers.
pub divider: Option<Hsla>,
/// Fill color override for the selected destination pill indicator.
pub active_indicator: Option<Hsla>,
/// Foreground color override for the selected destination icon and label.
pub active_content: Option<Hsla>,
/// Foreground color override for unselected destination icons and labels.
pub inactive_content: Option<Hsla>,
/// Color override for the modal backdrop scrim.
pub scrim: Option<Hsla>,
/// Opacity override for the modal backdrop scrim.
pub scrim_opacity: Option<f32>,
}
pub(super) type DismissRequest = Rc<dyn Fn(DialogCloseReason, &mut Window, &mut App) -> bool>;
pub(super) type Closed = Rc<dyn Fn(DialogCloseReason, &mut Window, &mut App)>;
/// Full-width Material 3 Navigation Drawer (360 dp default width) with Standard (docked) and
/// Modal (side-sheet) presentations, sections, dividers, and 56 dp pill destination indicators.
#[derive(IntoElement)]
pub struct NavigationDrawer {
pub(super) id: ElementId,
pub(super) items: NavigationItems,
pub(super) entries: Vec<NavigationDrawerEntry>,
pub(super) dividers_before: HashSet<SharedString>,
pub(super) mode: NavigationDrawerMode,
pub(super) width: Pixels,
pub(super) headline: Option<SharedString>,
pub(super) header: Option<AnyElement>,
pub(super) footer: Option<AnyElement>,
pub(super) shape: Option<CornerShape>,
pub(super) colors: NavigationDrawerColors,
pub(super) modal_state: Option<Entity<DialogState>>,
pub(super) modal_properties: DialogProperties,
pub(super) on_dismiss_request: Option<DismissRequest>,
pub(super) on_closed: Option<Closed>,
}
impl NavigationDrawer {
/// Default Material 3 navigation drawer width (360 dp).
pub const DEFAULT_WIDTH: Pixels = px(360.);
/// Creates a standard navigation drawer with the given element ID and controlled [`NavigationItems`].
pub fn new(id: impl Into<ElementId>, items: NavigationItems) -> Self {
Self {
id: id.into(),
items,
entries: Vec::new(),
dividers_before: HashSet::new(),
mode: NavigationDrawerMode::default(),
width: Self::DEFAULT_WIDTH,
headline: None,
header: None,
footer: None,
shape: None,
colors: NavigationDrawerColors::default(),
modal_state: None,
modal_properties: DialogProperties::default(),
on_dismiss_request: None,
on_closed: None,
}
}
/// Appends ordered destination, header, or divider entries to the drawer list.
pub fn entries(mut self, entries: impl IntoIterator<Item = NavigationDrawerEntry>) -> Self {
self.entries.extend(entries);
self
}
/// Appends a section heading followed by its navigation items.
pub fn section(
mut self,
header: impl Into<SharedString>,
items: impl IntoIterator<Item = NavigationItem>,
) -> Self {
self.entries
.push(NavigationDrawerEntry::Header(header.into()));
self.entries
.extend(items.into_iter().map(NavigationDrawerEntry::item));
self
}
/// Appends a [`NavigationDrawerSection`] with an optional header and its items.
pub fn push_section(mut self, section: NavigationDrawerSection) -> Self {
if let Some(header) = section.header {
self.entries.push(NavigationDrawerEntry::Header(header));
}
self.entries
.extend(section.items.into_iter().map(NavigationDrawerEntry::item));
self
}
/// Appends a horizontal divider entry to the ordered entries list.
pub fn divider(mut self) -> Self {
self.entries.push(NavigationDrawerEntry::Divider);
self
}
/// Inserts a horizontal divider immediately before the destination with matching `item_id`.
pub fn divider_before(mut self, item_id: impl Into<SharedString>) -> Self {
self.dividers_before.insert(item_id.into());
self
}
/// Chooses Standard (docked, default) or Modal (side-sheet overlay) mode.
pub fn mode(mut self, mode: NavigationDrawerMode) -> Self {
self.mode = mode;
self
}
/// Sets the drawer width in logical pixels (defaults to 360 dp, clamped to viewport).
pub fn width(mut self, width: Pixels) -> Self {
let value = f32::from(width);
if value.is_finite() && value > 0. {
self.width = width.max(px(200.));
}
self
}
/// Sets the drawer title displayed in Standard mode and used as the modal sheet's accessible name.
pub fn headline(mut self, headline: impl Into<SharedString>) -> Self {
self.headline = Some(headline.into());
self
}
/// Supplies custom header content above the destination list.
pub fn header(mut self, header: impl IntoElement) -> Self {
self.header = Some(header.into_any_element());
self
}
/// Supplies custom footer content anchored beneath the scrolling destination list.
pub fn footer(mut self, footer: impl IntoElement) -> Self {
self.footer = Some(footer.into_any_element());
self
}
/// Overrides the container corner shape.
pub fn shape(mut self, shape: CornerShape) -> Self {
self.shape = Some(shape);
self
}
/// Overrides container, content, header, divider, indicator, and modal scrim colors independently.
pub fn colors(mut self, colors: NavigationDrawerColors) -> Self {
self.colors = colors;
self
}
/// Attaches a caller-owned `DialogState` for `NavigationDrawerMode::Modal`.
pub fn modal_state(mut self, state: &Entity<DialogState>) -> Self {
self.modal_state = Some(state.clone());
self
}
/// Configures Escape and outside-click dismissal rules in Modal mode.
pub fn modal_properties(mut self, properties: DialogProperties) -> Self {
self.modal_properties = properties;
self
}
/// Intercepts user-initiated modal dismissal requests; return `false` to veto closing.
pub fn on_dismiss_request(
mut self,
callback: impl Fn(DialogCloseReason, &mut Window, &mut App) -> bool + 'static,
) -> Self {
self.on_dismiss_request = Some(Rc::new(callback));
self
}
/// Invoked once when an accepted modal dismissal closes the drawer.
pub fn on_closed(
mut self,
callback: impl Fn(DialogCloseReason, &mut Window, &mut App) + 'static,
) -> Self {
self.on_closed = Some(Rc::new(callback));
self
}
}