use super::bubble::BubbleAlign;
use super::form::rem;
use crate::{
icons::{CarbonIcon, Icon, IconSize},
theme::color,
tokens::{ColorRole, SPACING_02, SPACING_03, SPACING_04, SPACING_06, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
AnyElement, App, FontWeight, Hsla, IntoElement, ParentElement, RenderOnce, SharedString,
StyleRefinement, Styled, Window, div, prelude::FluentBuilder as _,
};
pub type MessageAlign = BubbleAlign;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum AvatarKind {
#[default]
User,
Agent,
}
impl AvatarKind {
pub const ALL: [AvatarKind; 2] = [Self::User, Self::Agent];
pub const fn name(self) -> &'static str {
match self {
Self::User => "user",
Self::Agent => "agent",
}
}
pub fn colors(self) -> (Hsla, Hsla) {
match self {
Self::User => (
color(ColorRole::ChatAvatarUser),
color(ColorRole::TextOnColor),
),
Self::Agent => (
color(ColorRole::ChatAvatarAgent),
color(ColorRole::TextInverse),
),
}
}
}
pub const AVATAR: f32 = 32.;
#[derive(IntoElement, Default)]
pub struct MessageAvatar {
kind: AvatarKind,
text: Option<SharedString>,
children: Vec<AnyElement>,
}
impl MessageAvatar {
pub fn user() -> Self {
Self::default()
}
pub fn agent(text: impl Into<SharedString>) -> Self {
Self {
kind: AvatarKind::Agent,
text: Some(text.into()),
children: Vec::new(),
}
}
pub fn new(kind: AvatarKind) -> Self {
Self {
kind,
..Self::default()
}
}
pub fn text(mut self, text: impl Into<SharedString>) -> Self {
self.text = Some(text.into());
self
}
}
impl ParentElement for MessageAvatar {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for MessageAvatar {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let (bg, fg) = self.kind.colors();
let base = div()
.flex()
.flex_none()
.size(rem(AVATAR))
.items_center()
.justify_center()
.rounded_full()
.bg(bg)
.text_color(fg);
if !self.children.is_empty() {
return base.children(self.children);
}
match (self.kind, self.text) {
(AvatarKind::User, None) => {
base.child(Icon::carbon(CarbonIcon::User).size(IconSize::S16).color(fg))
}
(_, text) => base.child(
div()
.text_size(rem(12.))
.font_weight(FontWeight::SEMIBOLD)
.children(text),
),
}
}
}
#[derive(IntoElement, Default)]
pub struct MessageContent {
align: MessageAlign,
children: Vec<AnyElement>,
}
impl MessageContent {
pub fn new() -> Self {
Self::default()
}
}
impl ParentElement for MessageContent {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for MessageContent {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.flex()
.flex_1()
.flex_col()
.gap(SPACING_02)
.min_w_0()
.when(self.align == MessageAlign::End, |this| this.items_end())
.children(self.children)
}
}
#[derive(IntoElement, Default)]
pub struct MessageHeader {
name: Option<SharedString>,
children: Vec<AnyElement>,
}
impl MessageHeader {
pub fn new() -> Self {
Self::default()
}
pub fn name(mut self, name: impl Into<SharedString>) -> Self {
self.name = Some(name.into());
self
}
}
impl ParentElement for MessageHeader {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for MessageHeader {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.flex()
.flex_row()
.items_baseline()
.gap(SPACING_03)
.type_style(TypeStyle::Label01)
.text_color(color(ColorRole::TextSecondary))
.when_some(self.name, |this, name| {
this.child(
div()
.font_weight(FontWeight::SEMIBOLD)
.text_color(color(ColorRole::TextPrimary))
.child(name),
)
})
.children(self.children)
}
}
#[derive(IntoElement, Default)]
pub struct MessageFooter {
children: Vec<AnyElement>,
}
impl MessageFooter {
pub fn new() -> Self {
Self::default()
}
}
impl ParentElement for MessageFooter {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for MessageFooter {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.flex()
.flex_row()
.items_center()
.gap(SPACING_02)
.text_size(rem(12.))
.text_color(color(ColorRole::TextHelper))
.children(self.children)
}
}
#[derive(IntoElement, Default)]
pub struct Message {
align: MessageAlign,
avatar: Option<MessageAvatar>,
content: Option<MessageContent>,
children: Vec<AnyElement>,
}
impl Message {
pub fn new() -> Self {
Self::default()
}
pub fn align(mut self, align: MessageAlign) -> Self {
self.align = align;
self
}
pub fn avatar(mut self, avatar: MessageAvatar) -> Self {
self.avatar = Some(avatar);
self
}
pub fn content(mut self, content: MessageContent) -> Self {
self.content = Some(content);
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, _: &mut Window, _: &mut App) -> impl IntoElement {
let align = self.align;
div()
.flex()
.map(|this| match align {
MessageAlign::Start => this.flex_row(),
MessageAlign::End => this.flex_row_reverse(),
})
.items_start()
.gap(SPACING_04)
.children(self.avatar)
.children(self.content.map(|mut c| {
c.align = align;
c
}))
.children(self.children)
}
}
#[derive(IntoElement, Default)]
pub struct MessageGroup {
style: StyleRefinement,
children: Vec<AnyElement>,
}
impl MessageGroup {
pub fn new() -> Self {
Self::default()
}
}
impl Styled for MessageGroup {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl ParentElement for MessageGroup {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for MessageGroup {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let mut group = div()
.flex()
.flex_col()
.gap(SPACING_06)
.children(self.children);
gpui_kit::Refineable::refine(group.style(), &self.style);
group
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn avatars_follow_custom_css() {
crate::theme::tests_set(CarbonTheme::White);
assert_eq!(
AvatarKind::User.colors(),
(
color(ColorRole::ChatAvatarUser),
color(ColorRole::TextOnColor)
)
);
assert_eq!(
AvatarKind::Agent.colors(),
(
color(ColorRole::ChatAvatarAgent),
color(ColorRole::TextInverse)
)
);
assert_eq!(AVATAR, 32.);
assert_eq!((SPACING_04.px_value(), SPACING_06.px_value()), (12., 24.));
}
}