| 1 | use std::collections::HashMap; |
| 2 | use std::time::Instant; |
| 3 | |
| 4 | use gpui_kit::component::h_flex; |
| 5 | use gpui_kit::prelude::FluentBuilder as _; |
| 6 | use gpui_kit::*; |
| 7 | use teams_core::JumboEmoji; |
| 8 | |
| 9 | use crate::emoticons::{self, StripLoader}; |
| 10 | use crate::theme; |
| 11 | |
| 12 | const EMOTICON_SIZE: f32 = 50.; |
| 13 | const GLYPH_SIZE: f32 = 40.; |
| 14 | const EMOTICON_GAP: f32 = 4.; |
| 15 | /// When each big emoji started its one play. Kept for the app's lifetime, so rows that |
| 16 | /// are laid out off screen or rebuilt keep their start and a reopened chat does not replay. |
| 17 | #[derive(Default)] |
| 18 | struct PlayedEmoticons(HashMap<ElementId, Instant>); |
| 19 | |
| 20 | impl Global for PlayedEmoticons {} |
| 21 | |
| 22 | /// Server message id of a sent message to the key its pending row had, so the big emoji |
| 23 | /// keeps playing across the swap from the pending row to the server copy. |
| 24 | #[derive(Default)] |
| 25 | struct SentEmoticonKeys(HashMap<String, String>); |
| 26 | |
| 27 | impl Global for SentEmoticonKeys {} |
| 28 | |
| 29 | pub fn hand_over_sent(pending_key: &str, message_id: &str, cx: &mut App) { |
| 30 | cx.default_global::<SentEmoticonKeys>() |
| 31 | .0 |
| 32 | .insert(message_id.to_owned(), pending_key.to_owned()); |
| 33 | } |
| 34 | |
| 35 | fn play_start(id: &ElementId, active: bool, now: Instant, cx: &mut App) -> Option<Instant> { |
| 36 | let played = cx.default_global::<PlayedEmoticons>(); |
| 37 | match played.0.get(id) { |
| 38 | Some(started) => Some(*started), |
| 39 | None if active => { |
| 40 | played.0.insert(id.clone(), now); |
| 41 | Some(now) |
| 42 | } |
| 43 | None => None, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[derive(IntoElement)] |
| 48 | struct JumboEmoticon { |
| 49 | id: ElementId, |
| 50 | emoji: JumboEmoji, |
| 51 | } |
| 52 | |
| 53 | fn glyph(emoji: &JumboEmoji) -> AnyElement { |
| 54 | div() |
| 55 | .h(px(EMOTICON_SIZE)) |
| 56 | .flex() |
| 57 | .items_center() |
| 58 | .text_size(px(GLYPH_SIZE)) |
| 59 | .font_family(theme::EMOJI_FONT_FAMILY) |
| 60 | .text_color(theme::text()) |
| 61 | .child(emoji.glyph.clone()) |
| 62 | .into_any_element() |
| 63 | } |
| 64 | |
| 65 | impl RenderOnce for JumboEmoticon { |
| 66 | fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { |
| 67 | let Some(resolved) = emoticons::resolve(self.emoji.id.as_deref(), &self.emoji.glyph) else { |
| 68 | return glyph(&self.emoji); |
| 69 | }; |
| 70 | let images = match window.use_asset::<StripLoader>(&resolved.strip_source(), cx) { |
| 71 | Some(Ok(images)) => images, |
| 72 | Some(Err(_)) => return glyph(&self.emoji), |
| 73 | None => return div().size(px(EMOTICON_SIZE)).into_any_element(), |
| 74 | }; |
| 75 | let now = Instant::now(); |
| 76 | let started = play_start(&self.id, window.is_window_active(), now, cx); |
| 77 | let playing = |
| 78 | started.is_some_and(|started| now.duration_since(started) < resolved.loop_duration()); |
| 79 | let image = if playing { |
| 80 | images.animated.clone() |
| 81 | } else { |
| 82 | images.still.clone() |
| 83 | }; |
| 84 | let image_id = ElementId::Name(format!("{}-image", self.id).into()); |
| 85 | div() |
| 86 | .id(self.id.clone()) |
| 87 | .size(px(EMOTICON_SIZE)) |
| 88 | .flex_none() |
| 89 | .on_hover({ |
| 90 | let id = self.id; |
| 91 | move |hovered, window, cx| { |
| 92 | if *hovered { |
| 93 | cx.default_global::<PlayedEmoticons>() |
| 94 | .0 |
| 95 | .insert(id.clone(), Instant::now()); |
| 96 | window.refresh(); |
| 97 | } |
| 98 | } |
| 99 | }) |
| 100 | .child(img(ImageSource::Render(image)).id(image_id).size_full()) |
| 101 | .into_any_element() |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | pub fn jumbo_row(message_key: &str, jumbo: &[JumboEmoji], own: bool, cx: &App) -> Div { |
| 106 | let message_key = cx |
| 107 | .try_global::<SentEmoticonKeys>() |
| 108 | .and_then(|sent| sent.0.get(message_key)) |
| 109 | .map_or(message_key, String::as_str); |
| 110 | h_flex() |
| 111 | .gap(px(EMOTICON_GAP)) |
| 112 | .when(own, |row| row.justify_end()) |
| 113 | .children( |
| 114 | jumbo |
| 115 | .iter() |
| 116 | .enumerate() |
| 117 | .map(|(index, emoji)| JumboEmoticon { |
| 118 | id: ElementId::Name(format!("jumbo-{message_key}-{index}").into()), |
| 119 | emoji: emoji.clone(), |
| 120 | }), |
| 121 | ) |
| 122 | } |