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
406
407
408
409
410
411
412
413
414
415
416
417
use gpui::{
    AnyElement, App, Axis, ElementId, Empty, Hsla, InteractiveElement, IntoElement, ParentElement,
    Pixels, Point, RenderOnce, Role, SharedString, StatefulInteractiveElement, Styled, Window, div,
    prelude::FluentBuilder, px,
};
use gpui_base::TestSupportExt;
use gpui_material_foundation::{color, current_material_theme, text_style};
use gpui_material_theme::{ColorRole, Direction, TypeRole, TypographyEmphasis};

use crate::SemanticTone;

/// Decorative Material divider, with logical start and end insets.
#[derive(IntoElement)]
pub struct Divider {
    axis: Axis,
    thickness: gpui::Pixels,
    color_role: ColorRole,
    color_override: Option<gpui::Hsla>,
    start: gpui::Pixels,
    end: gpui::Pixels,
}

impl Divider {
    const DEFAULT_THICKNESS: gpui::Pixels = px(1.);

    /// Creates a full-width horizontal divider with 1 px default thickness and `OutlineVariant` color.
    pub fn horizontal() -> Self {
        Self {
            axis: Axis::Horizontal,
            thickness: Self::DEFAULT_THICKNESS,
            color_role: ColorRole::OutlineVariant,
            color_override: None,
            start: px(0.),
            end: px(0.),
        }
    }

    /// Creates a full-height vertical divider with 1 px default thickness and `OutlineVariant` color.
    pub fn vertical() -> Self {
        Self {
            axis: Axis::Vertical,
            ..Self::horizontal()
        }
    }

    /// Sets the stroke thickness in logical pixels. Zero requests a one-device-pixel hairline.
    /// Negative or non-finite values use the default thickness of one logical pixel.
    pub fn thickness(mut self, thickness: gpui::Pixels) -> Self {
        self.thickness = if f32::from(thickness).is_finite() && thickness >= px(0.) {
            thickness
        } else {
            Self::DEFAULT_THICKNESS
        };
        self
    }

    /// Resolves the stroke color from the current window theme.
    pub fn color(mut self, role: ColorRole) -> Self {
        self.color_role = role;
        self.color_override = None;
        self
    }

    /// Sets a literal stroke color. A later call to `color` restores theme-role lookup.
    pub fn color_override(mut self, color: impl Into<gpui::Hsla>) -> Self {
        self.color_override = Some(color.into());
        self
    }

    /// Sets logical start and end insets along the divider's main axis (clamped to non-negative values).
    pub fn inset(mut self, start: gpui::Pixels, end: gpui::Pixels) -> Self {
        self.start = start.max(px(0.));
        self.end = end.max(px(0.));
        self
    }
}

impl RenderOnce for Divider {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let fill = self
            .color_override
            .unwrap_or_else(|| tone(self.color_role, window, cx));
        let thickness = if self.thickness == px(0.) {
            px(window.scale_factor().recip())
        } else {
            self.thickness
        };
        let (left, right) = current_material_theme(window, cx)
            .layout
            .horizontal(self.start, self.end);
        if self.axis == Axis::Vertical {
            div()
                .w(thickness)
                .h_full()
                .pt(self.start)
                .pb(self.end)
                .child(div().size_full().bg(fill))
        } else {
            div()
                .w_full()
                .h(thickness)
                .pl(left)
                .pr(right)
                .child(div().size_full().bg(fill))
        }
    }
}

/// A standalone or anchored Material badge. For anchored badges, include the
/// badge's information in the host's accessible name.
#[derive(IntoElement)]
pub struct Badge {
    host: Option<AnyElement>,
    content: BadgeContent,
    tone: SemanticTone,
    color_roles: Option<(ColorRole, ColorRole)>,
    container_color: Option<Hsla>,
    content_color: Option<Hsla>,
    accessible_label: Option<(ElementId, SharedString)>,
    visible: bool,
    offset: Point<Pixels>,
}

enum BadgeContent {
    Dot,
    Text(SharedString),
    Custom(ElementId, AnyElement),
}

fn format_badge_count(count: usize, maximum: usize) -> SharedString {
    if count > maximum {
        format!("{maximum}+").into()
    } else {
        count.to_string().into()
    }
}

fn resolve_badge_roles(
    tone: SemanticTone,
    color_roles: Option<(ColorRole, ColorRole)>,
) -> (ColorRole, ColorRole) {
    color_roles.unwrap_or_else(|| {
        tone.roles().map_or(
            (ColorRole::Error, ColorRole::OnError),
            |(role, on_role, _, _)| (role, on_role),
        )
    })
}

