gpui-component 0.6.4

GPUI Component: the styled component library of GPUI Kit, with 60+ desktop UI 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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
use gpui::{
    AnyElement, App, IntoElement, ParentElement, RenderOnce, StyleRefinement, Styled, Window, div,
    prelude::FluentBuilder as _, relative, rems,
};

use crate::{ActiveTheme as _, StyledExt as _, v_flex};

/// A presentational empty state with independently styled header and content slots.
///
/// The application decides when this element is shown and owns the actions and
/// state of its children. Additional direct children follow the named slots,
/// regardless of builder call order.
#[derive(IntoElement)]
pub struct Empty {
    style: StyleRefinement,
    header: Option<EmptyHeader>,
    content: Option<EmptyContent>,
    children: Vec<AnyElement>,
}

impl Empty {
    /// Create an empty state without a background or visible border.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            header: None,
            content: None,
            children: Vec::new(),
        }
    }

    /// Set the header, replacing any previously configured header.
    pub fn header(mut self, header: EmptyHeader) -> Self {
        self.header = Some(header);
        self
    }

    /// Set the content, replacing any previously configured content.
    pub fn content(mut self, content: EmptyContent) -> Self {
        self.content = Some(content);
        self
    }
}

impl Default for Empty {
    fn default() -> Self {
        Self::new()
    }
}

impl ParentElement for Empty {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl Styled for Empty {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for Empty {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        v_flex()
            .w_full()
            .min_w_0()
            .flex_1()
            .items_center()
            .justify_center()
            .gap_4()
            .p_6()
            .rounded(cx.theme().radius_tokens().xl)
            .border_dashed()
            .border_color(cx.theme().border)
            .text_center()
            .text_color(cx.theme().foreground)
            .refine_style(&self.style)
            .when_some(self.header, |this, header| this.child(header))
            .when_some(self.content, |this, content| this.child(content))
            .children(self.children)
    }
}

/// The media, title, and description of an [`Empty`] state, in that order.
///
/// Each slot is optional. Replacing one slot leaves the other slots intact.
#[derive(IntoElement)]
pub struct EmptyHeader {
    style: StyleRefinement,
    media: Option<EmptyMedia>,
    title: Option<EmptyTitle>,
    description: Option<EmptyDescription>,
}

impl EmptyHeader {
    /// Create a centered header with a maximum width of 24 rem.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            media: None,
            title: None,
            description: None,
        }
    }

    /// Set the media, replacing any previously configured media.
    pub fn media(mut self, media: EmptyMedia) -> Self {
        self.media = Some(media);
        self
    }

    /// Set the title, replacing any previously configured title.
    pub fn title(mut self, title: EmptyTitle) -> Self {
        self.title = Some(title);
        self
    }

    /// Set the description, replacing any previously configured description.
    pub fn description(mut self, description: EmptyDescription) -> Self {
        self.description = Some(description);
        self
    }
}

impl Default for EmptyHeader {
    fn default() -> Self {
        Self::new()
    }
}

impl Styled for EmptyHeader {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for EmptyHeader {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        v_flex()
            .w_full()
            .max_w(rems(24.))
            .min_w_0()
            .items_center()
            .gap_2()
            .refine_style(&self.style)
            .when_some(self.media, |this, media| this.child(media))
            .when_some(self.title, |this, title| this.child(title))
            .when_some(self.description, |this, description| {
                this.child(description)
            })
    }
}

/// Visual treatment for the media of an [`Empty`] state.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum EmptyMediaVariant {
    /// Unframed content such as an image, avatar, or avatar group.
    #[default]
    Default,
    /// A muted, rounded two-rem frame for an icon.
    Icon,
}

/// A media slot accepting icons, images, avatars, and custom elements.
#[derive(IntoElement)]
pub struct EmptyMedia {
    style: StyleRefinement,
    variant: EmptyMediaVariant,
    children: Vec<AnyElement>,
}

impl EmptyMedia {
    /// Create an unframed media slot.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            variant: EmptyMediaVariant::Default,
            children: Vec::new(),
        }
    }

    /// Set the visual treatment without changing the supplied children.
    pub fn with_variant(mut self, variant: EmptyMediaVariant) -> Self {
        self.variant = variant;
        self
    }
}

