One message in a conversation, with optional avatar, header, footer and alignment.
use gpui::{div, prelude::*, px, svg, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::avatar::Avatar;
use crate::ui::bubble::{Bubble, BubbleVariant};
use crate::ui::button::{Button, ButtonSize, ButtonVariant};
use crate::ui::marker::Marker;
use crate::ui::message::{Message, MessageAlign};
use crate::ui::spinner::Spinner;
use crate::icons::{COPY, REFRESH_CCW};
pub fn message_demo(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let muted = cx.theme().muted_foreground;
let action = |id: &'static str, icon: &'static [u8]| {
Button::new(id, "")
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Xs)
.icon_only()
.leading(svg().data(icon).size(px(14.0)).text_color(muted))
};
div()
.w(px(520.0))
.flex()
.flex_col()
.gap_4()
.child(
Message::new()
.align(MessageAlign::End)
.avatar(Avatar::new("JL"))
.child(
Bubble::new("question")
.child("Can you summarize what changed in the last release?"),
),
)
.child(
Message::new()
.avatar(Avatar::new("AI"))
.header("Assistant")
.footer(
div()
.flex()
.gap_1()
.child(action("copy", COPY))
.child(action("retry", REFRESH_CCW)),
)
.child(Bubble::new("answer").variant(BubbleVariant::Muted).child(
"Version 2.4 adds keyboard navigation to menus, fixes focus loss in \
dialogs and drops the deprecated theme API.",
)),
)
.child(
Message::new().child(
Marker::new("status")
.icon(Spinner::new().color(muted))
.shimmer(true)
.child("Checking the changelog..."),
),
)
}Installation
Copies src/ui/message.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/message.rs.src/ui/message.rs use gpui::{div, prelude::*, px, AnyElement, App, FontWeight, Pixels, Window}; use crate::theme::ActiveTheme; /// Which side of the conversation the message sits on. #[derive(Clone, Copy, Default, PartialEq)] pub enum MessageAlign { /// The other party: avatar on the left. #[default] Start, /// The reader: avatar on the right, bubble and footer pushed to the right edge. End, } /// Room for a default 32px avatar, like shadcn's `min-w-8`. const AVATAR_SLOT: Pixels = px(32.0); const GAP: Pixels = px(8.0); /// shadcn's `MessageContent` gap (`gap-2.5`) between the header, surfaces and footer. const CONTENT_GAP: Pixels = px(10.0); /// One message in a conversation: an optional avatar, a header such as the sender's name, /// the message surface (usually a `Bubble`, added as children) and a footer for status or /// actions. #[derive(IntoElement)] pub struct Message { align: MessageAlign, avatar: Option<AnyElement>, header: Option<AnyElement>, footer: Option<AnyElement>, children: Vec<AnyElement>, gap: Pixels, flush: bool, } impl Default for Message { fn default() -> Self { Self { align: MessageAlign::default(), avatar: None, header: None, footer: None, children: Vec::new(), gap: CONTENT_GAP, flush: false, } } } impl Message { pub fn new() -> Self { Self::default() } /// The space between the header, each child and the footer; 10px by default. An agent's /// reply often sets its own, like `<MessageContent className="gap-3">`. pub fn gap(mut self, gap: impl Into<Pixels>) -> Self { self.gap = gap.into(); self } /// Drops the 12px inset of the header and footer, which lines them up with a bubble's /// text. Use it when the message has no bubble (plain text, markdown or a ghost bubble), /// like shadcn's `px-0` footer. pub fn flush(mut self) -> Self { self.flush = true; self } pub fn align(mut self, align: MessageAlign) -> Self { self.align = align; self } /// Usually an `Avatar`. It lines up with the bottom of the message surface, above the /// footer. pub fn avatar(mut self, avatar: impl IntoElement) -> Self { self.avatar = Some(avatar.into_any_element()); self } /// Text or elements above the message, such as the sender's name and a time. pub fn header(mut self, header: impl IntoElement) -> Self { self.header = Some(header.into_any_element()); self } /// Text or elements below the message, such as "Read" or copy and retry buttons. It /// follows the message's side. pub fn footer(mut self, footer: impl IntoElement) -> Self { self.footer = Some(footer.into_any_element()); self } } impl ParentElement for Message { fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { self.children.extend(elements); } } impl RenderOnce for Message { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { let muted_fg = cx.theme().muted_foreground; let end = self.align == MessageAlign::End; // The header and footer sit beside the avatar's column, not under it, so the avatar // stays level with the bottom of the bubble whatever the footer holds. let indent = self.avatar.is_some().then_some(AVATAR_SLOT + GAP); let (gap, flush) = (self.gap, self.flush); let caption = move |content: AnyElement| { div() .flex() .items_center() .min_w_0() .when(!flush, |this| this.px_3()) .text_xs() .font_weight(FontWeight::MEDIUM) .text_color(muted_fg) .when(end, |this| this.justify_end()) .when_some(indent, |this, indent| { if end { this.mr(indent) } else { this.ml(indent) } }) .child(content) }; let body = div() .flex() .items_end() .gap(GAP) .w_full() .min_w_0() .when(end, |this| this.flex_row_reverse()) .when_some(self.avatar, |this, avatar| { this.child( div() .flex() .flex_none() .min_w(AVATAR_SLOT) .justify_center() .child(avatar), ) }) .child( div() .flex() .flex_col() .flex_1() .min_w_0() .gap(gap) // Each child gets its own row so a bubble keeps its content width (a column // would stretch it) and can be pushed to the message's side. .children(self.children.into_iter().map(|child| { div() .flex() .w_full() .min_w_0() .when(end, |this| this.justify_end()) .child(child) })), ); div() .flex() .flex_col() .gap(gap) .w_full() .min_w_0() .text_sm() .when_some(self.header, |this, header| this.child(caption(header))) .child(body) .when_some(self.footer, |this, footer| this.child(caption(footer))) } } /// Stacks consecutive messages from the same sender. #[derive(IntoElement, Default)] pub struct MessageGroup { children: Vec<AnyElement>, } impl MessageGroup { pub fn new() -> Self { Self::default() } } impl ParentElement for MessageGroup { fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { self.children.extend(elements); } } impl RenderOnce for MessageGroup { fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement { div() .flex() .flex_col() .w_full() .min_w_0() .gap_2() .children(self.children) } }Register the module
// src/ui/mod.rs pub mod message;
Usage
use crate::ui::message::*;API Reference
MessageAlign
Which side of the conversation the message sits on.
MessageAlign::StartThe other party: avatar on the left.
MessageAlign::EndThe reader: avatar on the right, bubble and footer pushed to the right edge.
Message
One message in a conversation: an optional avatar, a header such as the sender's name, the message surface (usually a Bubble, added as children) and a footer for status or actions.
Message::new() -> Self.gap(gap: impl Into<Pixels>)The space between the header, each child and the footer; 10px by default. An agent's reply often sets its own, like <MessageContent className="gap-3">.
.flush()Drops the 12px inset of the header and footer, which lines them up with a bubble's text. Use it when the message has no bubble (plain text, markdown or a ghost bubble), like shadcn's px-0 footer.
.align(align: MessageAlign).avatar(avatar: impl IntoElement)Usually an Avatar. It lines up with the bottom of the message surface, above the footer.
.header(header: impl IntoElement)Text or elements above the message, such as the sender's name and a time.
.footer(footer: impl IntoElement)Text or elements below the message, such as "Read" or copy and retry buttons. It follows the message's side.
MessageGroup
Stacks consecutive messages from the same sender.
MessageGroup::new() -> Self