impl Badge {
    /// A small badge that participates in the surrounding layout.
    pub fn standalone() -> Self {
        Self {
            host: None,
            content: BadgeContent::Dot,
            tone: SemanticTone::Default,
            color_roles: None,
            container_color: None,
            content_color: None,
            accessible_label: None,
            visible: true,
            offset: Point::default(),
        }
    }

    /// A standalone text badge that participates in the surrounding layout.
    pub fn standalone_label(text: impl Into<SharedString>) -> Self {
        Self {
            content: BadgeContent::Text(text.into()),
            ..Self::standalone()
        }
    }

    /// A standalone capped numeric badge that participates in the surrounding layout.
    pub fn standalone_count(count: usize, maximum: usize) -> Self {
        Self::standalone_label(format_badge_count(count, maximum))
    }

    /// Anchors a small badge to the host's upper trailing edge.
    pub fn dot(host: impl IntoElement) -> Self {
        Self {
            host: Some(host.into_any_element()),
            ..Self::standalone()
        }
    }

    /// Anchors a text badge to the host's upper trailing edge.
    pub fn label(host: impl IntoElement, text: impl Into<SharedString>) -> Self {
        Self {
            content: BadgeContent::Text(text.into()),
            ..Self::dot(host)
        }
    }

    /// Anchors a capped numeric badge to the host's upper trailing edge, displaying `"{maximum}+"` when `count > maximum`.
    pub fn count(host: impl IntoElement, count: usize, maximum: usize) -> Self {
        Self::label(host, format_badge_count(count, maximum))
    }

    /// Uses large badge styling with caller-supplied visual content.
    ///
    /// Use a stable ID within the parent and keep the content noninteractive.
    /// The badge or its host supplies the accessible name.
    pub fn content(mut self, id: impl Into<ElementId>, content: impl IntoElement) -> Self {
        self.content = BadgeContent::Custom(id.into(), content.into_any_element());
        self
    }

    /// Applies a Material 3 semantic tone shortcut.
    ///
    /// [`SemanticTone::Default`] and [`SemanticTone::Danger`] use `Error`/`OnError`.
    /// Explicit [`Self::color_role`], [`Self::container_color`], and
    /// [`Self::content_color`] overrides take precedence.
    pub fn tone(mut self, tone: SemanticTone) -> Self {
        self.tone = tone;
        self
    }

    /// Resolves container and content pigments from the window theme's semantic color roles.
    ///
    /// Takes precedence over [`Self::tone`], and is overridden by explicit
    /// [`Self::container_color`] and [`Self::content_color`] values.
    pub fn color_role(mut self, container: ColorRole, content: ColorRole) -> Self {
        self.color_roles = Some((container, content));
        self
    }

    /// Sets the background color, overriding [`Self::color_role`] and [`Self::tone`].
    pub fn container_color(mut self, color: impl Into<Hsla>) -> Self {
        self.container_color = Some(color.into());
        self
    }

    /// Sets the inherited content color, overriding [`Self::color_role`] and [`Self::tone`].
    pub fn content_color(mut self, color: impl Into<Hsla>) -> Self {
        self.content_color = Some(color.into());
        self
    }

    /// Controls whether the badge mark is rendered. When `false`, anchored badges render only
    /// their host and standalone badges render empty. Defaults to `true`.
    pub fn visible(mut self, visible: bool) -> Self {
        self.visible = visible;
        self
    }

    /// Shifts an anchored badge from its default upper-trailing anchor (`x` follows logical
    /// direction; `y` shifts downward).
    pub fn offset(mut self, offset: Point<Pixels>) -> Self {
        self.offset = offset;
        self
    }

    /// Names a badge for assistive technology without adding a focus stop.
    /// Anchored badges normally use their host's accessible name instead.
    pub fn accessible_label(
        mut self,
        id: impl Into<ElementId>,
        label: impl Into<SharedString>,
    ) -> Self {
        self.accessible_label = Some((id.into(), label.into()));
        self
    }

    #[cfg(feature = "navigation")]
    pub(crate) fn take_accessible_label(&mut self) -> Option<SharedString> {
        if !self.visible {
            return None;
        }
        self.accessible_label.take().map(|(_, label)| label)
    }

