gpui-m3 0.2.0

Material 3 Expressive components for GPUI: shape morphing, spring motion and dynamic color
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Navigation: drawer rows, navigation rail items and stretching icon tabs.

use std::rc::Rc;

use gpui::{
    AnyElement, App, ClickEvent, ElementId, FontWeight, InteractiveElement, IntoElement,
    ParentElement, RenderOnce, SharedString, StatefulInteractiveElement, Styled, Window, div, px,
    relative,
};

use super::{ChangeHandler, ClickHandler, Icon, RoundedExt, all};
use crate::interaction::{interactive, state_layer, use_interaction};
use crate::motion::{Spring, animate_color, animate_f32, animate_on_activate, key};
use crate::shapes::Tone;
use crate::theme::{Theme, alpha, font_family};

/// Navigation row (the sidebar in the reference design). The selected indicator starts at the
/// center and stretches with the spring.
#[derive(IntoElement)]
pub struct NavItem {
    id: ElementId,
    label: SharedString,
    icon: Option<SharedString>,
    leading: Option<AnyElement>,
    trailing: Option<AnyElement>,
    selected: bool,
    dense: bool,
    muted: bool,
    emphasized: bool,
    large: bool,
    tone: Option<Tone>,
    meta: Option<SharedString>,
    group: Option<SharedString>,
    on_click: Option<ClickHandler>,
}

impl NavItem {
    pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            icon: None,
            leading: None,
            trailing: None,
            selected: false,
            dense: false,
            muted: false,
            emphasized: false,
            large: false,
            tone: None,
            meta: None,
            group: None,
            on_click: None,
        }
    }

    /// Unread row: heavier label in the full text color.
    pub fn emphasized(mut self, emphasized: bool) -> Self {
        self.emphasized = emphasized;
        self
    }

    /// Short trailing text in the outline color (a time, a count). It gives way to
    /// [`NavItem::trailing`] while the row is hovered.
    pub fn meta(mut self, meta: impl Into<SharedString>) -> Self {
        self.meta = Some(meta.into());
        self
    }

    /// Large row (52 px, 26 px corners), like settings tabs; the icon pops when selected.
    pub fn large(mut self, large: bool) -> Self {
        self.large = large;
        self
    }

    /// Selected indicator in a tonal container instead of the secondary container.
    pub fn tone(mut self, tone: Tone) -> Self {
        self.tone = Some(tone);
        self
    }

    pub fn icon(mut self, icon: impl Into<SharedString>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Leading element instead of the icon (e.g. an [`super::Avatar`]).
    pub fn leading(mut self, el: impl IntoElement) -> Self {
        self.leading = Some(el.into_any_element());
        self
    }

    /// Trailing element (e.g. a [`super::Badge`]).
    pub fn trailing(mut self, el: impl IntoElement) -> Self {
        self.trailing = Some(el.into_any_element());
        self
    }

    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }

    /// Dimmed label (archived or inactive rows). Ignored while selected.
    pub fn muted(mut self, muted: bool) -> Self {
        self.muted = muted;
        self
    }

    /// Names the row as a GPUI hover group, so trailing elements can use
    /// `group_hover(name, ..)` to appear on hover.
    pub fn group(mut self, name: impl Into<SharedString>) -> Self {
        self.group = Some(name.into());
        self
    }

    /// Conversation row (30 px, 13 px text).
    pub fn dense(mut self, dense: bool) -> Self {
        self.dense = dense;
        self
    }

    pub fn on_click(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
        self.on_click = Some(Rc::new(f));
        self
    }
}

impl RenderOnce for NavItem {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let s = *Theme::of(cx);
        let state = use_interaction(&self.id, window, cx);
        let hovered = state.read(cx).hovered;
        let (height, font) = if self.large {
            (52., 14.)
        } else if self.dense {
            (30., 13.)
        } else {
            (34., 13.5)
        };
        let (indicator_bg, selected_fg) = match self.tone {
            Some(tone) => tone.colors(&s),
            None => (s.secondary_container, s.on_secondary_container),
        };
        let radius = px(height / 2.);
        let [grow, opacity] = animate_on_activate(
            key(&self.id, "indicator"),
            Spring::SPATIAL_FAST,
            self.selected,
            [0.35, 0.],
            [1., 1.],
            window,
            cx,
        );
        let fg = if self.selected {
            selected_fg
        } else if self.emphasized {
            s.on_surface
        } else if self.muted {
            s.on_surface_variant
        } else {
            s.on_surface
        };
        let fg = animate_color(key(&self.id, "fg"), Spring::EFFECTS, fg, window, cx);
        let icon_color = if self.selected {
            selected_fg
        } else {
            s.on_surface_variant
        };
        let layer = state_layer(&self.id, &state, all(radius), s.on_surface, window, cx);

