Offline friends folded behind a count
Browse files at this commit51c4d9869b9cb49bc2df64661bc2dc8e44f86348Parent: e65f020- feat
- offline friends folded behind a count
- your hue comes from your picture until you pick one
4 changed files +138 -20
- crates/client/src/gui.rs +1 -0
- crates/client/src/gui/chat.rs +2 -0
- crates/client/src/gui/sidebar.rs +57 -14
- crates/client/src/gui/style.rs +78 -6
crates/client/src/gui.rsmodified+1 -0
| @@ -842,6 +842,7 @@ impl Yumush { | ||
| 842 | 842 | let _ = this.update(cx, |yumush, cx| { |
| 843 | 843 | yumush.chat.avatar_requests.remove(&avatar_id); |
| 844 | 844 | if let Ok(bytes) = result { |
| 845 | + style::picture(avatar_id, &bytes); | |
| 845 | 846 | yumush.chat.avatars.insert( |
| 846 | 847 | avatar_id, |
| 847 | 848 | Arc::new(Image::from_bytes(ImageFormat::Jpeg, bytes)), |
crates/client/src/gui/chat.rsmodified+2 -0
| @@ -147,6 +147,7 @@ pub struct Chat { | ||
| 147 | 147 | pub(super) invite: InviteLookup, |
| 148 | 148 | pub(super) sidebar_collapsed: bool, |
| 149 | 149 | pub(super) sidebar_toggled: Option<std::time::Instant>, |
| 150 | + pub(super) offline_open: bool, | |
| 150 | 151 | pub(super) composer: Composer, |
| 151 | 152 | pub(super) drafts: HashMap<Id, String>, |
| 152 | 153 | pub(super) poll_question: Entity<InputState>, |
| @@ -393,6 +394,7 @@ impl Chat { | ||
| 393 | 394 | invite: InviteLookup::Idle, |
| 394 | 395 | sidebar_collapsed: false, |
| 395 | 396 | sidebar_toggled: None, |
| 397 | + offline_open: false, | |
| 396 | 398 | view: View::Chat, |
| 397 | 399 | section: 0, |
| 398 | 400 | username_input: editable_input("Username...", Yumush::save_username, window, cx), |
crates/client/src/gui/sidebar.rsmodified+57 -14
| @@ -421,20 +421,17 @@ impl Yumush { | ||
| 421 | 421 | .children(found.map(|user| self.found_row(entity, user, palette))) |
| 422 | 422 | .children(self.requests(entity, palette)) |
| 423 | 423 | .children( |
| 424 | - [ | |
| 425 | - (Doing::Call, "In a call"), | |
| 426 | - (Doing::Online, "Online"), | |
| 427 | - (Doing::Offline, "Offline"), | |
| 428 | - ] | |
| 429 | - .into_iter() | |
| 430 | - .filter_map(|(doing, title)| { | |
| 431 | - group( | |
| 432 | - title, | |
| 433 | - palette, | |
| 434 | - self.friend_rows(entity, query, doing, palette), | |
| 435 | - ) | |
| 436 | - }), | |
| 437 | - ), | |
| 424 | + [(Doing::Call, "In a call"), (Doing::Online, "Online")] | |
| 425 | + .into_iter() | |
| 426 | + .filter_map(|(doing, title)| { | |
| 427 | + group( | |
| 428 | + title, | |
| 429 | + palette, | |
| 430 | + self.friend_rows(entity, query, doing, palette), | |
| 431 | + ) | |
| 432 | + }), | |
| 433 | + ) | |
| 434 | + .children(self.offline(entity, query, palette)), | |
| 438 | 435 | ), |
| 439 | 436 | ) |
| 440 | 437 | } |
| @@ -621,6 +618,52 @@ impl Yumush { | ||
| 621 | 618 | .collect() |
| 622 | 619 | } |
| 623 | 620 | |
| 621 | + fn offline(&self, entity: &Entity<Self>, query: &str, palette: Palette) -> Option<Div> { | |
| 622 | + let rows = self.friend_rows(entity, query, Doing::Offline, palette); | |
| 623 | + if rows.is_empty() { | |
| 624 | + return None; | |
| 625 | + } | |
| 626 | + let open = self.chat.offline_open || !query.is_empty(); | |
| 627 | + let t = palette.tokens; | |
| 628 | + Some( | |
| 629 | + div() | |
| 630 | + .flex() | |
| 631 | + .flex_col() | |
| 632 | + .gap(px(space::XXS)) | |
| 633 | + .child( | |
| 634 | + clickable( | |
| 635 | + "friends_offline", | |
| 636 | + palette, | |
| 637 | + entity_action(entity, |yumush, _, cx| { | |
| 638 | + yumush.chat.offline_open = !yumush.chat.offline_open; | |
| 639 | + cx.notify(); | |
| 640 | + }), | |
| 641 | + ) | |
| 642 | + .px(px(space::SM)) | |
| 643 | + .h(px(size::CONTROL_SM)) | |
| 644 | + .flex() | |
| 645 | + .items_center() | |
| 646 | + .gap(px(space::XS)) | |
| 647 | + .child(super::parts::words( | |
| 648 | + format!("{} offline", rows.len()), | |
| 649 | + super::parts::Text::Caption, | |
| 650 | + super::parts::Ink::Dim, | |
| 651 | + t, | |
| 652 | + )) | |
| 653 | + .child( | |
| 654 | + Icon::new(if open { | |
| 655 | + IconName::ChevronDown | |
| 656 | + } else { | |
| 657 | + IconName::ChevronRight | |
| 658 | + }) | |
| 659 | + .size(px(size::ICON_SM)) | |
| 660 | + .text_color(t.faint()), | |
| 661 | + ), | |
| 662 | + ) | |
| 663 | + .when(open, |this| this.children(rows)), | |
| 664 | + ) | |
| 665 | + } | |
| 666 | + | |
| 624 | 667 | pub(super) fn call_of(&self, user_id: Id) -> Option<&Community> { |
| 625 | 668 | let community_id = self |
| 626 | 669 | .chat |
crates/client/src/gui/style.rsmodified+78 -6
| @@ -106,24 +106,78 @@ impl Tokens { | ||
| 106 | 106 | |
| 107 | 107 | pub const PICKS: &[Hue] = Hue::ALL.split_last().unwrap().1; |
| 108 | 108 | |
| 109 | +const PICTURE_SIDE: u32 = 16; | |
| 110 | +const PICTURE_COLOURFUL: f32 = 0.15; | |
| 111 | + | |
| 109 | 112 | thread_local! { |
| 110 | 113 | static CHOSEN: RefCell<HashMap<Id, Hue>> = RefCell::default(); |
| 114 | + static AVATAR: RefCell<HashMap<Id, Id>> = RefCell::default(); | |
| 115 | + static PICTURED: RefCell<HashMap<Id, Hue>> = RefCell::default(); | |
| 111 | 116 | } |
| 112 | 117 | |
| 113 | 118 | pub fn remember(users: &[&User]) { |
| 114 | 119 | CHOSEN.with_borrow_mut(|chosen| { |
| 115 | - for user in users { | |
| 116 | - match user.hue { | |
| 117 | - Some(hue) => chosen.insert(user.id, hue), | |
| 118 | - None => chosen.remove(&user.id), | |
| 119 | - }; | |
| 120 | - } | |
| 120 | + AVATAR.with_borrow_mut(|avatar| { | |
| 121 | + for user in users { | |
| 122 | + match user.hue { | |
| 123 | + Some(hue) => chosen.insert(user.id, hue), | |
| 124 | + None => chosen.remove(&user.id), | |
| 125 | + }; | |
| 126 | + match user.avatar_id { | |
| 127 | + Some(avatar_id) => avatar.insert(user.id, avatar_id), | |
| 128 | + None => avatar.remove(&user.id), | |
| 129 | + }; | |
| 130 | + } | |
| 131 | + }) | |
| 132 | + }); | |
| 133 | +} | |
| 134 | + | |
| 135 | +pub fn picture(avatar_id: Id, bytes: &[u8]) { | |
| 136 | + if let Some(hue) = picture_hue(bytes) { | |
| 137 | + PICTURED.with_borrow_mut(|pictured| pictured.insert(avatar_id, hue)); | |
| 138 | + } | |
| 139 | +} | |
| 140 | + | |
| 141 | +fn picture_hue(bytes: &[u8]) -> Option<Hue> { | |
| 142 | + let image = image::load_from_memory(bytes) | |
| 143 | + .ok()? | |
| 144 | + .thumbnail(PICTURE_SIDE, PICTURE_SIDE) | |
| 145 | + .to_rgb8(); | |
| 146 | + let (x, y) = image.pixels().fold((0., 0.), |(x, y), pixel| { | |
| 147 | + let colour = angle_of(Hsla::from(gpui_kit::Rgba { | |
| 148 | + r: pixel[0] as f32 / 255., | |
| 149 | + g: pixel[1] as f32 / 255., | |
| 150 | + b: pixel[2] as f32 / 255., | |
| 151 | + a: 1., | |
| 152 | + })); | |
| 153 | + (x + colour.0, y + colour.1) | |
| 121 | 154 | }); |
| 155 | + let pixels = image.pixels().len() as f32; | |
| 156 | + if x.hypot(y) < PICTURE_COLOURFUL * pixels { | |
| 157 | + return None; | |
| 158 | + } | |
| 159 | + PICKS.iter().copied().max_by(|a, b| { | |
| 160 | + let near = |hue: Hue| { | |
| 161 | + let (hx, hy) = angle_of(color(Theme::Gece.colors().hue[hue as usize])); | |
| 162 | + hx * x + hy * y | |
| 163 | + }; | |
| 164 | + near(*a).total_cmp(&near(*b)) | |
| 165 | + }) | |
| 166 | +} | |
| 167 | + | |
| 168 | +fn angle_of(colour: Hsla) -> (f32, f32) { | |
| 169 | + let weight = colour.s * (1. - (2. * colour.l - 1.).abs()); | |
| 170 | + let turn = colour.h * std::f32::consts::TAU; | |
| 171 | + (weight * turn.cos(), weight * turn.sin()) | |
| 122 | 172 | } |
| 123 | 173 | |
| 124 | 174 | pub fn hue_of(id: Id) -> Hue { |
| 125 | 175 | CHOSEN |
| 126 | 176 | .with_borrow(|chosen| chosen.get(&id).copied()) |
| 177 | + .or_else(|| { | |
| 178 | + let avatar_id = AVATAR.with_borrow(|avatar| avatar.get(&id).copied())?; | |
| 179 | + PICTURED.with_borrow(|pictured| pictured.get(&avatar_id).copied()) | |
| 180 | + }) | |
| 127 | 181 | .unwrap_or(PICKS[id.rem_euclid(PICKS.len() as Id) as usize]) |
| 128 | 182 | } |
| 129 | 183 | |
| @@ -135,6 +189,24 @@ fn color(value: u32) -> Hsla { | ||
| 135 | 189 | mod tests { |
| 136 | 190 | use super::*; |
| 137 | 191 | |
| 192 | + fn png(rgb: [u8; 3]) -> Vec<u8> { | |
| 193 | + let mut bytes = Vec::new(); | |
| 194 | + image::RgbImage::from_pixel(8, 8, image::Rgb(rgb)) | |
| 195 | + .write_to( | |
| 196 | + &mut std::io::Cursor::new(&mut bytes), | |
| 197 | + image::ImageFormat::Png, | |
| 198 | + ) | |
| 199 | + .unwrap(); | |
| 200 | + bytes | |
| 201 | + } | |
| 202 | + | |
| 203 | + #[test] | |
| 204 | + fn picture_gives_nearest_hue() { | |
| 205 | + assert_eq!(picture_hue(&png([230, 120, 20])), Some(Hue::Amber)); | |
| 206 | + assert_eq!(picture_hue(&png([30, 140, 230])), Some(Hue::Sky)); | |
| 207 | + assert_eq!(picture_hue(&png([120, 120, 120])), None); | |
| 208 | + } | |
| 209 | + | |
| 138 | 210 | #[test] |
| 139 | 211 | fn hues_match_tokens() { |
| 140 | 212 | let names: Vec<&str> = Hue::ALL.iter().map(|hue| hue.as_str()).collect(); |