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