BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / avatar.rs

avatar.rs

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