use std::time::Duration;
use arc_motion::{arc_spring, arc_transition, is_reduced_motion};
use arc_tokens::{ActiveArcTheme as _, Radii, Typography};
use gpui_kit::{
App, ElementId, IntoElement, ObjectFit, ParentElement, RenderOnce, SharedString, Styled,
Window, base::StyledExt as _, div, img, prelude::*, px,
};
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum AvatarSize {
Sm,
#[default]
Md,
Lg,
Xl,
}
impl AvatarSize {
pub const fn px_size(self) -> f32 {
match self {
Self::Sm => 28.0,
Self::Md => 36.0,
Self::Lg => 48.0,
Self::Xl => 88.0,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AvatarStatus {
Online,
Offline,
}
pub fn initials_for(name: &str) -> String {
name.split_whitespace()
.take(2)
.filter_map(|part| part.chars().next())
.flat_map(char::to_uppercase)
.collect()
}
#[derive(IntoElement)]
pub struct Avatar {
id: ElementId,
name: SharedString,
src: Option<SharedString>,
size: AvatarSize,
status: Option<AvatarStatus>,
ring_border: bool,
}
impl Avatar {
pub fn new(id: impl Into<ElementId>, name: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
name: name.into(),
src: None,
size: AvatarSize::Md,
status: None,
ring_border: false,
}
}
pub fn src(mut self, src: impl Into<SharedString>) -> Self {
self.src = Some(src.into());
self
}
pub fn src_opt(mut self, src: Option<SharedString>) -> Self {
self.src = src;
self
}
pub fn size(mut self, size: AvatarSize) -> Self {
self.size = size;
self
}
pub fn status(mut self, status: AvatarStatus) -> Self {
self.status = Some(status);
self
}
pub fn status_opt(mut self, status: Option<AvatarStatus>) -> Self {
self.status = status;
self
}
pub fn ring_border(mut self, ring: bool) -> Self {
self.ring_border = ring;
self
}
}
impl RenderOnce for Avatar {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.arc_theme().clone();
let colors = theme.colors;
let motion = theme.motion;
let reduced = is_reduced_motion(cx);
let dim = self.size.px_size();
let font_sz = match self.size {
AvatarSize::Sm => px(10.0),
AvatarSize::Md => Typography::TEXT_XS,
AvatarSize::Lg => Typography::TEXT_SM,
AvatarSize::Xl => Typography::TEXT_XL,
};
let (dot_sz, dot_inset) = match self.size {
AvatarSize::Sm => (8.0, 0.0),
AvatarSize::Md => (10.0, 0.0),
AvatarSize::Lg => (12.0, 0.0),
AvatarSize::Xl => (14.0, 4.0),
};
let has_status = self.status.is_some();
let dot_scale = if reduced {
if has_status { 1.0 } else { 0.6 }
} else {
arc_spring(
(self.id.clone(), "status-scale"),
if has_status { 1.0 } else { 0.6 },
motion.spring.snappy,
window,
cx,
)
};
let dot_op = arc_transition(
(self.id.clone(), "status-op"),
if has_status { 1.0 } else { 0.0 },
if reduced {
Duration::ZERO
} else {
motion.duration.fast
},
motion.ease.standard,
window,
cx,
);
let initials = initials_for(&self.name);
let status_bg = match self.status {
Some(AvatarStatus::Online) => colors.success,
Some(AvatarStatus::Offline) | None => colors.text_muted,
};
div()
.id(self.id)
.relative()
.flex_none()
.w(px(dim))
.h(px(dim))
.rounded(Radii::PILL)
.when(self.ring_border, |this| {
this.border_2().border_color(colors.surface)
})
.when(!self.ring_border, |this| {
this.border_1().border_color(colors.border)
})
.bg(colors.surface_muted)
.flex()
.items_center()
.justify_center()
.text_size(font_sz)
.font_medium()
.text_color(colors.foreground)
.child(if let Some(src) = self.src {
div()
.size_full()
.rounded(Radii::PILL)
.overflow_hidden()
.child(
img(src)
.size_full()
.rounded(Radii::PILL)
.object_fit(ObjectFit::Cover),
)
} else {
div().child(initials)
})
.when(has_status || dot_op > 0.02, |this| {
let sz = (dot_sz * dot_scale).max(4.0);
this.child(
div()
.absolute()
.right(px(dot_inset))
.bottom(px(dot_inset))
.w(px(sz))
.h(px(sz))
.rounded(Radii::PILL)
.border_2()
.border_color(colors.surface)
.bg(status_bg)
.opacity(dot_op.clamp(0.0, 1.0)),
)
})
}
}
#[cfg(test)]
mod tests {
use arc_tokens::{Accent, Spacing, ThemeMode, install_arc_theme};
use gpui_kit::{Context, Render, TestAppContext, div, px, test::TestWindowExt as _};
use super::*;
struct AvatarHost {
status: Option<AvatarStatus>,
}
impl Render for AvatarHost {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.w(px(320.0))
.p(Spacing::S4)
.flex()
.gap(Spacing::S3)
.child(
Avatar::new("av-1", "Elia Kuratli")
.size(AvatarSize::Lg)
.status_opt(self.status),
)
}
}
#[test]
fn initials_extracts_up_to_two_uppercase_chars() {
assert_eq!(initials_for("Elia Kuratli"), "EK");
assert_eq!(initials_for("Ada"), "A");
assert_eq!(initials_for("Grace Brewster Hopper"), "GB");
assert_eq!(initials_for(" "), "");
}
#[gpui_kit::test]
fn avatar_renders_and_toggles_status_dot(cx: &mut TestAppContext) {
cx.update(|cx| {
gpui_kit::init(cx);
install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
});
let (view, cx) = cx.add_window_view(|_, _| AvatarHost {
status: Some(AvatarStatus::Online),
});
cx.update(|window, cx| {
window.render_frame(cx);
view.update(cx, |v, cx| {
v.status = Some(AvatarStatus::Offline);
cx.notify();
});
window.render_frame(cx);
view.update(cx, |v, cx| {
v.status = None;
cx.notify();
});
window.render_frame(cx);
});
}
}