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
use gpui::{
    AnyElement, App, IntoElement, ParentElement, RenderOnce, StyleRefinement, Styled, Window, div,
    prelude::FluentBuilder as _, relative, rems,
};

use crate::{
    ActiveTheme as _, Colorize as _, StyledExt as _, button::Button, message::MessageAlignment,
    v_flex,
};

/// Visual treatment for a chat bubble.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BubbleVariant {
    /// A filled primary surface.
    #[default]
    Filled,
    /// A neutral secondary surface.
    Secondary,
    /// A lower-emphasis surface.
    Muted,
    /// A subtle primary-tinted surface.
    Tinted,
    /// A background surface with a visible border.
    Outline,
    /// No surface, padding, or border.
    Ghost,
    /// A destructive surface for failed or invalid content.
    Destructive,
}

/// Edge on which reaction feedback is attached.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BubbleReactionSide {
    /// Attach reactions above the bubble.
    Top,
    /// Attach reactions below the bubble.
    #[default]
    Bottom,
}

/// A chat bubble layout that owns alignment, width, and reaction positioning.
///
/// The visible surface is rendered by [`BubbleContent`]. Direct children are
/// added to that content slot as a convenience.
#[derive(IntoElement)]
pub struct Bubble {
    style: StyleRefinement,
    alignment: Option<MessageAlignment>,
    variant: BubbleVariant,
    content: BubbleContent,
    reactions: Option<BubbleReactions>,
}

impl Bubble {
    /// Create a filled bubble that can inherit alignment from its parent.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            alignment: None,
            variant: BubbleVariant::Filled,
            content: BubbleContent::new(),
            reactions: None,
        }
    }

    /// Set the bubble alignment.
    pub fn alignment(mut self, alignment: MessageAlignment) -> Self {
        self.alignment = Some(alignment);
        self
    }

    /// Set the visual treatment.
    pub fn with_variant(mut self, variant: BubbleVariant) -> Self {
        self.variant = variant;
        self
    }

    pub(crate) fn is_ghost(&self) -> bool {
        self.variant == BubbleVariant::Ghost
    }

    /// Replace the visible content surface.
    ///
    /// Children already added directly to the bubble move into the new
    /// surface, in front of its own children, so `.child(...)` composes the
    /// same way on either side of this call.
    pub fn content(mut self, mut content: BubbleContent) -> Self {
        let mut children = std::mem::take(&mut self.content.children);
        children.append(&mut content.children);
        content.children = children;
        self.content = content;
        self
    }

    /// Set an optional reaction region anchored to the bubble edge.
    pub fn reactions(mut self, reactions: BubbleReactions) -> Self {
        self.reactions = Some(reactions);
        self
    }
}

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

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

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

impl RenderOnce for Bubble {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        let variant = self.variant;
        let mut content = self.content;
        content.variant = variant;
        content.alignment = self.alignment;

        div()
            .relative()
            .flex()
            .min_w_0()
            .flex_col()
            .flex_none()
            .gap_1()
            .max_w(relative(0.8))
            .when(variant == BubbleVariant::Ghost, |this| {
                this.w_full().max_w_full()
            })
            .when_some(self.alignment, |this, alignment| match alignment {
                MessageAlignment::Start => this.self_start().mr_auto(),
                MessageAlignment::End => this.self_end().ml_auto(),
            })
            .refine_style(&self.style)
            .child(content)
            .when_some(self.reactions, |this, reactions| this.child(reactions))
    }
}

/// The visible surface inside a [`Bubble`].
///
/// This part owns padding, radius, border, typography, and semantic colors so
/// callers can refine the surface without changing the bubble's row layout.
#[derive(IntoElement)]
pub struct BubbleContent {
    style: StyleRefinement,
    variant: BubbleVariant,
    alignment: Option<MessageAlignment>,
    children: Vec<AnyElement>,
}

impl BubbleContent {
    /// Create an empty bubble content surface.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            variant: BubbleVariant::default(),
            alignment: None,
            children: Vec::new(),
        }
    }
}

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

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

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

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

        div()
            .min_w_0()
            .max_w_full()
            .overflow_hidden()
            .rounded(cx.theme().radius_2xl())
            .border_1()
            .border_color(cx.theme().transparent)
            .px_3()
            .py_2()
            .text_sm()
            .line_height(relative(1.625))
            .when_some(self.alignment, |this, alignment| match alignment {
                MessageAlignment::Start => this.self_start(),
                MessageAlignment::End => this.self_end(),
            })
            .map(|this| match self.variant {
                BubbleVariant::Filled => this
                    .bg(tokens.colors.primary)
                    .text_color(tokens.colors.primary_foreground),
                // The theme's `secondary` role is tuned for buttons and sits a
                // tier darker than shadcn's conversation secondary; the
                // near-background `muted` tier matches shadcn's value in both
                // light and dark themes.
                BubbleVariant::Secondary => this
                    .bg(tokens.colors.muted)
                    .text_color(tokens.colors.secondary_foreground),
                BubbleVariant::Muted => this
                    .bg(tokens.colors.muted)
                    .text_color(tokens.colors.foreground),
                BubbleVariant::Tinted => this
                    .bg(tokens.colors.primary.mix_oklab(
                        tokens.colors.background,
                        if cx.theme().is_dark() { 0.24 } else { 0.12 },
                    ))
                    .text_color(tokens.colors.foreground),
                BubbleVariant::Outline => this
                    .border_color(tokens.colors.border)
                    .bg(tokens.colors.background)
                    .text_color(tokens.colors.foreground),
                BubbleVariant::Ghost => this
                    .rounded(tokens.radius.none)
                    .border_0()
                    .bg(cx.theme().transparent)
                    .text_color(tokens.colors.foreground)
                    .p_0(),
                BubbleVariant::Destructive => this
                    .bg(tokens.colors.destructive.opacity(if cx.theme().is_dark() {
                        0.2
                    } else {
                        0.1
                    }))
                    .text_color(tokens.colors.destructive),
            })
            .refine_style(&self.style)
            .children(self.children)
    }
}

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

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

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

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

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

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

