BlueGit

ict rusty-teams

Files

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

avatar.rs

avatar.rs 12.7 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, 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 _ = window.paint_image(
93 bounds,
94 bounds,
95 Corners::all(px(radius)),
96 texture.image,
97 0,
98 false,
99 );
100 },
101 )
102 .size(px(size))
103 .flex_none()
104 .into_any_element()
105}
106
107pub fn person_avatar(
108 directory: &Directory,
109 user_id: Option<&str>,
110 name: &str,
111 size: f32,
112) -> AnyElement {
113 let face = Face {
114 user_id: user_id.map(str::to_owned),
115 name: name.to_owned(),
116 };
117 face_circle(directory, &face, size, INITIALS_RATIO)
118}
119
120pub fn bot_avatar(identity: &BotIdentity, size: f32) -> AnyElement {
121 let initials = {
122 let name = identity.name.clone();
123 move || initials_circle(&name, &name, size, false, INITIALS_RATIO).into_any_element()
124 };
125 match &identity.icon_url {
126 Some(url) => div()
127 .size(px(size))
128 .flex_none()
129 .rounded_full()
130 .overflow_hidden()
131 .child(
132 img(url.clone())
133 .size_full()
134 .rounded_full()
135 .object_fit(ObjectFit::Cover)
136 .with_loading(initials.clone())
137 .with_fallback(initials),
138 )
139 .into_any_element(),
140 None => initials(),
141 }
142}
143
144pub fn bot_tile(directory: &Directory, face: &Face, size: f32) -> AnyElement {
145 let radius = (size * BOT_CORNER_RATIO).round();
146 let image = face
147 .user_id
148 .as_deref()
149 .and_then(|user_id| directory.avatar(user_id));
150 let tile = div().size(px(size)).flex_none().rounded(px(radius));
151 match image {
152 Some(AvatarState::Ready(photo)) => tile
153 .child(photo_disc(photo, size, radius))
154 .into_any_element(),
155 Some(AvatarState::Icon(image)) => tile
156 .child(
157 img(ImageSource::Image(image.clone()))
158 .size_full()
159 .rounded(px(radius))
160 .object_fit(ObjectFit::Cover),
161 )
162 .into_any_element(),
163 _ => tile
164 .bg(theme::bot_avatar())
165 .child(symbol("bot", size, theme::white()))
166 .into_any_element(),
167 }
168}
169
170pub fn presence_size(avatar_size: f32) -> f32 {
171 (avatar_size * PRESENCE_RATIO)
172 .round()
173 .clamp(PRESENCE_MIN, PRESENCE_MAX)
174}
175
176pub fn status_glyph(kind: PresenceKind) -> Option<(&'static str, Hsla)> {
177 Some(match kind {
178 PresenceKind::Available => ("status_available", theme::green()),
179 PresenceKind::Busy => ("status_dot", theme::red()),
180 PresenceKind::DoNotDisturb => ("status_dnd", theme::red()),
181 PresenceKind::Away => ("status_away", theme::amber()),
182 PresenceKind::OutOfOffice => ("status_dot", theme::out_of_office()),
183 PresenceKind::Offline => ("status_offline", theme::text_muted()),
184 PresenceKind::Unknown => return None,
185 })
186}
187
188fn presence_badge(kind: PresenceKind, ring: Hsla, outer: f32) -> Option<Div> {
189 let inner = outer - 2. * PRESENCE_RING;
190 let glyph = match kind {
191 PresenceKind::OutOfOffice => div()
192 .size(px(inner))
193 .rounded_full()
194 .bg(theme::out_of_office())
195 .flex()
196 .items_center()
197 .justify_center()
198 .child(icon(IconName::ArrowLeft, inner - 2., theme::white()))
199 .into_any_element(),
200 _ => {
201 let (name, color) = status_glyph(kind)?;
202 symbol(name, inner / STATUS_GLYPH_DISC, color).into_any_element()
203 }
204 };
205 Some(
206 corner_badge(Corner::BottomRight, 2.)
207 .size(px(outer))
208 .rounded_full()
209 .bg(ring)
210 .flex()
211 .items_center()
212 .justify_center()
213 .child(glyph),
214 )
215}
216
217fn presence_spinner(ring: Hsla, outer: f32) -> Div {
218 let spinner = icon(IconName::Loader, outer - PRESENCE_RING, theme::text_muted())
219 .with_animation(
220 "presence-spinner",
221 Animation::new(PRESENCE_SPIN_PERIOD).repeat(),
222 |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
223 );
224 corner_badge(Corner::BottomRight, 2.)
225 .size(px(outer))
226 .rounded_full()
227 .bg(ring)
228 .flex()
229 .items_center()
230 .justify_center()
231 .child(spinner)
232}
233
234pub fn with_presence(avatar: AnyElement, presence: Presence, size: f32, ring: Hsla) -> Div {
235 with_presence_badge(avatar, presence, size, presence_size(size), ring)
236}
237
238pub fn with_presence_badge(
239 avatar: AnyElement,
240 presence: Presence,
241 size: f32,
242 badge: f32,
243 ring: Hsla,
244) -> Div {
245 let wrapper = div().relative().size(px(size)).flex_none().child(avatar);
246 match presence {
247 Presence::Loading => wrapper.child(presence_spinner(ring, badge)),
248 Presence::Live(kind) => wrapper.children(presence_badge(kind, ring, badge)),
249 Presence::Cached(kind) => wrapper.children(
250 presence_badge(kind, ring, badge).map(|dot| dot.opacity(CACHED_PRESENCE_OPACITY)),
251 ),
252 }
253}
254
255pub fn spec_avatar(directory: &Directory, spec: &AvatarSpec, size: f32, ring: Hsla) -> AnyElement {
256 match spec {
257 AvatarSpec::Single(face) => face_circle(directory, face, size, INITIALS_RATIO),
258 AvatarSpec::Bot(face) => bot_tile(directory, face, size),
259 AvatarSpec::Meeting => div()
260 .size(px(size))
261 .flex_none()
262 .rounded_full()
263 .bg(theme::meeting_avatar())
264 .flex()
265 .items_center()
266 .justify_center()
267 .child(icon(
268 IconName::Calendar,
269 (size * MEETING_ICON_RATIO).round(),
270 theme::white(),
271 ))
272 .into_any_element(),
273 AvatarSpec::Muted => div()
274 .size(px(size))
275 .flex_none()
276 .rounded_full()
277 .bg(theme::popup_hover())
278 .flex()
279 .items_center()
280 .justify_center()
281 .child(icon(
282 IconName::BellOff,
283 (size * MEETING_ICON_RATIO).round(),
284 theme::text_muted(),
285 ))
286 .into_any_element(),
287 AvatarSpec::Pair(first, second) => {
288 let small = (size * PAIR_RATIO).round();
289 div()
290 .relative()
291 .size(px(size))
292 .flex_none()
293 .child(div().absolute().left_0().top_0().child(face_circle(
294 directory,
295 first,
296 small,
297 PAIR_INITIALS_RATIO,
298 )))
299 .child(
300 div()
301 .absolute()
302 .right_0()
303 .bottom_0()
304 .size(px(small))
305 .flex()
306 .items_center()
307 .justify_center()
308 .rounded_full()
309 .bg(ring)
310 .child(face_circle(
311 directory,
312 second,
313 small - 2. * PRESENCE_RING,
314 PAIR_INITIALS_RATIO,
315 )),
316 )
317 .into_any_element()
318 }
319 }
320}
321
322pub fn square_avatar(name: &str, key: &str, size: f32, radius: f32) -> Div {
323 div()
324 .size(px(size))
325 .flex_none()
326 .flex()
327 .items_center()
328 .justify_center()
329 .rounded(px(radius))
330 .bg(theme::avatar_color(format::palette_index(key)))
331 .text_color(theme::text())
332 .text_size(px((size * 0.4).round()))
333 .font_weight(FontWeight::SEMIBOLD)
334 .child(format::initials(name))
335}
336
337pub fn member_stack(
338 directory: &Directory,
339 faces: &[Face],
340 total: usize,
341 opens_profiles: bool,
342) -> Div {
343 const LIMIT: usize = 3;
344 const SIZE: f32 = 22.;
345 const OVERLAP: f32 = 6.;
346 let visible = faces.len().min(LIMIT) + usize::from(total > LIMIT);
347 let width = SIZE * visible as f32 - OVERLAP * visible.saturating_sub(1) as f32;
348 let mut stack = div().flex().flex_none().w(px(width)).items_center();
349 for (index, face) in faces.iter().take(LIMIT).enumerate() {
350 stack = stack.child(
351 opens_profile(
352 div().id(ElementId::Name(format!("member-face-{index}").into())),
353 face.user_id.as_deref().filter(|_| opens_profiles),
354 )
355 .when(index > 0, |element| element.ml(px(-OVERLAP)))
356 .size(px(SIZE))
357 .flex_none()
358 .flex()
359 .items_center()
360 .justify_center()
361 .rounded_full()
362 .bg(theme::surface())
363 .child(face_circle(
364 directory,
365 face,
366 SIZE - 2. * PRESENCE_RING,
367 0.45,
368 )),
369 );
370 }
371 if total > LIMIT {
372 stack = stack.child(
373 div()
374 .ml(px(-OVERLAP))
375 .size(px(SIZE))
376 .flex_none()
377 .flex()
378 .items_center()
379 .justify_center()
380 .rounded_full()
381 .border(px(PRESENCE_RING))
382 .border_color(theme::surface())
383 .bg(theme::surface_raised())
384 .text_color(theme::text_soft())
385 .text_size(px(10.))
386 .font_weight(FontWeight::SEMIBOLD)
387 .child(format!("+{}", total - LIMIT)),
388 );
389 }
390 stack
391}
392
393#[cfg(test)]
394mod tests {
395 use super::presence_size;
396
397 #[test]
398 fn presence_dot_scales_with_the_avatar() {
399 assert_eq!(presence_size(26.), 10.);
400 assert_eq!(presence_size(36.), 14.);
401 assert_eq!(presence_size(20.), 10.);
402 }
403}