BlueGit

ict rusty-teams

Files

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

jumbo_emoji.rs

jumbo_emoji.rs 3.2 KiB Download Raw
1use std::time::Instant;
2
3use gpui_kit::component::h_flex;
4use gpui_kit::prelude::FluentBuilder as _;
5use gpui_kit::*;
6use teams_core::JumboEmoji;
7
8use crate::emoticons::{self, StripLoader};
9use crate::theme;
10
11const EMOTICON_SIZE: f32 = 50.;
12const GLYPH_SIZE: f32 = 40.;
13const EMOTICON_GAP: f32 = 4.;
14const APPEAR_LOOPS: u32 = 2;
15const HOVER_LOOPS: u32 = 5;
16
17struct Playback {
18 started: Option<Instant>,
19 loops: u32,
20}
21
22#[derive(IntoElement)]
23struct JumboEmoticon {
24 id: ElementId,
25 emoji: JumboEmoji,
26}
27
28fn glyph(emoji: &JumboEmoji) -> AnyElement {
29 div()
30 .h(px(EMOTICON_SIZE))
31 .flex()
32 .items_center()
33 .text_size(px(GLYPH_SIZE))
34 .font_family(theme::EMOJI_FONT_FAMILY)
35 .text_color(theme::text())
36 .child(emoji.glyph.clone())
37 .into_any_element()
38}
39
40impl RenderOnce for JumboEmoticon {
41 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
42 let Some(resolved) = emoticons::resolve(self.emoji.id.as_deref(), &self.emoji.glyph) else {
43 return glyph(&self.emoji);
44 };
45 let images = match window.use_asset::<StripLoader>(&resolved.strip_source(), cx) {
46 Some(Ok(images)) => images,
47 Some(Err(_)) => return glyph(&self.emoji),
48 None => return div().size(px(EMOTICON_SIZE)).into_any_element(),
49 };
50 let playback = window.use_keyed_state(self.id.clone(), cx, |_, _| Playback {
51 started: None,
52 loops: APPEAR_LOOPS,
53 });
54 let now = Instant::now();
55 let active = window.is_window_active();
56 let (started, loops) = playback.update(cx, |playback, _| {
57 if !active {
58 playback.started = None;
59 }
60 let started = active.then(|| *playback.started.get_or_insert(now));
61 (started, playback.loops)
62 });
63 let playing = started
64 .is_some_and(|started| now.duration_since(started) < resolved.loop_duration() * loops);
65 let image = if playing {
66 images.animated.clone()
67 } else {
68 images.still.clone()
69 };
70 let image_id = ElementId::Name(format!("{}-image", self.id).into());
71 div()
72 .id(self.id)
73 .size(px(EMOTICON_SIZE))
74 .flex_none()
75 .on_hover(move |hovered, _, cx| {
76 if *hovered {
77 playback.update(cx, |playback, cx| {
78 playback.started = Some(Instant::now());
79 playback.loops = HOVER_LOOPS;
80 cx.notify();
81 });
82 }
83 })
84 .child(img(ImageSource::Render(image)).id(image_id).size_full())
85 .into_any_element()
86 }
87}
88
89pub fn jumbo_row(message_key: &str, jumbo: &[JumboEmoji], own: bool) -> Div {
90 h_flex()
91 .gap(px(EMOTICON_GAP))
92 .when(own, |row| row.justify_end())
93 .children(
94 jumbo
95 .iter()
96 .enumerate()
97 .map(|(index, emoji)| JumboEmoticon {
98 id: ElementId::Name(format!("jumbo-{message_key}-{index}").into()),
99 emoji: emoji.clone(),
100 }),
101 )
102}