BlueGit

ict rusty-teams

Files

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

avatar.rs

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