impl Default for EmptyMedia {
    fn default() -> Self {
        Self::new()
    }
}

impl ParentElement for EmptyMedia {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl Styled for EmptyMedia {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for EmptyMedia {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        // A column preserves the intrinsic width of nested rows such as AvatarGroup.
        v_flex()
            .flex_shrink_0()
            .items_center()
            .justify_center()
            .mb_2()
            .when(self.variant == EmptyMediaVariant::Icon, |this| {
                this.size_8()
                    .rounded(cx.theme().radius_tokens().lg)
                    .bg(cx.theme().muted)
                    .text_color(cx.theme().foreground)
                    // Icon inherits one rem unless it has an explicit size.
                    .text_base()
            })
            .refine_style(&self.style)
            .children(self.children)
    }
}

/// The title of an [`Empty`] state, accepting text or custom children.
#[derive(IntoElement)]
pub struct EmptyTitle {
    style: StyleRefinement,
    children: Vec<AnyElement>,
}

impl EmptyTitle {
    /// Create an empty title.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            children: Vec::new(),
        }
    }
}

impl Default for EmptyTitle {
    fn default() -> Self {
        Self::new()
    }
}

impl ParentElement for EmptyTitle {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl Styled for EmptyTitle {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for EmptyTitle {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        div()
            .max_w_full()
            .min_w_0()
            .text_sm()
            .font_medium()
            .whitespace_normal()
            .refine_style(&self.style)
            .children(self.children)
    }
}

/// Supporting text or rich content for an [`Empty`] state.
#[derive(IntoElement)]
pub struct EmptyDescription {
    style: StyleRefinement,
    children: Vec<AnyElement>,
}

impl EmptyDescription {
    /// Create a muted description that wraps to the available width.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            children: Vec::new(),
        }
    }
}

impl Default for EmptyDescription {
    fn default() -> Self {
        Self::new()
    }
}

impl ParentElement for EmptyDescription {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl Styled for EmptyDescription {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for EmptyDescription {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        div()
            .w_full()
            .min_w_0()
            .text_sm()
            .line_height(relative(1.625))
            .text_color(cx.theme().muted_foreground)
            .whitespace_normal()
            .refine_style(&self.style)
            .children(self.children)
    }
}

/// Actions, inputs, or other application-owned content below an [`EmptyHeader`].
#[derive(IntoElement)]
pub struct EmptyContent {
    style: StyleRefinement,
    children: Vec<AnyElement>,
}

impl EmptyContent {
    /// Create a centered content column with a maximum width of 24 rem.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            children: Vec::new(),
        }
    }
}

impl Default for EmptyContent {
    fn default() -> Self {
        Self::new()
    }
}

impl ParentElement for EmptyContent {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl Styled for EmptyContent {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for EmptyContent {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        v_flex()
            .w_full()
            .max_w(rems(24.))
            .min_w_0()
            .items_center()
            .gap_2p5()
            .text_sm()
            .refine_style(&self.style)
            .children(self.children)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        Sizable as _,
        avatar::{Avatar, AvatarGroup},
    };
    use gpui::{Context, InteractiveElement as _, Render, TestAppContext, px};

    struct MediaLayout {
        grouped: bool,
        leading: bool,
    }

    impl Render for MediaLayout {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            let media = if self.grouped {
                AvatarGroup::new()
                    .small()
                    .debug_selector(|| "media-content".into())
                    .child(Avatar::new().name("Alex"))
                    .child(Avatar::new().name("Taylor"))
                    .child(Avatar::new().name("Sam"))
                    .into_any_element()
            } else {
                Avatar::new()
                    .small()
                    .debug_selector(|| "media-content".into())
                    .name("Alex")
                    .into_any_element()
            };

            // Supply the slots out of order; their visual order is a contract.
            Empty::new()
                .when(self.leading, |this| this.items_start().text_left())
                .child(div().size_4().debug_selector(|| "trailing".into()))
                .content(
                    EmptyContent::new()
                        .when(self.leading, |this| this.items_start())
                        .child(div().size_4().debug_selector(|| "content".into())),
                )
                .header(
                    EmptyHeader::new()
                        .when(self.leading, |this| this.items_start())
                        .description(EmptyDescription::new().child("Supporting content"))
                        .title(
                            EmptyTitle::new()
                                .child(div().size_4().debug_selector(|| "title".into())),
                        )
                        .media(EmptyMedia::new().child(media)),
                )
        }
    }

