BlueGit

ict rusty-teams

Files

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

avatar.rs

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