BlueGit

ict rusty-teams

Files

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

avatar.rs

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