enum BubbleReactionChild {
    Action(Box<Button>),
    Element(AnyElement),
}

/// A styleable reaction region positioned on a bubble edge.
///
/// Compose existing [`crate::button::Button`] values inside this region to
/// preserve button semantics and keyboard behavior.
#[derive(IntoElement)]
pub struct BubbleReactions {
    style: StyleRefinement,
    side: BubbleReactionSide,
    alignment: MessageAlignment,
    children: Vec<BubbleReactionChild>,
}

impl BubbleReactions {
    /// Create a trailing-aligned reaction region on the lower edge.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            side: BubbleReactionSide::Bottom,
            alignment: MessageAlignment::End,
            children: Vec::new(),
        }
    }

    /// Set the edge on which reactions are positioned.
    pub fn side(mut self, side: BubbleReactionSide) -> Self {
        self.side = side;
        self
    }

    /// Set the reaction region alignment along the bubble edge.
    pub fn alignment(mut self, alignment: MessageAlignment) -> Self {
        self.alignment = alignment;
        self
    }

    /// Add an interactive action that shares the reaction surface.
    ///
    /// Typed actions remove the reaction region's decorative content padding
    /// and use the theme's full radius so the button reads as part of one
    /// surface. The typed action owns that pill geometry; use `.child(...)`
    /// for emoji, labels, and arbitrary elements when the child should retain
    /// its own styling, including a custom button radius.
    pub fn action(mut self, action: Button) -> Self {
        self.children
            .push(BubbleReactionChild::Action(Box::new(action)));
        self
    }
}

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

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

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

impl RenderOnce for BubbleReactions {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let tokens = cx.theme().semantic_tokens();
        let has_action = self
            .children
            .iter()
            .any(|child| matches!(child, BubbleReactionChild::Action(_)));
        let action_radius = cx.theme().radius_full();
        let children = self.children.into_iter().map(move |child| match child {
            BubbleReactionChild::Action(action) => {
                (*action).rounded(action_radius).into_any_element()
            }
            BubbleReactionChild::Element(element) => element,
        });

        div()
            .absolute()
            .flex()
            .flex_none()
            .items_center()
            .justify_center()
            .gap_1()
            .rounded(cx.theme().radius_full())
            .border_3()
            .border_color(tokens.colors.background)
            .bg(tokens.colors.muted)
            .text_color(tokens.colors.foreground)
            .when(!has_action, |this| this.px_1p5().py_0p5())
            .text_sm()
            // Approximates shadcn's `translate-y-3/4`: GPUI cannot offset by a
            // fraction of the pill's own height, so this fixed value leaves
            // about three quarters of the default pill outside the bubble.
            .when(self.side == BubbleReactionSide::Top, |this| {
                this.top(-rems(1.25))
            })
            .when(self.side == BubbleReactionSide::Bottom, |this| {
                this.bottom(-rems(1.25))
            })
            .when(self.alignment == MessageAlignment::Start, |this| {
                this.left_3()
            })
            .when(self.alignment == MessageAlignment::End, |this| {
                this.right_3()
            })
            .refine_style(&self.style)
            .children(children)
    }
}

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

    #[test]
    fn test_bubble_builder() {
        let bubble = Bubble::new()
            .alignment(MessageAlignment::End)
            .with_variant(BubbleVariant::Outline)
            .content(BubbleContent::new().child("Hello"))
            .reactions(BubbleReactions::new().child("👍"));

        assert_eq!(bubble.alignment, Some(MessageAlignment::End));
        assert_eq!(bubble.variant, BubbleVariant::Outline);
        assert_eq!(bubble.content.children.len(), 1);
        assert!(bubble.reactions.is_some());

        // Direct children survive a later `content(...)` call and stay in
        // front of the new surface's own children.
        let reordered = Bubble::new()
            .child("Existing")
            .content(BubbleContent::new().child("Configured"));
        assert_eq!(reordered.content.children.len(), 2);

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

        let reactions = BubbleReactions::new()
            .side(BubbleReactionSide::Top)
            .alignment(MessageAlignment::Start)
            .child("👍 2");

        assert_eq!(reactions.side, BubbleReactionSide::Top);
        assert_eq!(reactions.alignment, MessageAlignment::Start);
        assert_eq!(reactions.children.len(), 1);

        let reactions = BubbleReactions::new()
            .action(Button::new("reaction-action"))
            .child("👍");
        assert_eq!(reactions.children.len(), 2);
        assert!(matches!(
            reactions.children.first(),
            Some(BubbleReactionChild::Action(_))
        ));
        assert!(matches!(
            reactions.children.get(1),
            Some(BubbleReactionChild::Element(_))
        ));
    }
}