BlueGit

ict rusty-teams

Files

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

avatar.rs

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