BlueGit

ict rusty-teams

Files

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

avatar.rs

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