use crate::views::image_memory::bounded; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::*; use std::sync::Arc; use std::time::Duration; use gpui_kit::assets::IconName; use crate::avatar_image::{AvatarPhoto, TEXTURE_MARGIN, device_span}; use crate::card_state::BotIdentity; use crate::data::{AvatarState, Directory, Presence, PresenceKind}; use crate::format; use crate::sidebar_model::{AvatarSpec, Face}; use crate::theme; use crate::views::profile_card::opens_profile; use crate::views::widgets::{Corner, corner_badge, icon, symbol}; const PAIR_RATIO: f32 = 24. / 36.; const PRESENCE_MIN: f32 = 10.; const PRESENCE_MAX: f32 = 15.; const PRESENCE_RATIO: f32 = 0.38; const PRESENCE_RING: f32 = 2.; const STATUS_GLYPH_DISC: f32 = 0.75; const PRESENCE_SPIN_PERIOD: Duration = Duration::from_millis(1600); const CACHED_PRESENCE_OPACITY: f32 = 0.5; const INITIALS_RATIO: f32 = 0.36; const PAIR_INITIALS_RATIO: f32 = 0.42; const MEETING_ICON_RATIO: f32 = 0.5; const BOT_CORNER_RATIO: f32 = 0.25; fn initials_circle(name: &str, key: &str, size: f32, pending: bool, text_ratio: f32) -> Div { let (background, foreground) = if pending { (theme::surface_raised(), theme::text_muted()) } else { ( theme::avatar_color(format::palette_index(key)), theme::text(), ) }; div() .size(px(size)) .flex_none() .flex() .items_center() .justify_center() .rounded_full() .bg(background) .text_color(foreground) .text_size(px((size * text_ratio).round())) .font_weight(FontWeight::SEMIBOLD) .child(format::initials(name)) } fn face_circle(directory: &Directory, face: &Face, size: f32, text_ratio: f32) -> AnyElement { let key = face.user_id.as_deref().unwrap_or(&face.name); let state = face .user_id .as_deref() .and_then(|user_id| directory.avatar(user_id)); match state { Some(AvatarState::Ready(photo)) => photo_disc(photo, size, size / 2.), Some(AvatarState::Icon(image)) => img(ImageSource::Image(image.clone())) .size(px(size)) .flex_none() .rounded_full() .object_fit(ObjectFit::Cover) .into_any_element(), Some(AvatarState::Pending) => { initials_circle(&face.name, key, size, true, text_ratio).into_any_element() } _ => initials_circle(&face.name, key, size, false, text_ratio).into_any_element(), } } fn photo_disc(photo: &Arc, size: f32, radius: f32) -> AnyElement { let photo = photo.clone(); canvas( |_, _, _| {}, move |bounds, _, window, cx| { let scale_factor = window.scale_factor(); let side = device_span( f32::from(bounds.left()), f32::from(bounds.right()), scale_factor, ); let Some(texture) = photo.texture(side) else { return; }; if let Some(evicted) = texture.evicted { cx.drop_image(evicted, Some(window)); } let margin = px(TEXTURE_MARGIN as f32 / scale_factor); let _ = window.paint_image( bounds, bounds.dilate(margin), Corners::all(px(radius)), texture.image, 0, false, ); }, ) .size(px(size)) .flex_none() .into_any_element() } pub fn person_avatar( directory: &Directory, user_id: Option<&str>, name: &str, size: f32, ) -> AnyElement { let face = Face { user_id: user_id.map(str::to_owned), name: name.to_owned(), }; face_circle(directory, &face, size, INITIALS_RATIO) } pub fn bot_avatar(identity: &BotIdentity, size: f32) -> AnyElement { let initials = { let name = identity.name.clone(); move || initials_circle(&name, &name, size, false, INITIALS_RATIO).into_any_element() }; match &identity.icon_url { Some(url) => div() .size(px(size)) .flex_none() .rounded_full() .overflow_hidden() .child( img(bounded(url.clone())) .size_full() .rounded_full() .object_fit(ObjectFit::Cover) .with_loading(initials.clone()) .with_fallback(initials), ) .into_any_element(), None => initials(), } } pub fn bot_tile(directory: &Directory, face: &Face, size: f32) -> AnyElement { let radius = (size * BOT_CORNER_RATIO).round(); let image = face .user_id .as_deref() .and_then(|user_id| directory.avatar(user_id)); let tile = div().size(px(size)).flex_none().rounded(px(radius)); match image { Some(AvatarState::Ready(photo)) => tile .child(photo_disc(photo, size, radius)) .into_any_element(), Some(AvatarState::Icon(image)) => tile .child( img(ImageSource::Image(image.clone())) .size_full() .rounded(px(radius)) .object_fit(ObjectFit::Cover), ) .into_any_element(), _ => tile .bg(theme::bot_avatar()) .child(symbol("bot", size, theme::white())) .into_any_element(), } } pub fn presence_size(avatar_size: f32) -> f32 { (avatar_size * PRESENCE_RATIO) .round() .clamp(PRESENCE_MIN, PRESENCE_MAX) } pub fn status_glyph(kind: PresenceKind) -> Option<(&'static str, Hsla)> { Some(match kind { PresenceKind::Available => ("status_available", theme::green()), PresenceKind::Busy => ("status_dot", theme::red()), PresenceKind::DoNotDisturb => ("status_dnd", theme::red()), PresenceKind::Away => ("status_away", theme::amber()), PresenceKind::OutOfOffice => ("status_dot", theme::out_of_office()), PresenceKind::Offline => ("status_offline", theme::text_muted()), PresenceKind::Unknown => return None, }) } fn presence_badge(kind: PresenceKind, ring: Hsla, outer: f32) -> Option
{ let inner = outer - 2. * PRESENCE_RING; let glyph = match kind { PresenceKind::OutOfOffice => div() .size(px(inner)) .rounded_full() .bg(theme::out_of_office()) .flex() .items_center() .justify_center() .child(icon(IconName::ArrowLeft, inner - 2., theme::white())) .into_any_element(), _ => { let (name, color) = status_glyph(kind)?; symbol(name, inner / STATUS_GLYPH_DISC, color).into_any_element() } }; Some( corner_badge(Corner::BottomRight, 2.) .size(px(outer)) .rounded_full() .bg(ring) .flex() .items_center() .justify_center() .child(glyph), ) } fn presence_spinner(ring: Hsla, outer: f32) -> Div { let spinner = icon(IconName::Loader, outer - PRESENCE_RING, theme::text_muted()) .with_animation( "presence-spinner", Animation::new(PRESENCE_SPIN_PERIOD).repeat(), |icon, delta| icon.transform(Transformation::rotate(percentage(delta))), ); corner_badge(Corner::BottomRight, 2.) .size(px(outer)) .rounded_full() .bg(ring) .flex() .items_center() .justify_center() .child(spinner) } pub fn with_presence(avatar: AnyElement, presence: Presence, size: f32, ring: Hsla) -> Div { with_presence_badge(avatar, presence, size, presence_size(size), ring) } pub fn with_presence_badge( avatar: AnyElement, presence: Presence, size: f32, badge: f32, ring: Hsla, ) -> Div { let wrapper = div().relative().size(px(size)).flex_none().child(avatar); match presence { Presence::Loading => wrapper.child(presence_spinner(ring, badge)), Presence::Live(kind) => wrapper.children(presence_badge(kind, ring, badge)), Presence::Cached(kind) => wrapper.children( presence_badge(kind, ring, badge).map(|dot| dot.opacity(CACHED_PRESENCE_OPACITY)), ), } } pub fn spec_avatar(directory: &Directory, spec: &AvatarSpec, size: f32, ring: Hsla) -> AnyElement { match spec { AvatarSpec::Single(face) => face_circle(directory, face, size, INITIALS_RATIO), AvatarSpec::Bot(face) => bot_tile(directory, face, size), AvatarSpec::Meeting => div() .size(px(size)) .flex_none() .rounded_full() .bg(theme::meeting_avatar()) .flex() .items_center() .justify_center() .child(icon( IconName::Calendar, (size * MEETING_ICON_RATIO).round(), theme::white(), )) .into_any_element(), AvatarSpec::Muted => div() .size(px(size)) .flex_none() .rounded_full() .bg(theme::popup_hover()) .flex() .items_center() .justify_center() .child(icon( IconName::BellOff, (size * MEETING_ICON_RATIO).round(), theme::text_muted(), )) .into_any_element(), AvatarSpec::Pair(first, second) => { let small = (size * PAIR_RATIO).round(); div() .relative() .size(px(size)) .flex_none() .child(div().absolute().left_0().top_0().child(face_circle( directory, first, small, PAIR_INITIALS_RATIO, ))) .child( div() .absolute() .right_0() .bottom_0() .size(px(small)) .flex() .items_center() .justify_center() .rounded_full() .bg(ring) .child(face_circle( directory, second, small - 2. * PRESENCE_RING, PAIR_INITIALS_RATIO, )), ) .into_any_element() } } } pub fn square_avatar(name: &str, key: &str, size: f32, radius: f32) -> Div { div() .size(px(size)) .flex_none() .flex() .items_center() .justify_center() .rounded(px(radius)) .bg(theme::avatar_color(format::palette_index(key))) .text_color(theme::text()) .text_size(px((size * 0.4).round())) .font_weight(FontWeight::SEMIBOLD) .child(format::team_initials(name)) } pub fn team_avatar( photo: Option<&AvatarState>, name: &str, key: &str, size: f32, radius: f32, ) -> AnyElement { match photo { Some(AvatarState::Ready(photo)) => div() .size(px(size)) .flex_none() .rounded(px(radius)) .child(photo_disc(photo, size, radius)) .into_any_element(), _ => square_avatar(name, key, size, radius).into_any_element(), } } pub fn member_stack( directory: &Directory, faces: &[Face], total: usize, opens_profiles: bool, ) -> Div { const LIMIT: usize = 3; const SIZE: f32 = 22.; const OVERLAP: f32 = 6.; let visible = faces.len().min(LIMIT) + usize::from(total > LIMIT); let width = SIZE * visible as f32 - OVERLAP * visible.saturating_sub(1) as f32; let mut stack = div().flex().flex_none().w(px(width)).items_center(); for (index, face) in faces.iter().take(LIMIT).enumerate() { stack = stack.child( opens_profile( div().id(ElementId::Name(format!("member-face-{index}").into())), face.user_id.as_deref().filter(|_| opens_profiles), ) .when(index > 0, |element| element.ml(px(-OVERLAP))) .size(px(SIZE)) .flex_none() .flex() .items_center() .justify_center() .rounded_full() .bg(theme::surface()) .child(face_circle( directory, face, SIZE - 2. * PRESENCE_RING, 0.45, )), ); } if total > LIMIT { stack = stack.child( div() .ml(px(-OVERLAP)) .size(px(SIZE)) .flex_none() .flex() .items_center() .justify_center() .rounded_full() .border(px(PRESENCE_RING)) .border_color(theme::surface()) .bg(theme::surface_raised()) .text_color(theme::text_soft()) .text_size(px(10.)) .font_weight(FontWeight::SEMIBOLD) .child(format!("+{}", total - LIMIT)), ); } stack } #[cfg(test)] mod tests { use super::presence_size; #[test] fn presence_dot_scales_with_the_avatar() { assert_eq!(presence_size(26.), 10.); assert_eq!(presence_size(36.), 14.); assert_eq!(presence_size(20.), 10.); } }