gpui-material-components 0.3.2

Material 3 Expressive desktop components for GPUI
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::{collections::HashMap, rc::Rc};

use gpui::{
    AnyElement, App, ElementId, Entity, Hsla, IntoElement, RenderOnce, SharedString, Window,
};
use gpui_material_foundation::InteractionSource;

use super::rail::{NavigationRailPadding, RailPresentation};
use super::tabs::TabVariant;
use crate::{Badge, Icon};

mod render;

use render::RowState;

/// Placement of a navigation item's icon relative to its label.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NavigationItemIconPosition {
    /// Place the icon above the label.
    Top,
    /// Place the icon at the logical start of the label.
    Start,
}

/// Optional pigments for an Expressive navigation destination.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct NavigationItemColors {
    /// Icon color when the destination is selected.
    pub selected_icon: Option<Hsla>,
    /// Label color when selected and the icon is positioned above the label.
    pub selected_label_top: Option<Hsla>,
    /// Label color when selected and the icon is positioned at the logical start.
    pub selected_label_start: Option<Hsla>,
    /// Fill color for the selected destination pill indicator.
    pub selected_indicator: Option<Hsla>,
    /// Icon color when the destination is unselected.
    pub unselected_icon: Option<Hsla>,
    /// Label color when the destination is unselected.
    pub unselected_label: Option<Hsla>,
    /// Icon color when the destination is disabled.
    pub disabled_icon: Option<Hsla>,
    /// Label color when the destination is disabled.
    pub disabled_label: Option<Hsla>,
}

/// One destination or tab with an ID unique within its collection.
pub struct NavigationItem {
    pub(super) id: SharedString,
    pub(super) label: SharedString,
    pub(super) icon: Option<Icon>,
    pub(super) icon_position: Option<NavigationItemIconPosition>,
    pub(super) label_content: Option<AnyElement>,
    pub(super) badge: Option<Badge>,
    pub(super) indicator_padding: Option<(NavigationRailPadding, NavigationRailPadding)>,
    pub(super) colors: NavigationItemColors,
    pub(super) interaction_source: Option<Entity<InteractionSource>>,
    pub(super) disabled: bool,
}

impl NavigationItem {
    /// Creates a navigation destination or tab with a unique ID and visible label.
    pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            icon: None,
            icon_position: None,
            label_content: None,
            badge: None,
            indicator_padding: None,
            colors: NavigationItemColors::default(),
            interaction_source: None,
            disabled: false,
        }
    }

    /// Sets the destination's icon.
    pub fn icon(mut self, icon: Icon) -> Self {
        self.icon = Some(icon);
        self
    }

    /// Disables pointer and keyboard activation for this destination.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Overrides icon placement inherited from the tab, bar, or rail.
    pub fn icon_position(mut self, position: NavigationItemIconPosition) -> Self {
        self.icon_position = Some(position);
        self
    }

    /// Replaces the visible label while retaining the accessible name.
    pub fn label_content(mut self, content: impl IntoElement) -> Self {
        self.label_content = Some(content.into_any_element());
        self
    }

    /// Anchors a standalone badge to the icon, or to the label if there is no icon.
    /// An accessible badge label becomes the destination's description.
    pub fn badge(mut self, badge: Badge) -> Self {
        self.badge = Some(badge);
        self
    }

    /// Sets the rail indicator's logical content padding without shrinking its target.
    pub fn indicator_padding(
        mut self,
        collapsed: NavigationRailPadding,
        expanded: NavigationRailPadding,
    ) -> Self {
        self.indicator_padding = Some((collapsed, expanded));
        self
    }

    /// Sets per-state pigments for a navigation or tab destination.
    pub fn colors(mut self, colors: NavigationItemColors) -> Self {
        self.colors = colors;
        self
    }

    /// Observes the destination using its collection ID and item ID.
    pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
        self.interaction_source = Some(source.clone());
        self
    }
}

pub(super) type Select = Rc<dyn Fn(&SharedString, &mut Window, &mut App)>;

/// Controlled destinations. Use the same collection ID across adaptive
/// presentations to preserve focus on the destination that currently owns it.
pub struct NavigationItems {
    id: ElementId,
    label: SharedString,
    items: Vec<NavigationItem>,
    selected: Option<SharedString>,
    on_select: Option<Select>,
    font_scale: f32,
    state: Option<Entity<RowState>>,
    rail_dividers: HashMap<SharedString, AnyElement>,
    retain_roving_stop: bool,
}

