| 1 | use gpui_kit::prelude::FluentBuilder as _; |
| 2 | use gpui_kit::*; |
| 3 | |
| 4 | use crate::data::{AvatarState, Directory, PresenceKind}; |
| 5 | use crate::format; |
| 6 | use crate::sidebar_model::{AvatarSpec, Face}; |
| 7 | use crate::theme; |
| 8 | |
| 9 | const PAIR_RATIO: f32 = 24. / 36.; |
| 10 | const PRESENCE_MIN: f32 = 10.; |
| 11 | const PRESENCE_MAX: f32 = 14.; |
| 12 | const PRESENCE_RATIO: f32 = 0.38; |
| 13 | const PRESENCE_RING: f32 = 2.; |
| 14 | const INITIALS_RATIO: f32 = 0.36; |
| 15 | const PAIR_INITIALS_RATIO: f32 = 0.42; |
| 16 | |
| 17 | fn 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 | |
| 40 | fn 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 | |
| 70 | pub 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 | |
| 83 | pub fn presence_size(avatar_size: f32) -> f32 { |
| 84 | (avatar_size * PRESENCE_RATIO).round().clamp(PRESENCE_MIN, PRESENCE_MAX) |
| 85 | } |
| 86 | |
| 87 | pub fn presence_dot(kind: PresenceKind, ring: Hsla, avatar_size: f32) -> Option<Div> { |
| 88 | let outer = presence_size(avatar_size); |
| 89 | let inner = outer - 2. * PRESENCE_RING; |
| 90 | let fill = match kind { |
| 91 | PresenceKind::Available => theme::green(), |
| 92 | PresenceKind::Busy | PresenceKind::DoNotDisturb => theme::red(), |
| 93 | PresenceKind::Away => theme::amber(), |
| 94 | PresenceKind::Offline => theme::background(), |
| 95 | PresenceKind::Unknown => return None, |
| 96 | }; |
| 97 | let mut center = div() |
| 98 | .size(px(inner)) |
| 99 | .rounded_full() |
| 100 | .bg(fill) |
| 101 | .flex() |
| 102 | .items_center() |
| 103 | .justify_center(); |
| 104 | center = match kind { |
| 105 | PresenceKind::DoNotDisturb => center.child( |
| 106 | div() |
| 107 | .w(px(inner - 4.)) |
| 108 | .h(px(2.)) |
| 109 | .rounded(px(1.)) |
| 110 | .bg(ring), |
| 111 | ), |
| 112 | PresenceKind::Offline => center.border(px(1.5)).border_color(theme::text_muted()), |
| 113 | _ => center, |
| 114 | }; |
| 115 | Some( |
| 116 | div() |
| 117 | .absolute() |
| 118 | .right(px(-2.)) |
| 119 | .bottom(px(-2.)) |
| 120 | .size(px(outer)) |
| 121 | .rounded_full() |
| 122 | .bg(ring) |
| 123 | .flex() |
| 124 | .items_center() |
| 125 | .justify_center() |
| 126 | .child(center), |
| 127 | ) |
| 128 | } |
| 129 | |
| 130 | pub fn with_presence(avatar: AnyElement, kind: PresenceKind, size: f32, ring: Hsla) -> Div { |
| 131 | let mut wrapper = div().relative().size(px(size)).flex_none().child(avatar); |
| 132 | if let Some(dot) = presence_dot(kind, ring, size) { |
| 133 | wrapper = wrapper.child(dot); |
| 134 | } |
| 135 | wrapper |
| 136 | } |
| 137 | |
| 138 | pub fn spec_avatar(directory: &Directory, spec: &AvatarSpec, size: f32, ring: Hsla) -> AnyElement { |
| 139 | match spec { |
| 140 | AvatarSpec::Single(face) => face_circle(directory, face, size, INITIALS_RATIO), |
| 141 | AvatarSpec::Pair(first, second) => { |
| 142 | let small = (size * PAIR_RATIO).round(); |
| 143 | div() |
| 144 | .relative() |
| 145 | .size(px(size)) |
| 146 | .flex_none() |
| 147 | .child(div().absolute().left_0().top_0().child(face_circle( |
| 148 | directory, |
| 149 | first, |
| 150 | small, |
| 151 | PAIR_INITIALS_RATIO, |
| 152 | ))) |
| 153 | .child( |
| 154 | div() |
| 155 | .absolute() |
| 156 | .right_0() |
| 157 | .bottom_0() |
| 158 | .size(px(small)) |
| 159 | .flex() |
| 160 | .items_center() |
| 161 | .justify_center() |
| 162 | .rounded_full() |
| 163 | .bg(ring) |
| 164 | .child(face_circle( |
| 165 | directory, |
| 166 | second, |
| 167 | small - 2. * PRESENCE_RING, |
| 168 | PAIR_INITIALS_RATIO, |
| 169 | )), |
| 170 | ) |
| 171 | .into_any_element() |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | pub fn square_avatar(name: &str, key: &str, size: f32, radius: f32) -> Div { |
| 177 | div() |
| 178 | .size(px(size)) |
| 179 | .flex_none() |
| 180 | .flex() |
| 181 | .items_center() |
| 182 | .justify_center() |
| 183 | .rounded(px(radius)) |
| 184 | .bg(theme::avatar_color(format::palette_index(key))) |
| 185 | .text_color(theme::text()) |
| 186 | .text_size(px((size * 0.4).round())) |
| 187 | .font_weight(FontWeight::SEMIBOLD) |
| 188 | .child(format::initials(name)) |
| 189 | } |
| 190 | |
| 191 | pub fn member_stack(directory: &Directory, faces: &[Face], total: usize) -> Div { |
| 192 | const LIMIT: usize = 3; |
| 193 | const SIZE: f32 = 22.; |
| 194 | const OVERLAP: f32 = 6.; |
| 195 | let visible = faces.len().min(LIMIT) + usize::from(total > LIMIT); |
| 196 | let width = SIZE * visible as f32 - OVERLAP * visible.saturating_sub(1) as f32; |
| 197 | let mut stack = div().flex().flex_none().w(px(width)).items_center(); |
| 198 | for (index, face) in faces.iter().take(LIMIT).enumerate() { |
| 199 | stack = stack.child( |
| 200 | div() |
| 201 | .when(index > 0, |element| element.ml(px(-OVERLAP))) |
| 202 | .size(px(SIZE)) |
| 203 | .flex_none() |
| 204 | .flex() |
| 205 | .items_center() |
| 206 | .justify_center() |
| 207 | .rounded_full() |
| 208 | .bg(theme::background()) |
| 209 | .child(face_circle( |
| 210 | directory, |
| 211 | face, |
| 212 | SIZE - 2. * PRESENCE_RING, |
| 213 | 0.45, |
| 214 | )), |
| 215 | ); |
| 216 | } |
| 217 | if total > LIMIT { |
| 218 | stack = stack.child( |
| 219 | div() |
| 220 | .ml(px(-OVERLAP)) |
| 221 | .size(px(SIZE)) |
| 222 | .flex_none() |
| 223 | .flex() |
| 224 | .items_center() |
| 225 | .justify_center() |
| 226 | .rounded_full() |
| 227 | .border(px(PRESENCE_RING)) |
| 228 | .border_color(theme::background()) |
| 229 | .bg(theme::surface_raised()) |
| 230 | .text_color(theme::text_soft()) |
| 231 | .text_size(px(10.)) |
| 232 | .font_weight(FontWeight::SEMIBOLD) |
| 233 | .child(format!("+{}", total - LIMIT)), |
| 234 | ); |
| 235 | } |
| 236 | stack |
| 237 | } |
| 238 | |
| 239 | #[cfg(test)] |
| 240 | mod tests { |
| 241 | use super::presence_size; |
| 242 | |
| 243 | #[test] |
| 244 | fn presence_dot_scales_with_the_avatar() { |
| 245 | assert_eq!(presence_size(26.), 10.); |
| 246 | assert_eq!(presence_size(36.), 14.); |
| 247 | assert_eq!(presence_size(20.), 10.); |
| 248 | } |
| 249 | } |