    #[cfg(feature = "navigation")]
    pub(crate) fn attach_to(mut self, host: impl IntoElement) -> Self {
        self.host = Some(host.into_any_element());
        self
    }
}

impl RenderOnce for Badge {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        if !self.visible {
            return self.host.unwrap_or_else(|| Empty.into_any_element());
        }
        let large = !matches!(self.content, BadgeContent::Dot);
        let side = px(if large { 16. } else { 6. });
        let (container_role, content_role) = resolve_badge_roles(self.tone, self.color_roles);
        let container_color = self
            .container_color
            .unwrap_or_else(|| tone(container_role, window, cx));
        let content_color = self
            .content_color
            .unwrap_or_else(|| tone(content_role, window, cx));
        let typography = TypeRole::LabelSmall.style(TypographyEmphasis::Baseline);
        let overlap = px(if large { 12. } else { 6. });
        let dx = if f32::from(self.offset.x).is_finite() {
            self.offset.x
        } else {
            px(0.)
        };
        let dy = if f32::from(self.offset.y).is_finite() {
            self.offset.y
        } else {
            px(0.)
        };
        let mark = div()
            .min_w(side)
            .rounded_full()
            .when(large, |badge| badge.px(px(4.)))
            .when(self.host.is_some(), |badge| {
                badge
                    .absolute()
                    .top(if large { px(-2.) } else { px(0.) } + dy)
                    .when_else(
                        current_material_theme(window, cx).layout.direction == Direction::Rtl,
                        |badge| badge.right(gpui::relative(1.)).mr(-overlap + dx),
                        |badge| badge.left(gpui::relative(1.)).ml(-overlap + dx),
                    )
            })
            .bg(container_color)
            .text_color(content_color)
            .flex()
            .items_center()
            .justify_center();
        let mark = match self.content {
            BadgeContent::Dot => mark.h(side).w(side),
            BadgeContent::Text(text) => mark.min_h(side).child(crate::text::single_line(
                text,
                typography,
                1.,
                content_color,
                (None, 1., 1.),
                window,
                cx,
            )),
            BadgeContent::Custom(id, content) => {
                text_style(mark.min_h(side), typography, 1., window, cx).child(
                    div()
                        .id(id)
                        .role(Role::Group)
                        .a11y_synthetic_children(|tree| tree.parent_node().set_hidden())
                        .child(content),
                )
            }
        };
        let mark = if let Some((id, label)) = self.accessible_label {
            mark.id(id)
                .test_support()
                .role(Role::Label)
                .aria_label(label)
                .into_any_element()
        } else {
            mark.into_any_element()
        };
        if let Some(host) = self.host {
            div().relative().child(host).child(mark).into_any_element()
        } else {
            div().flex().items_center().child(mark).into_any_element()
        }
    }
}

fn tone(role: ColorRole, window: &Window, cx: &App) -> gpui::Hsla {
    color(current_material_theme(window, cx).colors.color(role))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn badge_roles_follow_tone_and_explicit_color_role_precedence() {
        use ColorRole::*;
        assert_eq!(
            resolve_badge_roles(SemanticTone::Default, None),
            (Error, OnError)
        );
        assert_eq!(
            resolve_badge_roles(SemanticTone::Danger, None),
            (Error, OnError)
        );
        assert_eq!(
            resolve_badge_roles(SemanticTone::Success, None),
            (Success, OnSuccess)
        );
        assert_eq!(
            resolve_badge_roles(SemanticTone::Warning, None),
            (Warning, OnWarning)
        );
        assert_eq!(
            resolve_badge_roles(SemanticTone::Info, None),
            (Info, OnInfo)
        );
        assert_eq!(
            resolve_badge_roles(
                SemanticTone::Danger,
                Some((PrimaryContainer, OnPrimaryContainer))
            ),
            (PrimaryContainer, OnPrimaryContainer)
        );
    }

    #[test]
    fn standalone_badge_constructors_format_labels_and_capped_counts() {
        assert_eq!(format_badge_count(0, 99), SharedString::from("0"));
        assert_eq!(format_badge_count(99, 99), SharedString::from("99"));
        assert_eq!(format_badge_count(120, 99), SharedString::from("99+"));

        let label = Badge::standalone_label("NEW");
        assert!(label.host.is_none());
        assert!(matches!(label.content, BadgeContent::Text(ref text) if text == "NEW"));

        let count = Badge::standalone_count(150, 99);
        assert!(count.host.is_none());
        assert!(matches!(count.content, BadgeContent::Text(ref text) if text == "99+"));
    }
}