gpui-component 0.6.0

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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
use gpui::{
    AnyElement, App, IntoElement, ParentElement, RenderOnce, StyleRefinement, Styled, Window,
    prelude::FluentBuilder as _, relative, rems,
};

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

/// Horizontal alignment for a message and message-owned chat surfaces.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum MessageAlignment {
    /// Place the message at the leading edge.
    #[default]
    Start,
    /// Place the message at the trailing edge.
    End,
}

/// A vertical stack of consecutive messages from the same sender.
#[derive(IntoElement)]
pub struct MessageGroup {
    style: StyleRefinement,
    children: Vec<AnyElement>,
}

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

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

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

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

impl RenderOnce for MessageGroup {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        v_flex()
            .min_w_0()
            .gap_2()
            .refine_style(&self.style)
            .children(self.children)
    }
}

/// A composable message row with named avatar, header, content, and footer slots.
///
/// Named slots let the message apply its alignment consistently while every
/// part remains independently styleable.
#[derive(IntoElement)]
pub struct Message {
    style: StyleRefinement,
    stack_style: StyleRefinement,
    alignment: MessageAlignment,
    avatar: Option<MessageAvatar>,
    header: Option<MessageHeader>,
    content: Option<MessageContent>,
    footer: Option<MessageFooter>,
}

impl Message {
    /// Create a leading-aligned message.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            stack_style: StyleRefinement::default(),
            alignment: MessageAlignment::Start,
            avatar: None,
            header: None,
            content: None,
            footer: None,
        }
    }

    /// Set whether the message is aligned to the leading or trailing edge.
    pub fn alignment(mut self, alignment: MessageAlignment) -> Self {
        self.alignment = alignment;
        self
    }

    /// Refine the inner vertical stack that contains the named slots.
    pub fn with_stack_style(mut self, style: StyleRefinement) -> Self {
        self.stack_style = style;
        self
    }

    /// Set an optional avatar or other sender identity element.
    pub fn avatar(mut self, avatar: impl IntoElement) -> Self {
        self.avatar = Some(MessageAvatar::new().child(avatar));
        self
    }

    /// Set a fully configured avatar slot.
    pub fn avatar_slot(mut self, avatar: MessageAvatar) -> Self {
        self.avatar = Some(avatar);
        self
    }

    /// Set the message header.
    pub fn header(mut self, header: MessageHeader) -> Self {
        self.header = Some(header);
        self
    }

    /// Set the message body.
    pub fn content(mut self, content: MessageContent) -> Self {
        self.content = Some(content);
        self
    }

    /// Set the message footer.
    pub fn footer(mut self, footer: MessageFooter) -> Self {
        self.footer = Some(footer);
        self
    }
}

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

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

impl RenderOnce for Message {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        let alignment = self.alignment;
        let has_avatar = self.avatar.is_some();
        let has_ghost_bubble = self
            .content
            .as_ref()
            .is_some_and(|content| content.has_ghost_bubble);
        let stack_style = self.stack_style;

        v_flex()
            .relative()
            .w_full()
            .min_w_0()
            .gap(rems(0.625))
            .text_sm()
            .line_height(relative(1.25))
            .map(|this| match alignment {
                MessageAlignment::Start => this.items_start(),
                MessageAlignment::End => this.items_end(),
            })
            .refine_style(&self.style)
            .child(
                // The footer lives outside this row so the bottom-anchored
                // avatar always sits flush with the content's bottom edge,
                // whatever the footer contains.
                h_flex()
                    .w_full()
                    .min_w_0()
                    .items_end()
                    .gap_2()
                    .when(alignment == MessageAlignment::End, |this| {
                        this.flex_row_reverse()
                    })
                    .when_some(self.avatar, |this, avatar| this.child(avatar))
                    .child(
                        v_flex()
                            .w_full()
                            .min_w_0()
                            .gap(rems(0.625))
                            .map(|this| match alignment {
                                MessageAlignment::Start => this.items_start(),
                                MessageAlignment::End => this.items_end(),
                            })
                            .refine_style(&stack_style)
                            .when_some(self.header, |this, header| {
                                this.child(header.with_inherited_content_inset(!has_ghost_bubble))
                            })
                            .when_some(self.content, |this, content| {
                                this.child(content.aligned(alignment))
                            }),
                    ),
            )
            .when_some(self.footer, |this, footer| {
                this.child(
                    footer
                        .with_inherited_content_inset(!has_ghost_bubble)
                        // Align the footer with the content column: the
                        // avatar's shared `size-8` baseline plus the row gap.
                        .when(has_avatar && alignment == MessageAlignment::Start, |this| {
                            this.ml(rems(2.5))
                        })
                        .when(has_avatar && alignment == MessageAlignment::End, |this| {
                            this.mr(rems(2.5))
                        }),
                )
            })
    }
}

/// The sender identity slot rendered beside a [`Message`].
///
/// The slot reserves the shared `size-8` baseline; the message row keeps it
/// flush with the bottom edge of the visible message surface.
#[derive(IntoElement)]
pub struct MessageAvatar {
    style: StyleRefinement,
    children: Vec<AnyElement>,
}

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

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

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

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

impl RenderOnce for MessageAvatar {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let tokens = cx.theme().semantic_tokens();

        h_flex()
            .relative()
            .min_w_8()
            .flex_none()
            .items_center()
            .justify_center()
            .self_end()
            .overflow_hidden()
            .rounded(cx.theme().radius_full())
            .bg(tokens.colors.muted)
            .refine_style(&self.style)
            .children(self.children)
    }
}

/// Header content such as a sender name and timestamp.
#[derive(IntoElement)]
pub struct MessageHeader {
    style: StyleRefinement,
    content_inset: Option<bool>,
    children: Vec<AnyElement>,
}

