A framed message surface with variants, alignment, grouping and reactions.
use gpui::{div, prelude::*, px, svg, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::bubble::{Bubble, BubbleAlign, BubbleGroup, BubbleVariant};
use crate::icons::{HEART, THUMBS_UP};
pub fn bubble_demo(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let (fg, destructive) = (theme.foreground, theme.destructive);
let reaction = |data: &'static [u8], color| svg().data(data).size(px(14.0)).text_color(color);
div()
.w(px(480.0))
.flex()
.flex_col()
.gap_3()
.child(
BubbleGroup::new()
.child(
Bubble::new("question")
.variant(BubbleVariant::Muted)
.child("Did the registry build pass after the rename?"),
)
.child(
Bubble::new("follow-up")
.variant(BubbleVariant::Muted)
.child("The docs page still links to the old route."),
),
)
.child(
div().pb(px(14.0)).flex().flex_col().child(
Bubble::new("answer")
.align(BubbleAlign::End)
.reactions(
div()
.flex()
.items_center()
.gap_1()
.child(reaction(THUMBS_UP, fg))
.child(reaction(HEART, destructive)),
)
.child("It passed. I checked the registry output and removed the stale route."),
),
)
.child(
Bubble::new("tinted")
.variant(BubbleVariant::Tinted)
.child("Preview deployed to staging."),
)
.child(
Bubble::new("failed")
.align(BubbleAlign::End)
.variant(BubbleVariant::Destructive)
.on_click(|_, _, _| {})
.child("Message failed to send. Click to retry."),
)
}Installation
Copies src/ui/bubble.rs into your project, along with any components it depends on, and registers the module in src/ui/mod.rs.
Install the theme
Every component reads colors from
crate::theme. Follow the installation guide if you have not added it yet.Copy the source
Paste the code below into
src/ui/bubble.rs.src/ui/bubble.rs use gpui::{div, prelude::*, px, relative, AnyElement, App, ClickEvent, ElementId, Pixels, Window}; use crate::theme::ActiveTheme; #[derive(Clone, Copy, Default, PartialEq)] pub enum BubbleVariant { /// Primary fill, for the reader's own messages. #[default] Default, Secondary, Muted, /// A faint wash of the primary color. Tinted, Outline, /// No frame: plain text that can span the full row, for long assistant answers. Ghost, /// For errors and failed sends. Destructive, } /// Which side of the row the bubble sits on. #[derive(Clone, Copy, Default, PartialEq)] pub enum BubbleAlign { #[default] Start, End, } /// Which edge of the bubble the reactions overlap. #[derive(Clone, Copy, Default, PartialEq)] pub enum ReactionsSide { Top, #[default] Bottom, } /// Reactions sit 3/4 outside the bubble, like shadcn's `translate-y-3/4` on a 24px pill. const REACTIONS_OFFSET: Pixels = px(-18.0); /// The card-colored ring around the reactions, so they read as lifted off the bubble's edge. const REACTIONS_RING: Pixels = px(3.0); type ClickHandler = Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>; /// A framed message surface. It sizes to its text, up to 80% of the row. Children are the /// message content. #[derive(IntoElement)] pub struct Bubble { id: ElementId, variant: BubbleVariant, align: BubbleAlign, reactions: Option<(AnyElement, ReactionsSide, BubbleAlign)>, children: Vec<AnyElement>, on_click: Option<ClickHandler>, } impl Bubble { pub fn new(id: impl Into<ElementId>) -> Self { Self { id: id.into(), variant: BubbleVariant::default(), align: BubbleAlign::default(), reactions: None, children: Vec::new(), on_click: None, } } pub fn variant(mut self, variant: BubbleVariant) -> Self { self.variant = variant; self } pub fn align(mut self, align: BubbleAlign) -> Self { self.align = align; self } /// A pill of reactions overlapping the bottom-end corner. Leave extra vertical space /// between rows, since it hangs outside the bubble. pub fn reactions(self, reactions: impl IntoElement) -> Self { self.reactions_at(reactions, ReactionsSide::Bottom, BubbleAlign::End) } /// Reactions on a chosen edge and corner. pub fn reactions_at( mut self, reactions: impl IntoElement, side: ReactionsSide, align: BubbleAlign, ) -> Self { self.reactions = Some((reactions.into_any_element(), side, align)); self } /// Makes the bubble a button, e.g. to open a collapsed message or retry a failed send. pub fn on_click( mut self, handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, ) -> Self { self.on_click = Some(Box::new(handler)); self } } impl ParentElement for Bubble { fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { self.children.extend(elements); } } impl RenderOnce for Bubble { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { let theme = cx.theme(); let transparent = gpui::transparent_black(); let (bg, fg, border, hover_bg) = match self.variant { BubbleVariant::Default => ( theme.primary, theme.primary_foreground, transparent, theme.primary.opacity(0.8), ), BubbleVariant::Secondary => ( theme.secondary, theme.secondary_foreground, transparent, theme.secondary.blend(theme.foreground.opacity(0.05)), ), BubbleVariant::Muted => ( theme.muted, theme.foreground, transparent, theme.muted.blend(theme.foreground.opacity(0.05)), ), // shadcn derives this from the primary hue in oklch; there is no tint token, so a // thin layer of primary stands in for it. BubbleVariant::Tinted => ( theme.primary.opacity(0.1), theme.foreground, transparent, theme.primary.opacity(0.16), ), BubbleVariant::Outline => ( theme.background, theme.foreground, theme.border, theme.muted, ), BubbleVariant::Ghost => (transparent, theme.foreground, transparent, theme.muted), BubbleVariant::Destructive => ( theme.destructive.opacity(0.1), theme.destructive, transparent, theme.destructive.opacity(0.2), ), }; let ghost = self.variant == BubbleVariant::Ghost; let end = self.align == BubbleAlign::End; let clickable = self.on_click.is_some(); // shadcn's AI elements round bubbles to 22px, so a one-line bubble reads as a pill. let radius = px(22.0); let (ring, reaction_bg) = (theme.card, theme.muted); let content = div() .id(self.id) .max_w_full() .min_w_0() .when(end, |this| this.self_end()) .when(!end, |this| this.self_start()) .text_sm() .line_height(relative(1.625)) .text_color(fg) .bg(bg) .when(!ghost, |this| { this.rounded(radius) .border_1() .border_color(border) .px_3() .py(px(10.0)) }) .when(ghost && clickable, |this| this.rounded(radius)) .when_some(self.on_click, |this, handler| { this.cursor_pointer() .hover(move |style| style.bg(hover_bg)) .on_click(move |event, window, cx| handler(event, window, cx)) }) .children(self.children); div() .relative() .flex() .flex_col() .min_w_0() .when(end, |this| this.self_end()) .when(!end, |this| this.self_start()) .map(|this| { if ghost { this.max_w_full() } else { this.max_w(relative(0.8)) } }) .child(content) .when_some(self.reactions, |this, (reactions, side, align)| { let offset = REACTIONS_OFFSET - REACTIONS_RING; this.child( div() .absolute() .map(|this| match side { ReactionsSide::Top => this.top(offset), ReactionsSide::Bottom => this.bottom(offset), }) .map(|this| match align { BubbleAlign::Start => this.left(px(12.0) - REACTIONS_RING), BubbleAlign::End => this.right(px(12.0) - REACTIONS_RING), }) .rounded_full() .border(REACTIONS_RING) .border_color(ring) .bg(ring) .child( div() .flex() .items_center() .gap_1() .h(px(24.0)) .px(px(6.0)) .rounded_full() .bg(reaction_bg) .text_sm() .text_color(theme.foreground) .child(reactions), ), ) }) } } /// Stacks consecutive bubbles from the same sender. #[derive(IntoElement, Default)] pub struct BubbleGroup { children: Vec<AnyElement>, } impl BubbleGroup { pub fn new() -> Self { Self::default() } } impl ParentElement for BubbleGroup { fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { self.children.extend(elements); } } impl RenderOnce for BubbleGroup { fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement { div() .flex() .flex_col() .min_w_0() .gap_2() .children(self.children) } }Register the module
// src/ui/mod.rs pub mod bubble;
Usage
use crate::ui::bubble::*;API Reference
BubbleVariant
BubbleVariant::DefaultPrimary fill, for the reader's own messages.
BubbleVariant::SecondaryBubbleVariant::MutedBubbleVariant::TintedA faint wash of the primary color.
BubbleVariant::OutlineBubbleVariant::GhostNo frame: plain text that can span the full row, for long assistant answers.
BubbleVariant::DestructiveFor errors and failed sends.
BubbleAlign
Which side of the row the bubble sits on.
BubbleAlign::StartBubbleAlign::EndReactionsSide
Which edge of the bubble the reactions overlap.
ReactionsSide::TopReactionsSide::BottomBubble
A framed message surface. It sizes to its text, up to 80% of the row. Children are the message content.
Bubble::new(id: impl Into<ElementId>) -> Self.variant(variant: BubbleVariant).align(align: BubbleAlign)Bubble::reactions(self, reactions: impl IntoElement) -> SelfA pill of reactions overlapping the bottom-end corner. Leave extra vertical space between rows, since it hangs outside the bubble.
.reactions_at(reactions: impl IntoElement, side: ReactionsSide, align: BubbleAlign)Reactions on a chosen edge and corner.
.on_click(handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static)Makes the bubble a button, e.g. to open a collapsed message or retry a failed send.
BubbleGroup
Stacks consecutive bubbles from the same sender.
BubbleGroup::new() -> Self