impl NavigationItems {
    /// Creates a controlled navigation collection with an element ID, accessible landmark label, and items.
    pub fn new(
        id: impl Into<ElementId>,
        label: impl Into<SharedString>,
        items: impl IntoIterator<Item = NavigationItem>,
    ) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            items: items.into_iter().collect(),
            selected: None,
            on_select: None,
            font_scale: 1.,
            state: None,
            rail_dividers: HashMap::new(),
            retain_roving_stop: false,
        }
    }

    /// Sets the ID of the currently selected destination.
    pub fn selected(mut self, id: impl Into<SharedString>) -> Self {
        self.selected = Some(id.into());
        self
    }

    /// Reports pointer, keyboard activation and accessibility activation.
    /// Arrow keys and Home/End move focus. The caller commits selection.
    pub fn on_select(
        mut self,
        handler: impl Fn(&SharedString, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_select = Some(Rc::new(handler));
        self
    }

    /// Multiplies item label typography scale by a positive finite factor (default `1.0`).
    pub fn font_scale(mut self, scale: f32) -> Self {
        self.font_scale = if scale.is_finite() && scale > 0. {
            scale
        } else {
            1.
        };
        self
    }

    pub(super) fn with_rail_dividers(
        mut self,
        dividers: HashMap<SharedString, AnyElement>,
    ) -> Self {
        self.rail_dividers = dividers;
        self
    }

    // Resolve the state before the adaptive scaffold puts it under a different
    // element path. Otherwise switching from its bar slot to its rail slot
    // replaces the destination focus handles.
    pub(super) fn retain(mut self, window: &mut Window, cx: &mut App) -> Self {
        if self.state.is_none() {
            self.state = Some(self.row_state(window, cx));
        }
        self
    }

    pub(super) fn id(&self) -> &ElementId {
        &self.id
    }

    #[allow(clippy::type_complexity)]
    pub(super) fn into_drawer_parts(
        self,
    ) -> (
        ElementId,
        SharedString,
        Vec<NavigationItem>,
        Option<SharedString>,
        Option<Select>,
        f32,
    ) {
        (
            self.id,
            self.label,
            self.items,
            self.selected,
            self.on_select,
            self.font_scale,
        )
    }

    pub(super) fn retain_roving_stop(mut self) -> Self {
        self.retain_roving_stop = true;
        self
    }

    fn row_state(&self, window: &mut Window, cx: &mut App) -> Entity<RowState> {
        window.use_keyed_state((self.id.clone(), "navigation-state"), cx, |_, _| {
            RowState::default()
        })
    }
}

/// Icon-and-label orientation for items inside a [`NavigationBar`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum NavigationBarLayout {
    /// Stack the icon above the label.
    #[default]
    Vertical,
    /// Place the icon beside the label at the logical start.
    Horizontal,
}

/// Distribution of the navigation items along the width of the bar.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum NavigationBarArrangement {
    /// Distribute items evenly across the full width of the bar.
    #[default]
    EqualWeight,
    /// Group items together centered along the bar.
    Centered,
}

/// Short navigation bar pigments. Item overrides take precedence over `content`.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct NavigationBarColors {
    /// Background color override for the navigation bar container.
    pub container: Option<Hsla>,
    /// Default foreground color override for bar item content.
    pub content: Option<Hsla>,
}

/// Material Expressive short navigation bar.
#[derive(IntoElement)]
pub struct NavigationBar {
    items: NavigationItems,
    layout: NavigationBarLayout,
    arrangement: NavigationBarArrangement,
    colors: NavigationBarColors,
}

impl NavigationBar {
    /// Creates a short navigation bar displaying the given [`NavigationItems`].
    pub fn new(items: NavigationItems) -> Self {
        Self {
            items,
            layout: NavigationBarLayout::Vertical,
            arrangement: NavigationBarArrangement::default(),
            colors: NavigationBarColors::default(),
        }
    }

    /// Sets the item icon-and-label layout (default [`NavigationBarLayout::Vertical`]).
    pub fn layout(mut self, layout: NavigationBarLayout) -> Self {
        self.layout = layout;
        self
    }

    /// Sets how items are distributed across the bar width (default [`NavigationBarArrangement::EqualWeight`]).
    pub fn arrangement(mut self, arrangement: NavigationBarArrangement) -> Self {
        self.arrangement = arrangement;
        self
    }

    /// Overrides container and default content colors for the bar.
    pub fn colors(mut self, colors: NavigationBarColors) -> Self {
        self.colors = colors;
        self
    }
}

impl RenderOnce for NavigationBar {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        self.items.render_row(
            Presentation::Bar {
                layout: self.layout,
                arrangement: self.arrangement,
                colors: self.colors,
            },
            window,
            cx,
        )
    }
}

#[derive(Clone, Copy, PartialEq)]
pub(super) enum Presentation {
    Tabs(TabVariant, bool),
    Bar {
        layout: NavigationBarLayout,
        arrangement: NavigationBarArrangement,
        colors: NavigationBarColors,
    },
    Rail(RailPresentation),
}

impl Presentation {
    fn is_tab(self) -> bool {
        matches!(self, Self::Tabs(..))
    }

    fn is_rail(self) -> bool {
        matches!(self, Self::Rail(_))
    }

    fn horizontal_label(self) -> bool {
        matches!(
            self,
            Self::Bar {
                layout: NavigationBarLayout::Horizontal,
                ..
            } | Self::Rail(RailPresentation { expanded: true, .. })
        )
    }

    fn bar_colors(self) -> Option<NavigationBarColors> {
        match self {
            Self::Bar { colors, .. } => Some(colors),
            _ => None,
        }
    }

    fn rail_options(self) -> Option<RailPresentation> {
        match self {
            Self::Rail(options) => Some(options),
            _ => None,
        }
    }

    fn item_colors(self) -> Option<NavigationBarColors> {
        match self {
            Self::Rail(options) => Some(NavigationBarColors {
                content: options.content,
                container: None,
            }),
            _ => self.bar_colors(),
        }
    }

    fn centered(self) -> bool {
        matches!(
            self,
            Self::Bar {
                arrangement: NavigationBarArrangement::Centered,
                ..
            }
        )
    }

    fn is_scrollable(self) -> bool {
        !matches!(self, Self::Tabs(_, false))
    }

    fn minimum_height(self, has_icons: bool) -> f32 {
        match self {
            Self::Tabs(..) if has_icons => 64.,
            Self::Tabs(..) => 48.,
            Self::Bar { .. } | Self::Rail(_) => 64.,
        }
    }
}