    #[gpui::test]
    fn media_keeps_intrinsic_width_and_slot_alignment(cx: &mut TestAppContext) {
        cx.update(crate::init);

        for grouped in [false, true] {
            for leading in [false, true] {
                let (_, cx) = cx.add_window_view(|_, _| MediaLayout { grouped, leading });
                for rem in [14., 20.] {
                    cx.update(|window, cx| {
                        window.set_rem_size(px(rem));
                        window.draw(cx).clear(cx);
                    });
                    let media = cx.debug_bounds("media-content").unwrap();
                    let title = cx.debug_bounds("title").unwrap();
                    let content = cx.debug_bounds("content").unwrap();
                    let trailing = cx.debug_bounds("trailing").unwrap();

                    // A zero-width group paints outside its centered slot.
                    assert!(media.size.width > px(0.), "grouped: {grouped}");
                    let alignment = |bounds: gpui::Bounds<gpui::Pixels>| {
                        if leading {
                            bounds.left()
                        } else {
                            bounds.center().x
                        }
                    };
                    assert_eq!(alignment(media), alignment(title));
                    assert_eq!(alignment(content), alignment(title));
                    assert!(media.bottom() <= title.top());
                    assert!(title.bottom() <= content.top());
                    assert!(content.bottom() <= trailing.top());
                }
            }
        }
    }

    #[test]
    fn test_empty_builder() {
        let root_style = StyleRefinement::default().p_4().border_1();
        let header_style = StyleRefinement::default().items_start();
        let media_style = StyleRefinement::default().size_10();
        let title_style = StyleRefinement::default().text_base();
        let description_style = StyleRefinement::default().text_left();
        let content_style = StyleRefinement::default().flex_row().flex_wrap().gap_2();

        let empty = Empty::new()
            .refine_style(&root_style)
            .child("Before the named setters, still after the slots")
            .header(EmptyHeader::new().media(EmptyMedia::new()))
            .content(EmptyContent::new().child("Replaced content"))
            .header(
                EmptyHeader::new()
                    .refine_style(&header_style)
                    .title(EmptyTitle::new().child("Replaced title"))
                    .description(EmptyDescription::new().child("Replaced description"))
                    .media(EmptyMedia::new().child("Replaced media"))
                    .description(
                        EmptyDescription::new()
                            .refine_style(&description_style)
                            .children(["Description", "Custom content"]),
                    )
                    .title(EmptyTitle::new().refine_style(&title_style).child("Title"))
                    .media(
                        EmptyMedia::new()
                            .with_variant(EmptyMediaVariant::Icon)
                            .refine_style(&media_style)
                            .child(crate::Icon::new(crate::IconName::Search)),
                    ),
            )
            .content(
                EmptyContent::new()
                    .refine_style(&content_style)
                    .children(["First action", "Second action"]),
            )
            .child("Trailing content");

        assert_eq!(empty.style, root_style);
        assert_eq!(empty.children.len(), 2);
        let header = empty.header.unwrap();
        assert_eq!(header.style, header_style);
        let media = header.media.unwrap();
        assert_eq!(media.variant, EmptyMediaVariant::Icon);
        assert_eq!(media.style, media_style);
        assert_eq!(media.children.len(), 1);
        let title = header.title.unwrap();
        assert_eq!(title.style, title_style);
        assert_eq!(title.children.len(), 1);
        let description = header.description.unwrap();
        assert_eq!(description.style, description_style);
        assert_eq!(description.children.len(), 2);
        let content = empty.content.unwrap();
        assert_eq!(content.style, content_style);
        assert_eq!(content.children.len(), 2);

        let empty = Empty::default();
        assert!(empty.header.is_none());
        assert!(empty.content.is_none());
        assert!(empty.children.is_empty());
        assert_eq!(EmptyMedia::default().variant, EmptyMediaVariant::Default);
    }
}