BlueGit

ict rusty-teams

Files

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

avatar.rs

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