fix(chat): big emojis play once like Teams
- Play start is kept app-wide per emoji, so rows rebuilt or laid out off screen keep it - Plays once when the window is active, again on hover, then shows the still frame Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -1,4 +1,4 @@
1
use std::collections::HashSet;
1
use std::collections::HashMap;
2
2
use std::time::Instant;
3
3
4
4
use gpui_kit::component::h_flex;
@@ -12,17 +12,23 @@ use crate::theme;
12
12
const EMOTICON_SIZE: f32 = 50.;
13
13
const GLYPH_SIZE: f32 = 40.;
14
14
const EMOTICON_GAP: f32 = 4.;
15
struct Playback {
16
started: Option<Instant>,
17
}
18
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.
19
17
#[derive(Default)]
20
struct PlayedEmoticons(HashSet<ElementId>);
18
struct PlayedEmoticons(HashMap<ElementId, Instant>);
21
19
22
20
impl Global for PlayedEmoticons {}
23
21
24
fn first_play(id: &ElementId, cx: &mut App) -> bool {
25
cx.default_global::<PlayedEmoticons>().0.insert(id.clone())
22
fn play_start(id: &ElementId, active: bool, now: Instant, cx: &mut App) -> Option<Instant> {
23
let played = cx.default_global::<PlayedEmoticons>();
24
match played.0.get(id) {
25
Some(started) => Some(*started),
26
None if active => {
27
played.0.insert(id.clone(), now);
28
Some(now)
29
}
30
None => None,
31
}
26
32
}
27
33
28
34
#[derive(IntoElement)]
@@ -53,17 +59,8 @@ impl RenderOnce for JumboEmoticon {
53
59
Some(Err(_)) => return glyph(&self.emoji),
54
60
None => return div().size(px(EMOTICON_SIZE)).into_any_element(),
55
61
};
56
let playback =
57
window.use_keyed_state(self.id.clone(), cx, |_, _| Playback { started: None });
58
62
let now = Instant::now();
59
let active = window.is_window_active();
60
let unplayed = active && playback.read(cx).started.is_none() && first_play(&self.id, cx);
61
let started = playback.update(cx, |playback, _| {
62
if unplayed {
63
playback.started = Some(now);
64
}
65
playback.started
66
});
63
let started = play_start(&self.id, window.is_window_active(), now, cx);
67
64
let playing =
68
65
started.is_some_and(|started| now.duration_since(started) < resolved.loop_duration());
69
66
let image = if playing {
@@ -73,15 +70,18 @@ impl RenderOnce for JumboEmoticon {
73
70
};
74
71
let image_id = ElementId::Name(format!("{}-image", self.id).into());
75
72
div()
76
.id(self.id)
73
.id(self.id.clone())
77
74
.size(px(EMOTICON_SIZE))
78
75
.flex_none()
79
.on_hover(move |hovered, _, cx| {
80
if *hovered {
81
playback.update(cx, |playback, cx| {
82
playback.started = Some(Instant::now());
83
cx.notify();
84
});
76
.on_hover({
77
let id = self.id;
78
move |hovered, window, cx| {
79
if *hovered {
80
cx.default_global::<PlayedEmoticons>()
81
.0
82
.insert(id.clone(), Instant::now());
83
window.refresh();
84
}
85
85
}
86
86
})
87
87
.child(img(ImageSource::Render(image)).id(image_id).size_full())
@@ -1,4 +1,4 @@1
use std::collections::HashSet;1
use std::collections::HashMap;2
use std::time::Instant;2
use std::time::Instant;3
3
4
use gpui_kit::component::h_flex;4
use gpui_kit::component::h_flex;@@ -12,17 +12,23 @@ use crate::theme;12
const EMOTICON_SIZE: f32 = 50.;12
const EMOTICON_SIZE: f32 = 50.;13
const GLYPH_SIZE: f32 = 40.;13
const GLYPH_SIZE: f32 = 40.;14
const EMOTICON_GAP: f32 = 4.;14
const EMOTICON_GAP: f32 = 4.;15
struct Playback {15
/// When each big emoji started its one play. Kept for the app's lifetime, so rows that16
started: Option<Instant>,16
/// are laid out off screen or rebuilt keep their start and a reopened chat does not replay.17
}18
19
#[derive(Default)]17
#[derive(Default)]20
struct PlayedEmoticons(HashSet<ElementId>);18
struct PlayedEmoticons(HashMap<ElementId, Instant>);21
19
22
impl Global for PlayedEmoticons {}20
impl Global for PlayedEmoticons {}23
21
24
fn first_play(id: &ElementId, cx: &mut App) -> bool {22
fn play_start(id: &ElementId, active: bool, now: Instant, cx: &mut App) -> Option<Instant> {25
cx.default_global::<PlayedEmoticons>().0.insert(id.clone())23
let played = cx.default_global::<PlayedEmoticons>();24
match played.0.get(id) {25
Some(started) => Some(*started),26
None if active => {27
played.0.insert(id.clone(), now);28
Some(now)29
}30
None => None,31
}26
}32
}27
33
28
#[derive(IntoElement)]34
#[derive(IntoElement)]@@ -53,17 +59,8 @@ impl RenderOnce for JumboEmoticon {53
Some(Err(_)) => return glyph(&self.emoji),59
Some(Err(_)) => return glyph(&self.emoji),54
None => return div().size(px(EMOTICON_SIZE)).into_any_element(),60
None => return div().size(px(EMOTICON_SIZE)).into_any_element(),55
};61
};56
let playback =57
window.use_keyed_state(self.id.clone(), cx, |_, _| Playback { started: None });58
let now = Instant::now();62
let now = Instant::now();59
let active = window.is_window_active();63
let started = play_start(&self.id, window.is_window_active(), now, cx);60
let unplayed = active && playback.read(cx).started.is_none() && first_play(&self.id, cx);61
let started = playback.update(cx, |playback, _| {62
if unplayed {63
playback.started = Some(now);64
}65
playback.started66
});67
let playing =64
let playing =68
started.is_some_and(|started| now.duration_since(started) < resolved.loop_duration());65
started.is_some_and(|started| now.duration_since(started) < resolved.loop_duration());69
let image = if playing {66
let image = if playing {@@ -73,15 +70,18 @@ impl RenderOnce for JumboEmoticon {73
};70
};74
let image_id = ElementId::Name(format!("{}-image", self.id).into());71
let image_id = ElementId::Name(format!("{}-image", self.id).into());75
div()72
div()76
.id(self.id)73
.id(self.id.clone())77
.size(px(EMOTICON_SIZE))74
.size(px(EMOTICON_SIZE))78
.flex_none()75
.flex_none()79
.on_hover(move |hovered, _, cx| {76
.on_hover({80
if *hovered {77
let id = self.id;81
playback.update(cx, |playback, cx| {78
move |hovered, window, cx| {82
playback.started = Some(Instant::now());79
if *hovered {83
cx.notify();80
cx.default_global::<PlayedEmoticons>()84
});81
.082
.insert(id.clone(), Instant::now());83
window.refresh();84
}85
}85
}86
})86
})87
.child(img(ImageSource::Render(image)).id(image_id).size_full())87
.child(img(ImageSource::Render(image)).id(image_id).size_full())