| 1 | use crate::views::image_memory::bounded; |
| 2 | use gpui_kit::prelude::FluentBuilder as _; |
| 3 | use gpui_kit::*; |
| 4 | |
| 5 | use std::sync::Arc; |
| 6 | use std::time::Duration; |
| 7 | |
| 8 | use gpui_kit::assets::IconName; |
| 9 | |
| 10 | use crate::avatar_image::{AvatarPhoto, TEXTURE_MARGIN, device_span}; |
| 11 | use crate::card_state::BotIdentity; |
| 12 | use crate::data::{AvatarState, Directory, Presence, PresenceKind}; |
| 13 | use crate::format; |
| 14 | use crate::sidebar_model::{AvatarSpec, Face}; |
| 15 | use crate::theme; |
| 16 | use crate::views::profile_card::opens_profile; |
| 17 | use crate::views::widgets::{Corner, corner_badge, icon, symbol}; |
| 18 | |
| 19 | const PAIR_RATIO: f32 = 24. / 36.; |
| 20 | const PRESENCE_MIN: f32 = 10.; |
| 21 | const PRESENCE_MAX: f32 = 15.; |
| 22 | const PRESENCE_RATIO: f32 = 0.38; |
| 23 | const PRESENCE_RING: f32 = 2.; |
| 24 | const STATUS_GLYPH_DISC: f32 = 0.75; |
| 25 | const PRESENCE_SPIN_PERIOD: Duration = Duration::from_millis(1600); |
| 26 | const CACHED_PRESENCE_OPACITY: f32 = 0.5; |
| 27 | const INITIALS_RATIO: f32 = 0.36; |
| 28 | const PAIR_INITIALS_RATIO: f32 = 0.42; |
| 29 | const MEETING_ICON_RATIO: f32 = 0.5; |
| 30 | const BOT_CORNER_RATIO: f32 = 0.25; |
| 31 | |
| 32 | fn initials_circle(name: &str, key: &str, size: f32, pending: bool, text_ratio: f32) -> Div { |
| 33 | let (background, foreground) = if pending { |
| 34 | (theme::surface_raised(), theme::text_muted()) |
| 35 | } else { |
| 36 | ( |
| 37 | theme::avatar_color(format::palette_index(key)), |
| 38 | theme::text(), |
| 39 | ) |
| 40 | }; |
| 41 | div() |
| 42 | .size(px(size)) |
| 43 | .flex_none() |
| 44 | .flex() |
| 45 | .items_center() |
| 46 | .justify_center() |
| 47 | .rounded_full() |
| 48 | .bg(background) |
| 49 | .text_color(foreground) |
| 50 | .text_size(px((size * text_ratio).round())) |
| 51 | .font_weight(FontWeight::SEMIBOLD) |
| 52 | .child(format::initials(name)) |
| 53 | } |
| 54 | |
| 55 | fn face_circle(directory: &Directory, face: &Face, size: f32, text_ratio: f32) -> AnyElement { |
| 56 | let key = face.user_id.as_deref().unwrap_or(&face.name); |
| 57 | let state = face |
| 58 | .user_id |
| 59 | .as_deref() |
| 60 | .and_then(|user_id| directory.avatar(user_id)); |
| 61 | match state { |
| 62 | Some(AvatarState::Ready(photo)) => photo_disc(photo, size, size / 2.), |
| 63 | Some(AvatarState::Icon(image)) => img(ImageSource::Image(image.clone())) |
| 64 | .size(px(size)) |
| 65 | .flex_none() |
| 66 | .rounded_full() |
| 67 | .object_fit(ObjectFit::Cover) |
| 68 | .into_any_element(), |
| 69 | Some(AvatarState::Pending) => { |
| 70 | initials_circle(&face.name, key, size, true, text_ratio).into_any_element() |
| 71 | } |
| 72 | _ => initials_circle(&face.name, key, size, false, text_ratio).into_any_element(), |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | fn photo_disc(photo: &Arc<AvatarPhoto>, size: f32, radius: f32) -> AnyElement { |
| 77 | let photo = photo.clone(); |
| 78 | canvas( |
| 79 | |_, _, _| {}, |
| 80 | move |bounds, _, window, cx| { |
| 81 | let scale_factor = window.scale_factor(); |
| 82 | let side = device_span( |
| 83 | f32::from(bounds.left()), |
| 84 | f32::from(bounds.right()), |
| 85 | scale_factor, |
| 86 | ); |
| 87 | let Some(texture) = photo.texture(side) else { |
| 88 | return; |
| 89 | }; |
| 90 | if let Some(evicted) = texture.evicted { |
| 91 | cx.drop_image(evicted, Some(window)); |
| 92 | } |
| 93 | let margin = px(TEXTURE_MARGIN as f32 / scale_factor); |
| 94 | let _ = window.paint_image( |
| 95 | bounds, |
| 96 | bounds.dilate(margin), |
| 97 | Corners::all(px(radius)), |
| 98 | texture.image, |
| 99 | 0, |
| 100 | false, |
| 101 | ); |
| 102 | }, |
| 103 | ) |
| 104 | .size(px(size)) |
| 105 | .flex_none() |
| 106 | .into_any_element() |
| 107 | } |
| 108 | |
| 109 | pub fn person_avatar( |
| 110 | directory: &Directory, |
| 111 | user_id: Option<&str>, |
| 112 | name: &str, |
| 113 | size: f32, |
| 114 | ) -> AnyElement { |
| 115 | let face = Face { |
| 116 | user_id: user_id.map(str::to_owned), |
| 117 | name: name.to_owned(), |
| 118 | }; |
| 119 | face_circle(directory, &face, size, INITIALS_RATIO) |
| 120 | } |
| 121 | |
| 122 | pub fn bot_avatar(identity: &BotIdentity, size: f32) -> AnyElement { |
| 123 | let initials = { |
| 124 | let name = identity.name.clone(); |
| 125 | move || initials_circle(&name, &name, size, false, INITIALS_RATIO).into_any_element() |
| 126 | }; |
| 127 | match &identity.icon_url { |
| 128 | Some(url) => div() |
| 129 | .size(px(size)) |
| 130 | .flex_none() |
| 131 | .rounded_full() |
| 132 | .overflow_hidden() |
| 133 | .child( |
| 134 | img(bounded(url.clone())) |
| 135 | .size_full() |
| 136 | .rounded_full() |
| 137 | .object_fit(ObjectFit::Cover) |
| 138 | .with_loading(initials.clone()) |
| 139 | .with_fallback(initials), |
| 140 | ) |
| 141 | .into_any_element(), |
| 142 | None => initials(), |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | pub fn bot_tile(directory: &Directory, face: &Face, size: f32) -> AnyElement { |
| 147 | let radius = (size * BOT_CORNER_RATIO).round(); |
| 148 | let image = face |
| 149 | .user_id |
| 150 | .as_deref() |
| 151 | .and_then(|user_id| directory.avatar(user_id)); |
| 152 | let tile = div().size(px(size)).flex_none().rounded(px(radius)); |
| 153 | match image { |
| 154 | Some(AvatarState::Ready(photo)) => tile |
| 155 | .child(photo_disc(photo, size, radius)) |
| 156 | .into_any_element(), |
| 157 | Some(AvatarState::Icon(image)) => tile |
| 158 | .child( |
| 159 | img(ImageSource::Image(image.clone())) |
| 160 | .size_full() |
| 161 | .rounded(px(radius)) |
| 162 | .object_fit(ObjectFit::Cover), |
| 163 | ) |
| 164 | .into_any_element(), |
| 165 | _ => tile |
| 166 | .bg(theme::bot_avatar()) |
| 167 | .child(symbol("bot", size, theme::white())) |
| 168 | .into_any_element(), |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | pub fn presence_size(avatar_size: f32) -> f32 { |
| 173 | (avatar_size * PRESENCE_RATIO) |
| 174 | .round() |
| 175 | .clamp(PRESENCE_MIN, PRESENCE_MAX) |
| 176 | } |
| 177 | |
| 178 | pub fn status_glyph(kind: PresenceKind) -> Option<(&'static str, Hsla)> { |
| 179 | Some(match kind { |
| 180 | PresenceKind::Available => ("status_available", theme::green()), |
| 181 | PresenceKind::Busy => ("status_dot", theme::red()), |
| 182 | PresenceKind::DoNotDisturb => ("status_dnd", theme::red()), |
| 183 | PresenceKind::Away => ("status_away", theme::amber()), |
| 184 | PresenceKind::OutOfOffice => ("status_dot", theme::out_of_office()), |
| 185 | PresenceKind::Offline => ("status_offline", theme::text_muted()), |
| 186 | PresenceKind::Unknown => return None, |
| 187 | }) |
| 188 | } |
| 189 | |
| 190 | fn presence_badge(kind: PresenceKind, ring: Hsla, outer: f32) -> Option<Div> { |
| 191 | let inner = outer - 2. * PRESENCE_RING; |
| 192 | let glyph = match kind { |
| 193 | PresenceKind::OutOfOffice => div() |
| 194 | .size(px(inner)) |
| 195 | .rounded_full() |
| 196 | .bg(theme::out_of_office()) |
| 197 | .flex() |
| 198 | .items_center() |
| 199 | .justify_center() |
| 200 | .child(icon(IconName::ArrowLeft, inner - 2., theme::white())) |
| 201 | .into_any_element(), |
| 202 | _ => { |
| 203 | let (name, color) = status_glyph(kind)?; |
| 204 | symbol(name, inner / STATUS_GLYPH_DISC, color).into_any_element() |
| 205 | } |
| 206 | }; |
| 207 | Some( |
| 208 | corner_badge(Corner::BottomRight, 2.) |
| 209 | .size(px(outer)) |
| 210 | .rounded_full() |
| 211 | .bg(ring) |
| 212 | .flex() |
| 213 | .items_center() |
| 214 | .justify_center() |
| 215 | .child(glyph), |
| 216 | ) |
| 217 | } |
| 218 | |
| 219 | fn presence_spinner(ring: Hsla, outer: f32) -> Div { |
| 220 | let spinner = icon(IconName::Loader, outer - PRESENCE_RING, theme::text_muted()) |
| 221 | .with_animation( |
| 222 | "presence-spinner", |
| 223 | Animation::new(PRESENCE_SPIN_PERIOD).repeat(), |
| 224 | |icon, delta| icon.transform(Transformation::rotate(percentage(delta))), |
| 225 | ); |
| 226 | corner_badge(Corner::BottomRight, 2.) |
| 227 | .size(px(outer)) |
| 228 | .rounded_full() |
| 229 | .bg(ring) |
| 230 | .flex() |
| 231 | .items_center() |
| 232 | .justify_center() |
| 233 | .child(spinner) |
| 234 | } |
| 235 | |
| 236 | pub fn with_presence(avatar: AnyElement, presence: Presence, size: f32, ring: Hsla) -> Div { |
| 237 | with_presence_badge(avatar, presence, size, presence_size(size), ring) |
| 238 | } |
| 239 | |
| 240 | pub fn with_presence_badge( |
| 241 | avatar: AnyElement, |
| 242 | presence: Presence, |
| 243 | size: f32, |
| 244 | badge: f32, |
| 245 | ring: Hsla, |
| 246 | ) -> Div { |
| 247 | let wrapper = div().relative().size(px(size)).flex_none().child(avatar); |
| 248 | match presence { |
| 249 | Presence::Loading => wrapper.child(presence_spinner(ring, badge)), |
| 250 | Presence::Live(kind) => wrapper.children(presence_badge(kind, ring, badge)), |
| 251 | Presence::Cached(kind) => wrapper.children( |
| 252 | presence_badge(kind, ring, badge).map(|dot| dot.opacity(CACHED_PRESENCE_OPACITY)), |
| 253 | ), |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | pub fn spec_avatar(directory: &Directory, spec: &AvatarSpec, size: f32, ring: Hsla) -> AnyElement { |
| 258 | match spec { |
| 259 | AvatarSpec::Single(face) => face_circle(directory, face, size, INITIALS_RATIO), |
| 260 | AvatarSpec::Bot(face) => bot_tile(directory, face, size), |
| 261 | AvatarSpec::Meeting => div() |
| 262 | .size(px(size)) |
| 263 | .flex_none() |
| 264 | .rounded_full() |
| 265 | .bg(theme::meeting_avatar()) |
| 266 | .flex() |
| 267 | .items_center() |
| 268 | .justify_center() |
| 269 | .child(icon( |
| 270 | IconName::Calendar, |
| 271 | (size * MEETING_ICON_RATIO).round(), |
| 272 | theme::white(), |
| 273 | )) |
| 274 | .into_any_element(), |
| 275 | AvatarSpec::Muted => div() |
| 276 | .size(px(size)) |
| 277 | .flex_none() |
| 278 | .rounded_full() |
| 279 | .bg(theme::popup_hover()) |
| 280 | .flex() |
| 281 | .items_center() |
| 282 | .justify_center() |
| 283 | .child(icon( |
| 284 | IconName::BellOff, |
| 285 | (size * MEETING_ICON_RATIO).round(), |
| 286 | theme::text_muted(), |
| 287 | )) |
| 288 | .into_any_element(), |
| 289 | AvatarSpec::Pair(first, second) => { |
| 290 | let small = (size * PAIR_RATIO).round(); |
| 291 | div() |
| 292 | .relative() |
| 293 | .size(px(size)) |
| 294 | .flex_none() |
| 295 | .child(div().absolute().left_0().top_0().child(face_circle( |
| 296 | directory, |
| 297 | first, |
| 298 | small, |
| 299 | PAIR_INITIALS_RATIO, |
| 300 | ))) |
| 301 | .child( |
| 302 | div() |
| 303 | .absolute() |
| 304 | .right_0() |
| 305 | .bottom_0() |
| 306 | .size(px(small)) |
| 307 | .flex() |
| 308 | .items_center() |
| 309 | .justify_center() |
| 310 | .rounded_full() |
| 311 | .bg(ring) |
| 312 | .child(face_circle( |
| 313 | directory, |
| 314 | second, |
| 315 | small - 2. * PRESENCE_RING, |
| 316 | PAIR_INITIALS_RATIO, |
| 317 | )), |
| 318 | ) |
| 319 | .into_any_element() |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | pub fn square_avatar(name: &str, key: &str, size: f32, radius: f32) -> Div { |
| 325 | div() |
| 326 | .size(px(size)) |
| 327 | .flex_none() |
| 328 | .flex() |
| 329 | .items_center() |
| 330 | .justify_center() |
| 331 | .rounded(px(radius)) |
| 332 | .bg(theme::avatar_color(format::palette_index(key))) |
| 333 | .text_color(theme::text()) |
| 334 | .text_size(px((size * 0.4).round())) |
| 335 | .font_weight(FontWeight::SEMIBOLD) |
| 336 | .child(format::team_initials(name)) |
| 337 | } |
| 338 | |
| 339 | pub fn team_avatar( |
| 340 | photo: Option<&AvatarState>, |
| 341 | name: &str, |
| 342 | key: &str, |
| 343 | size: f32, |
| 344 | radius: f32, |
| 345 | ) -> AnyElement { |
| 346 | match photo { |
| 347 | Some(AvatarState::Ready(photo)) => div() |
| 348 | .size(px(size)) |
| 349 | .flex_none() |
| 350 | .rounded(px(radius)) |
| 351 | .child(photo_disc(photo, size, radius)) |
| 352 | .into_any_element(), |
| 353 | _ => square_avatar(name, key, size, radius).into_any_element(), |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | pub fn member_stack( |
| 358 | directory: &Directory, |
| 359 | faces: &[Face], |
| 360 | total: usize, |
| 361 | opens_profiles: bool, |
| 362 | ) -> Div { |
| 363 | const LIMIT: usize = 3; |
| 364 | const SIZE: f32 = 22.; |
| 365 | const OVERLAP: f32 = 6.; |
| 366 | let visible = faces.len().min(LIMIT) + usize::from(total > LIMIT); |
| 367 | let width = SIZE * visible as f32 - OVERLAP * visible.saturating_sub(1) as f32; |
| 368 | let mut stack = div().flex().flex_none().w(px(width)).items_center(); |
| 369 | for (index, face) in faces.iter().take(LIMIT).enumerate() { |
| 370 | stack = stack.child( |
| 371 | opens_profile( |
| 372 | div().id(ElementId::Name(format!("member-face-{index}").into())), |
| 373 | face.user_id.as_deref().filter(|_| opens_profiles), |
| 374 | ) |
| 375 | .when(index > 0, |element| element.ml(px(-OVERLAP))) |
| 376 | .size(px(SIZE)) |
| 377 | .flex_none() |
| 378 | .flex() |
| 379 | .items_center() |
| 380 | .justify_center() |
| 381 | .rounded_full() |
| 382 | .bg(theme::surface()) |
| 383 | .child(face_circle( |
| 384 | directory, |
| 385 | face, |
| 386 | SIZE - 2. * PRESENCE_RING, |
| 387 | 0.45, |
| 388 | )), |
| 389 | ); |
| 390 | } |
| 391 | if total > LIMIT { |
| 392 | stack = stack.child( |
| 393 | div() |
| 394 | .ml(px(-OVERLAP)) |
| 395 | .size(px(SIZE)) |
| 396 | .flex_none() |
| 397 | .flex() |
| 398 | .items_center() |
| 399 | .justify_center() |
| 400 | .rounded_full() |
| 401 | .border(px(PRESENCE_RING)) |
| 402 | .border_color(theme::surface()) |
| 403 | .bg(theme::surface_raised()) |
| 404 | .text_color(theme::text_soft()) |
| 405 | .text_size(px(10.)) |
| 406 | .font_weight(FontWeight::SEMIBOLD) |
| 407 | .child(format!("+{}", total - LIMIT)), |
| 408 | ); |
| 409 | } |
| 410 | stack |
| 411 | } |
| 412 | |
| 413 | #[cfg(test)] |
| 414 | mod tests { |
| 415 | use super::presence_size; |
| 416 | |
| 417 | #[test] |
| 418 | fn presence_dot_scales_with_the_avatar() { |
| 419 | assert_eq!(presence_size(26.), 10.); |
| 420 | assert_eq!(presence_size(36.), 14.); |
| 421 | assert_eq!(presence_size(20.), 10.); |
| 422 | } |
| 423 | } |