impl MessageHeader {
    /// Create an empty message header.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            content_inset: None,
            children: Vec::new(),
        }
    }

    /// Set whether the header keeps its default horizontal content inset.
    pub fn content_inset(mut self, content_inset: bool) -> Self {
        self.content_inset = Some(content_inset);
        self
    }

    fn with_inherited_content_inset(mut self, content_inset: bool) -> Self {
        self.content_inset.get_or_insert(content_inset);
        self
    }
}

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

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

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

impl RenderOnce for MessageHeader {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let tokens = cx.theme().semantic_tokens();

        h_flex()
            .max_w_full()
            .min_w_0()
            .gap_1()
            .text_xs()
            .line_height(relative(1.25))
            .font_medium()
            .text_color(tokens.colors.muted_foreground)
            .when(self.content_inset.unwrap_or(true), |this| this.px_3())
            .refine_style(&self.style)
            .children(self.children)
    }
}

/// The message body slot. It can contain bubbles, images, code, or files.
#[derive(IntoElement)]
pub struct MessageContent {
    style: StyleRefinement,
    alignment: MessageAlignment,
    has_ghost_bubble: bool,
    children: Vec<AnyElement>,
}

impl MessageContent {
    /// Create an empty message body.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            alignment: MessageAlignment::Start,
            has_ghost_bubble: false,
            children: Vec::new(),
        }
    }

    /// Add a typed bubble and inherit ghost-surface metadata layout.
    ///
    /// Ordinary `.child(...)` content remains available for arbitrary elements;
    /// use this builder when surrounding message slots should react to a
    /// bubble's variant.
    pub fn bubble(mut self, bubble: Bubble) -> Self {
        self.has_ghost_bubble |= bubble.is_ghost();
        self.children.push(bubble.into_any_element());
        self
    }

    fn aligned(mut self, alignment: MessageAlignment) -> Self {
        self.alignment = alignment;
        self
    }
}

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

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

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

impl RenderOnce for MessageContent {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        v_flex()
            .w_full()
            .max_w_full()
            .min_w_0()
            .gap(rems(0.625))
            .map(|this| match self.alignment {
                MessageAlignment::Start => this.items_start(),
                MessageAlignment::End => this.items_end(),
            })
            .refine_style(&self.style)
            .children(self.children)
    }
}

/// Footer content such as delivery state, reactions, or action buttons.
#[derive(IntoElement)]
pub struct MessageFooter {
    style: StyleRefinement,
    content_inset: Option<bool>,
    children: Vec<AnyElement>,
}

impl MessageFooter {
    /// Create an empty message footer.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            content_inset: None,
            children: Vec::new(),
        }
    }

    /// Set whether the footer keeps its default horizontal content inset.
    pub fn content_inset(mut self, content_inset: bool) -> Self {
        self.content_inset = Some(content_inset);
        self
    }

    fn with_inherited_content_inset(mut self, content_inset: bool) -> Self {
        self.content_inset.get_or_insert(content_inset);
        self
    }
}

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

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

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

impl RenderOnce for MessageFooter {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let tokens = cx.theme().semantic_tokens();

        h_flex()
            .max_w_full()
            .min_w_0()
            .gap_1()
            .text_xs()
            .line_height(relative(1.25))
            .font_medium()
            .text_color(tokens.colors.muted_foreground)
            .when(self.content_inset.unwrap_or(true), |this| this.px_3())
            .refine_style(&self.style)
            .children(self.children)
    }
}

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

    #[test]
    fn test_message_builder() {
        let stack_style = StyleRefinement::default().gap_1();
        let message = Message::new()
            .alignment(MessageAlignment::End)
            .with_stack_style(stack_style.clone())
            .avatar_slot(MessageAvatar::new().child(gpui::div()))
            .header(MessageHeader::new().content_inset(false).child("Alice"))
            .content(MessageContent::new().child("Hello"))
            .footer(MessageFooter::new().content_inset(false).child("Delivered"));

        assert_eq!(message.alignment, MessageAlignment::End);
        assert_eq!(message.stack_style, stack_style);
        assert!(message.avatar.is_some());
        assert!(message.header.is_some());
        assert!(message.content.is_some());
        assert!(message.footer.is_some());
        assert_eq!(message.header.as_ref().unwrap().content_inset, Some(false));
        assert_eq!(message.footer.as_ref().unwrap().content_inset, Some(false));

        let group = MessageGroup::new().child("First").child("Second");
        assert_eq!(group.children.len(), 2);

        let content = MessageContent::new().aligned(MessageAlignment::End);
        assert_eq!(content.alignment, MessageAlignment::End);

        let avatar = MessageAvatar::new().child("ME");
        assert_eq!(avatar.children.len(), 1);
    }

    #[test]
    fn test_ghost_bubble_inherits_message_slot_insets() {
        let content = MessageContent::new()
            .bubble(Bubble::new())
            .bubble(Bubble::new().with_variant(crate::bubble::BubbleVariant::Ghost));

        assert!(content.has_ghost_bubble);
        assert_eq!(content.children.len(), 2);
        assert_eq!(
            MessageHeader::new()
                .with_inherited_content_inset(false)
                .content_inset,
            Some(false)
        );
        assert_eq!(
            MessageFooter::new()
                .with_inherited_content_inset(false)
                .content_inset,
            Some(false)
        );
        assert_eq!(
            MessageHeader::new()
                .content_inset(true)
                .with_inherited_content_inset(false)
                .content_inset,
            Some(true)
        );
        assert_eq!(
            MessageFooter::new()
                .content_inset(true)
                .with_inherited_content_inset(false)
                .content_inset,
            Some(true)
        );
    }
}