diff --git a/crates/client/src/gui.rs b/crates/client/src/gui.rs index 74689d9..8aa4814 100644 --- a/crates/client/src/gui.rs +++ b/crates/client/src/gui.rs @@ -842,6 +842,7 @@ impl Yumush { let _ = this.update(cx, |yumush, cx| { yumush.chat.avatar_requests.remove(&avatar_id); if let Ok(bytes) = result { + style::picture(avatar_id, &bytes); yumush.chat.avatars.insert( avatar_id, Arc::new(Image::from_bytes(ImageFormat::Jpeg, bytes)), diff --git a/crates/client/src/gui/chat.rs b/crates/client/src/gui/chat.rs index 46dab2f..fe9442e 100644 --- a/crates/client/src/gui/chat.rs +++ b/crates/client/src/gui/chat.rs @@ -147,6 +147,7 @@ pub struct Chat { pub(super) invite: InviteLookup, pub(super) sidebar_collapsed: bool, pub(super) sidebar_toggled: Option, + pub(super) offline_open: bool, pub(super) composer: Composer, pub(super) drafts: HashMap, pub(super) poll_question: Entity, @@ -393,6 +394,7 @@ impl Chat { invite: InviteLookup::Idle, sidebar_collapsed: false, sidebar_toggled: None, + offline_open: false, view: View::Chat, section: 0, username_input: editable_input("Username...", Yumush::save_username, window, cx), diff --git a/crates/client/src/gui/sidebar.rs b/crates/client/src/gui/sidebar.rs index 87f99c3..94733c7 100644 --- a/crates/client/src/gui/sidebar.rs +++ b/crates/client/src/gui/sidebar.rs @@ -421,20 +421,17 @@ impl Yumush { .children(found.map(|user| self.found_row(entity, user, palette))) .children(self.requests(entity, palette)) .children( - [ - (Doing::Call, "In a call"), - (Doing::Online, "Online"), - (Doing::Offline, "Offline"), - ] - .into_iter() - .filter_map(|(doing, title)| { - group( - title, - palette, - self.friend_rows(entity, query, doing, palette), - ) - }), - ), + [(Doing::Call, "In a call"), (Doing::Online, "Online")] + .into_iter() + .filter_map(|(doing, title)| { + group( + title, + palette, + self.friend_rows(entity, query, doing, palette), + ) + }), + ) + .children(self.offline(entity, query, palette)), ), ) } @@ -621,6 +618,52 @@ impl Yumush { .collect() } + fn offline(&self, entity: &Entity, query: &str, palette: Palette) -> Option
{ + let rows = self.friend_rows(entity, query, Doing::Offline, palette); + if rows.is_empty() { + return None; + } + let open = self.chat.offline_open || !query.is_empty(); + let t = palette.tokens; + Some( + div() + .flex() + .flex_col() + .gap(px(space::XXS)) + .child( + clickable( + "friends_offline", + palette, + entity_action(entity, |yumush, _, cx| { + yumush.chat.offline_open = !yumush.chat.offline_open; + cx.notify(); + }), + ) + .px(px(space::SM)) + .h(px(size::CONTROL_SM)) + .flex() + .items_center() + .gap(px(space::XS)) + .child(super::parts::words( + format!("{} offline", rows.len()), + super::parts::Text::Caption, + super::parts::Ink::Dim, + t, + )) + .child( + Icon::new(if open { + IconName::ChevronDown + } else { + IconName::ChevronRight + }) + .size(px(size::ICON_SM)) + .text_color(t.faint()), + ), + ) + .when(open, |this| this.children(rows)), + ) + } + pub(super) fn call_of(&self, user_id: Id) -> Option<&Community> { let community_id = self .chat diff --git a/crates/client/src/gui/style.rs b/crates/client/src/gui/style.rs index 5d7aa35..bed6a52 100644 --- a/crates/client/src/gui/style.rs +++ b/crates/client/src/gui/style.rs @@ -106,24 +106,78 @@ impl Tokens { pub const PICKS: &[Hue] = Hue::ALL.split_last().unwrap().1; +const PICTURE_SIDE: u32 = 16; +const PICTURE_COLOURFUL: f32 = 0.15; + thread_local! { static CHOSEN: RefCell> = RefCell::default(); + static AVATAR: RefCell> = RefCell::default(); + static PICTURED: RefCell> = RefCell::default(); } pub fn remember(users: &[&User]) { CHOSEN.with_borrow_mut(|chosen| { - for user in users { - match user.hue { - Some(hue) => chosen.insert(user.id, hue), - None => chosen.remove(&user.id), - }; - } + AVATAR.with_borrow_mut(|avatar| { + for user in users { + match user.hue { + Some(hue) => chosen.insert(user.id, hue), + None => chosen.remove(&user.id), + }; + match user.avatar_id { + Some(avatar_id) => avatar.insert(user.id, avatar_id), + None => avatar.remove(&user.id), + }; + } + }) + }); +} + +pub fn picture(avatar_id: Id, bytes: &[u8]) { + if let Some(hue) = picture_hue(bytes) { + PICTURED.with_borrow_mut(|pictured| pictured.insert(avatar_id, hue)); + } +} + +fn picture_hue(bytes: &[u8]) -> Option { + let image = image::load_from_memory(bytes) + .ok()? + .thumbnail(PICTURE_SIDE, PICTURE_SIDE) + .to_rgb8(); + let (x, y) = image.pixels().fold((0., 0.), |(x, y), pixel| { + let colour = angle_of(Hsla::from(gpui_kit::Rgba { + r: pixel[0] as f32 / 255., + g: pixel[1] as f32 / 255., + b: pixel[2] as f32 / 255., + a: 1., + })); + (x + colour.0, y + colour.1) }); + let pixels = image.pixels().len() as f32; + if x.hypot(y) < PICTURE_COLOURFUL * pixels { + return None; + } + PICKS.iter().copied().max_by(|a, b| { + let near = |hue: Hue| { + let (hx, hy) = angle_of(color(Theme::Gece.colors().hue[hue as usize])); + hx * x + hy * y + }; + near(*a).total_cmp(&near(*b)) + }) +} + +fn angle_of(colour: Hsla) -> (f32, f32) { + let weight = colour.s * (1. - (2. * colour.l - 1.).abs()); + let turn = colour.h * std::f32::consts::TAU; + (weight * turn.cos(), weight * turn.sin()) } pub fn hue_of(id: Id) -> Hue { CHOSEN .with_borrow(|chosen| chosen.get(&id).copied()) + .or_else(|| { + let avatar_id = AVATAR.with_borrow(|avatar| avatar.get(&id).copied())?; + PICTURED.with_borrow(|pictured| pictured.get(&avatar_id).copied()) + }) .unwrap_or(PICKS[id.rem_euclid(PICKS.len() as Id) as usize]) } @@ -135,6 +189,24 @@ fn color(value: u32) -> Hsla { mod tests { use super::*; + fn png(rgb: [u8; 3]) -> Vec { + let mut bytes = Vec::new(); + image::RgbImage::from_pixel(8, 8, image::Rgb(rgb)) + .write_to( + &mut std::io::Cursor::new(&mut bytes), + image::ImageFormat::Png, + ) + .unwrap(); + bytes + } + + #[test] + fn picture_gives_nearest_hue() { + assert_eq!(picture_hue(&png([230, 120, 20])), Some(Hue::Amber)); + assert_eq!(picture_hue(&png([30, 140, 230])), Some(Hue::Sky)); + assert_eq!(picture_hue(&png([120, 120, 120])), None); + } + #[test] fn hues_match_tokens() { let names: Vec<&str> = Hue::ALL.iter().map(|hue| hue.as_str()).collect();