        let mut row = div()
            .id(self.id.clone())
            .relative()
            .flex()
            .items_center()
            .gap(px(10.))
            .w_full()
            .h(px(height))
            .px(px(12.))
            .rounded(radius)
            .text_color(fg)
            .font_family(font_family())
            .text_size(px(font))
            .font_weight(FontWeight(if self.selected {
                700.
            } else if self.emphasized {
                650.
            } else {
                500.
            }));
        if let Some(group) = self.group {
            row = row.group(group);
        }
        if self.selected {
            row = row.child(
                div()
                    .absolute()
                    .top_0()
                    .h_full()
                    .left(relative((1. - grow) / 2.))
                    .w(relative(grow.max(0.)))
                    .rounded(radius)
                    .bg(alpha(indicator_bg, opacity.clamp(0., 1.))),
            );
        }
        row = row.child(layer);
        if let Some(leading) = self.leading {
            row = row.child(leading);
        } else if let Some(icon) = self.icon {
            let mut icon = Icon::new(icon)
                .size(px(if self.large { 20. } else { 17. }))
                .color(icon_color);
            if self.large {
                let [scale, rotation] = animate_on_activate(
                    key(&self.id, "icon-pop"),
                    Spring::SPATIAL,
                    self.selected,
                    [1.25, -8f32.to_radians()],
                    [1., 0.],
                    window,
                    cx,
                );
                icon = icon.scale(scale).rotate(rotation);
            }
            row = row.child(icon);
        }
        row = row.child(
            div()
                .flex_1()
                .overflow_hidden()
                .whitespace_nowrap()
                .text_ellipsis()
                .child(self.label),
        );
        let show_trailing = self.meta.is_none() || hovered;
        if let Some(meta) = self.meta.filter(|_| !(hovered && self.trailing.is_some())) {
            row = row.child(
                div()
                    .flex_none()
                    .text_size(px(11.5))
                    .font_weight(FontWeight(400.))
                    .text_color(s.outline)
                    .child(meta),
            );
        }
        if let Some(trailing) = self.trailing.filter(|_| show_trailing) {
            row = row.child(trailing);
        }
        let mut row = interactive(row, &state, true, true);
        if let Some(f) = self.on_click {
            row = row.on_click(move |e, w, cx| f(e, w, cx));
        }
        row
    }
}

/// Navigation rail item: a 56 × 32 pill with the icon and the label below.
#[derive(IntoElement)]
pub struct RailItem {
    id: ElementId,
    label: SharedString,
    icon: SharedString,
    selected: bool,
    on_click: Option<ClickHandler>,
}

impl RailItem {
    pub fn new(
        id: impl Into<ElementId>,
        icon: impl Into<SharedString>,
        label: impl Into<SharedString>,
    ) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            icon: icon.into(),
            selected: false,
            on_click: None,
        }
    }

    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }

    pub fn on_click(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
        self.on_click = Some(Rc::new(f));
        self
    }
}

impl RenderOnce for RailItem {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let s = *Theme::of(cx);
        let state = use_interaction(&self.id, window, cx);
        let pressed = state.read(cx).pressed;
        let radius = px(animate_f32(
            key(&self.id, "radius"),
            Spring::SPATIAL_FAST,
            if pressed { 8. } else { 16. },
            window,
            cx,
        ));
        let [width, opacity] = animate_on_activate(
            key(&self.id, "pill"),
            Spring::SPATIAL,
            self.selected,
            [24., 0.],
            [56., 1.],
            window,
            cx,
        );
        let fg = if self.selected {
            s.on_secondary_container
        } else {
            s.on_surface_variant
        };
        let fg = animate_color(key(&self.id, "fg"), Spring::EFFECTS, fg, window, cx);
        let label_color = if self.selected {
            s.on_surface
        } else {
            s.on_surface_variant
        };
        let layer = state_layer(&self.id, &state, all(radius), s.on_surface, window, cx);

        let mut pill = div()
            .relative()
            .flex()
            .items_center()
            .justify_center()
            .w(px(56.))
            .h(px(32.))
            .rounded(radius);
        if self.selected {
            pill = pill.child(
                div()
                    .absolute()
                    .top_0()
                    .h_full()
                    .left(px((56. - width) / 2.))
                    .w(px(width.max(0.)))
                    .corners(all(radius))
                    .bg(alpha(s.secondary_container, opacity.clamp(0., 1.))),
            );
        }
        pill = pill
            .child(layer)
            .child(Icon::new(self.icon).size(px(20.)).color(fg));

