use super::form::rem;
use crate::{
theme::color,
tokens::{
BORDER_RADIUS_00, BORDER_RADIUS_08, BORDER_RADIUS_16, ColorRole, SPACING_02, SPACING_03,
SPACING_04, SPACING_05, TypeStyle,
},
typography::TypeStyled as _,
};
use gpui_kit::{
AnyElement, App, Hsla, IntoElement, ParentElement, RenderOnce, StyleRefinement, Styled, Window,
div, prelude::FluentBuilder as _, px, relative,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum BubbleVariant {
#[default]
Default,
Secondary,
Muted,
Tinted,
Outline,
Ghost,
Destructive,
}
impl BubbleVariant {
pub const ALL: [BubbleVariant; 7] = [
Self::Default,
Self::Secondary,
Self::Muted,
Self::Tinted,
Self::Outline,
Self::Ghost,
Self::Destructive,
];
pub const fn name(self) -> &'static str {
match self {
Self::Default => "default",
Self::Secondary => "secondary",
Self::Muted => "muted",
Self::Tinted => "tinted",
Self::Outline => "outline",
Self::Ghost => "ghost",
Self::Destructive => "destructive",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum BubbleAlign {
#[default]
Start,
End,
}
impl BubbleAlign {
pub const ALL: [BubbleAlign; 2] = [Self::Start, Self::End];
pub const fn name(self) -> &'static str {
match self {
Self::Start => "start",
Self::End => "end",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ReactionsSide {
Top,
#[default]
Bottom,
}
impl ReactionsSide {
pub const ALL: [ReactionsSide; 2] = [Self::Top, Self::Bottom];
pub const fn name(self) -> &'static str {
match self {
Self::Top => "top",
Self::Bottom => "bottom",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BubbleLook {
pub background: Hsla,
pub text: Hsla,
pub outline: Option<Hsla>,
pub flush: bool,
}
impl BubbleLook {
pub fn of(variant: BubbleVariant) -> Self {
use ColorRole::*;
let clear = Hsla::transparent_black();
let (background, text, outline) = match variant {
BubbleVariant::Default => (
color(ChatBubbleUser),
color(ChatBubbleUserText),
Some(color(ChatBubbleBorder)),
),
BubbleVariant::Secondary => (
color(ChatBubbleAgent),
color(ChatBubbleAgentText),
Some(color(ChatBubbleBorder)),
),
BubbleVariant::Muted => (color(Layer01), color(TextPrimary), None),
BubbleVariant::Tinted => (color(NotificationBackgroundInfo), color(TextPrimary), None),
BubbleVariant::Outline => (clear, color(TextPrimary), Some(color(BorderStrong01))),
BubbleVariant::Ghost => (clear, color(TextPrimary), None),
BubbleVariant::Destructive => (
color(NotificationBackgroundError),
color(TextPrimary),
Some(color(SupportError)),
),
};
Self {
background,
text,
outline,
flush: variant == BubbleVariant::Ghost,
}
}
}
pub const MAX_WIDTH: f32 = 576.;
pub const MAX_FRACTION: f32 = 0.85;
pub const REACTIONS_OVERHANG: f32 = 12.;
pub fn corner_radii(align: BubbleAlign) -> [f32; 4] {
let (r, square) = (BORDER_RADIUS_08.px_value(), BORDER_RADIUS_00.px_value());
match align {
BubbleAlign::End => [r, r, square, r],
BubbleAlign::Start => [r, r, r, square],
}
}
#[derive(IntoElement, Default)]
pub struct Bubble {
variant: BubbleVariant,
align: BubbleAlign,
fill_message: bool,
style: StyleRefinement,
children: Vec<AnyElement>,
}
impl Bubble {
pub fn new() -> Self {
Self::default()
}
pub fn variant(mut self, variant: BubbleVariant) -> Self {
self.variant = variant;
self
}
pub fn align(mut self, align: BubbleAlign) -> Self {
self.align = align;
self
}
pub fn fill_message(mut self, fill: bool) -> Self {
self.fill_message = fill;
self
}
}
impl Styled for Bubble {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl ParentElement for Bubble {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Bubble {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let look = BubbleLook::of(self.variant);
let [tl, tr, br, bl] = corner_radii(self.align).map(rem);
let round = move |el: gpui_kit::Div| {
el.rounded_tl(tl)
.rounded_tr(tr)
.rounded_br(br)
.rounded_bl(bl)
};
let mut bubble = round(div())
.relative()
.flex()
.flex_col()
.min_w_0()
.when(!self.fill_message, |this| this.max_w(rem(MAX_WIDTH)))
.py(SPACING_04)
.when(!look.flush, |this| this.px(SPACING_05))
.bg(look.background)
.text_color(look.text)
.type_style(TypeStyle::Body01)
.children(self.children)
.when_some(look.outline, |this, line| {
this.child(
round(div())
.absolute()
.top_0()
.left_0()
.size_full()
.border_1()
.border_color(line),
)
});
gpui_kit::Refineable::refine(bubble.style(), &self.style);
div()
.flex()
.flex_col()
.min_w_0()
.map(|this| match self.align {
BubbleAlign::Start => this.self_start(),
BubbleAlign::End => this.self_end(),
})
.map(|this| {
if self.fill_message {
this.max_w_full()
} else {
this.max_w(relative(MAX_FRACTION))
}
})
.child(bubble)
}
}
#[derive(IntoElement, Default)]
pub struct BubbleContent {
children: Vec<AnyElement>,
}
impl BubbleContent {
pub fn new() -> Self {
Self::default()
}
}
impl ParentElement for BubbleContent {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for BubbleContent {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div().flex().flex_col().children(self.children)
}
}
#[derive(IntoElement, Default)]
pub struct BubbleGroup {
style: StyleRefinement,
children: Vec<AnyElement>,
}
impl BubbleGroup {
pub fn new() -> Self {
Self::default()
}
}
impl Styled for BubbleGroup {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl ParentElement for BubbleGroup {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for BubbleGroup {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let mut group = div()
.flex()
.flex_col()
.gap(SPACING_02)
.children(self.children);
gpui_kit::Refineable::refine(group.style(), &self.style);
group
}
}
#[derive(IntoElement, Default)]
pub struct BubbleReactions {
side: ReactionsSide,
align: Option<BubbleAlign>,
children: Vec<AnyElement>,
}
impl BubbleReactions {
pub fn new() -> Self {
Self::default()
}
pub fn side(mut self, side: ReactionsSide) -> Self {
self.side = side;
self
}
pub fn align(mut self, align: BubbleAlign) -> Self {
self.align = Some(align);
self
}
}
impl ParentElement for BubbleReactions {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for BubbleReactions {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let overhang = px(-REACTIONS_OVERHANG);
let r = BORDER_RADIUS_16;
div()
.absolute()
.map(|this| match self.side {
ReactionsSide::Top => this.top(overhang),
ReactionsSide::Bottom => this.bottom(overhang),
})
.map(|this| match self.align.unwrap_or(BubbleAlign::End) {
BubbleAlign::End => this.right(SPACING_03),
BubbleAlign::Start => this.left(SPACING_03),
})
.flex()
.flex_row()
.gap(px(2.))
.px(SPACING_02)
.rounded(r)
.bg(color(ColorRole::Layer02))
.text_color(color(ColorRole::TextPrimary))
.text_size(rem(12.))
.line_height(rem(20.))
.children(self.children)
.child(
div()
.absolute()
.top(px(-1.))
.left(px(-1.))
.right(px(-1.))
.bottom(px(-1.))
.rounded(r)
.border_1()
.border_color(color(ColorRole::BorderSubtle01)),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn variants_follow_custom_css() {
crate::theme::tests_set(CarbonTheme::White);
let user = BubbleLook::of(BubbleVariant::Default);
assert_eq!(user.background, color(ColorRole::ChatBubbleUser));
assert_eq!(user.text, color(ColorRole::ChatBubbleUserText));
assert_eq!(user.outline, Some(color(ColorRole::ChatBubbleBorder)));
let agent = BubbleLook::of(BubbleVariant::Secondary);
assert_eq!(agent.background, color(ColorRole::ChatBubbleAgent));
assert_eq!(agent.text, color(ColorRole::ChatBubbleAgentText));
assert_eq!(
BubbleLook::of(BubbleVariant::Muted).background,
color(ColorRole::Layer01)
);
assert_eq!(
BubbleLook::of(BubbleVariant::Tinted).background,
color(ColorRole::NotificationBackgroundInfo)
);
let outline = BubbleLook::of(BubbleVariant::Outline);
assert_eq!(outline.background.a, 0.);
assert_eq!(outline.outline, Some(color(ColorRole::BorderStrong01)));
assert!(BubbleLook::of(BubbleVariant::Ghost).flush);
let error = BubbleLook::of(BubbleVariant::Destructive);
assert_eq!(
error.background,
color(ColorRole::NotificationBackgroundError)
);
assert_eq!(error.outline, Some(color(ColorRole::SupportError)));
}
#[test]
fn geometry_follows_custom_css() {
assert_eq!((MAX_WIDTH, MAX_FRACTION), (576., 0.85));
assert_eq!((SPACING_04.px_value(), SPACING_05.px_value()), (12., 16.));
assert_eq!(REACTIONS_OVERHANG, 12.);
assert_eq!(SPACING_02.px_value(), 4.);
}
#[test]
fn corners_follow_custom_css_not_the_t3_craft_pass() {
assert_eq!(corner_radii(BubbleAlign::End), [8., 8., 0., 8.]);
assert_eq!(corner_radii(BubbleAlign::Start), [8., 8., 8., 0.]);
}
}