        let el = div()
            .id(self.id.clone())
            .flex()
            .flex_col()
            .items_center()
            .gap(px(4.))
            .w(px(72.))
            .pt(px(4.))
            .pb(px(6.))
            .font_family(font_family())
            .text_size(px(12.))
            .text_color(label_color)
            .font_weight(FontWeight(if self.selected { 700. } else { 500. }))
            .child(pill)
            .child(self.label);
        let mut el = interactive(el, &state, true, true);
        if let Some(f) = self.on_click {
            el = el.on_click(move |e, w, cx| f(e, w, cx));
        }
        el
    }
}

/// A tab in [`IconTabs`].
#[derive(Clone)]
pub struct IconTab {
    pub icon: SharedString,
    pub label: SharedString,
}

impl IconTab {
    pub fn new(icon: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
        Self {
            icon: icon.into(),
            label: label.into(),
        }
    }
}

/// Round icon tabs: the active one stretches into a pill showing its name
/// (the action-menu tabs in the reference design).
#[derive(IntoElement)]
pub struct IconTabs {
    id: ElementId,
    tabs: Vec<IconTab>,
    selected: usize,
    on_change: Option<ChangeHandler<usize>>,
}

impl IconTabs {
    pub fn new(
        id: impl Into<ElementId>,
        tabs: impl IntoIterator<Item = IconTab>,
        selected: usize,
    ) -> Self {
        Self {
            id: id.into(),
            tabs: tabs.into_iter().collect(),
            selected,
            on_change: None,
        }
    }

    pub fn on_change(mut self, f: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
        self.on_change = Some(Rc::new(f));
        self
    }
}

impl RenderOnce for IconTabs {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let s = *Theme::of(cx);
        let mut row = div().flex().w_full().gap(px(4.)).font_family(font_family());
        for (i, tab) in self.tabs.into_iter().enumerate() {
            let id = ElementId::NamedInteger(format!("{}-tab", self.id).into(), i as u64);
            let state = use_interaction(&id, window, cx);
            let hovered = state.read(cx).hovered;
            let on = i == self.selected;
            // The active one takes the free space (flex-grow 1); the rest stay at 34 px.
            let grow = animate_f32(
                key(&id, "grow"),
                Spring::SPATIAL,
                if on { 1. } else { 0. },
                window,
                cx,
            );
            let radius = if hovered && !on { 12. } else { 17. };
            let radius = px(animate_f32(
                key(&id, "radius"),
                Spring::SPATIAL_FAST,
                radius,
                window,
                cx,
            ));
            let (bg, fg) = match (on, hovered) {
                (true, _) => (s.secondary_container, s.on_secondary_container),
                (false, true) => (s.surface_container_highest, s.on_surface_variant),
                (false, false) => (s.surface_container_high, s.on_surface_variant),
            };
            let bg = animate_color(key(&id, "bg"), Spring::EFFECTS, bg, window, cx);
            let fg = animate_color(key(&id, "fg"), Spring::EFFECTS, fg, window, cx);
            let [scale, rot] = animate_on_activate(
                key(&id, "pop"),
                Spring::SPATIAL,
                on,
                [0.6, -0.52],
                [1., 0.],
                window,
                cx,
            );

            let mut el = div()
                .id(id.clone())
                .flex()
                .items_center()
                .justify_center()
                .gap(px(6.))
                .h(px(34.))
                .min_w(px(34.))
                .flex_basis(px(34.))
                .overflow_hidden()
                .whitespace_nowrap()
                .corners(all(radius))
                .bg(bg)
                .text_color(fg)
                .text_size(px(12.5))
                .font_weight(FontWeight(600.))
                .child(
                    Icon::new(tab.icon)
                        .size(px(17.))
                        .color(fg)
                        .scale(scale)
                        .rotate(rot),
                );
            el.style().flex_grow = Some(grow.max(0.) * 10.);
            if grow > 0.35 {
                el = el.child(
                    div()
                        .opacity(((grow - 0.35) / 0.65).clamp(0., 1.))
                        .child(tab.label),
                );
            }
            let mut el = interactive(el, &state, true, false);
            if let Some(f) = self.on_change.clone() {
                el = el.on_click(move |_, w, cx| f(i, w, cx));
            }
            row = row.child(el);
        }
        row
    }
}