BlueGit

ict rusty-teams

feat(chat): new messages animate in like Telegram

- own messages rise from the composer side with a soft spring overshoot (420 ms)
- incoming messages slide in from the left with fade (340 ms)
- only messages that arrive while the chat is open, never on open, sync, older history, edits or the server copy of a sent message
- offsets and opacity only, no layout shift; off with reduce motion

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-11 · 2 weeks ago

Hash
4378439aabc80b0b0938ff08c28e23e199569ffa
Parent
46ff8197
6 files changed +435 −1
crates/app/src/views/conversation.rs +30 −0
@@ -36,6 +36,7 @@ mod forward_select;
36 36 pub use forward_select::{
37 37 FORWARD_FOOTER_BAR_TAG, FORWARD_FOOTER_COUNT_TAG, FORWARD_FOOTER_NEXT_TAG, FORWARD_FOOTER_TAG,
38 38 };
39 mod appear_tracker;
39 40 mod jump;
40 41 mod notes_space;
41 42 mod pins;
@@ -285,6 +286,7 @@ pub struct ConversationView {
285 286 agent_runs: Rc<RefCell<HashMap<String, AgentRun>>>,
286 287 reaction_proposals: Rc<RefCell<HashMap<String, ReactionProposal>>>,
287 288 receipt_fade: receipt_fade::ReceiptFade,
289 appear_tracker: appear_tracker::AppearTracker,
288 290 find_panel: Option<Entity<FindPanel>>,
289 291 find_subscription: Option<Subscription>,
290 292 group_name: GroupNameSlot,
@@ -638,6 +640,7 @@ impl ConversationView {
638 640 agent_runs: Rc::new(RefCell::new(HashMap::new())),
639 641 reaction_proposals: Rc::new(RefCell::new(HashMap::new())),
640 642 receipt_fade: receipt_fade::ReceiptFade::default(),
643 appear_tracker: appear_tracker::AppearTracker::default(),
641 644 find_panel: None,
642 645 find_subscription: None,
643 646 _subscriptions: subscriptions,
@@ -1781,6 +1784,8 @@ impl ConversationView {
1781 1784 let placeholders = records.is_empty()
1782 1785 && (loading_post || current.sync.messages && thread_root.is_none());
1783 1786 let in_feed = current.mode == ViewMode::ThreadList;
1787 let appear_scope = format!("{conversation_id}/{:?}", current.mode);
1788 let appear_live = !placeholders && !in_feed && window.is_none() && !current.sync.messages;
1784 1789 let mut rows = match &current.mode {
1785 1790 _ if placeholders => {
1786 1791 current.has_older = false;
@@ -1856,6 +1861,7 @@ impl ConversationView {
1856 1861 }
1857 1862 }
1858 1863 assign_series(&mut rows);
1864 self.track_appearing(&rows, &appear_scope, appear_live, reset);
1859 1865 self.apply_rows(rows, reset, cx);
1860 1866 if self.is_detached() {
1861 1867 self.scroller
@@ -2245,6 +2251,29 @@ impl ConversationView {
2245 2251 .apply(&conversation_id, settled, receipts, Instant::now())
2246 2252 }
2247 2253
2254 fn track_appearing(&mut self, rows: &[Row], scope: &str, live: bool, reset: bool) {
2255 let sending: HashSet<&str> = self.pending.iter().map(|row| row.key.as_str()).collect();
2256 let candidates: Vec<appear_tracker::Candidate> = rows
2257 .iter()
2258 .filter_map(|row| match row {
2259 Row::Message(message) => Some(appear_tracker::Candidate {
2260 key: &message.key,
2261 own: message.own,
2262 origin: if sending.contains(message.key.as_str()) {
2263 appear_tracker::Origin::Sending
2264 } else if message.key.starts_with(SCHEDULED_KEY_PREFIX) {
2265 appear_tracker::Origin::Scheduled
2266 } else {
2267 appear_tracker::Origin::Server
2268 },
2269 }),
2270 _ => None,
2271 })
2272 .collect();
2273 self.appear_tracker
2274 .apply(scope, live, reset, &candidates, Instant::now());
2275 }
2276
2248 2277 fn schedule_fade_end(&mut self, cx: &mut Context<Self>) {
2249 2278 cx.spawn(async move |this, cx| {
2250 2279 cx.background_executor()
@@ -3988,6 +4017,7 @@ impl Render for ConversationView {
3988 4017 agent_runs: self.agent_runs.clone(),
3989 4018 reaction_proposals: self.reaction_proposals.clone(),
3990 4019 forward_selection: forward_selection.clone(),
4020 appearing: self.appear_tracker.entering(Instant::now()),
3991 4021 };
3992 4022 let pin_banner = self
3993 4023 .render_pin_banner(cx)
@@ -36,6 +36,7 @@ mod forward_select;
36pub use forward_select::{
36pub use forward_select::{
37 FORWARD_FOOTER_BAR_TAG, FORWARD_FOOTER_COUNT_TAG, FORWARD_FOOTER_NEXT_TAG, FORWARD_FOOTER_TAG,
37 FORWARD_FOOTER_BAR_TAG, FORWARD_FOOTER_COUNT_TAG, FORWARD_FOOTER_NEXT_TAG, FORWARD_FOOTER_TAG,
38};
38};
39mod appear_tracker;
39mod jump;
40mod jump;
40mod notes_space;
41mod notes_space;
41mod pins;
42mod pins;
@@ -285,6 +286,7 @@ pub struct ConversationView {
285 agent_runs: Rc<RefCell<HashMap<String, AgentRun>>>,
286 agent_runs: Rc<RefCell<HashMap<String, AgentRun>>>,
286 reaction_proposals: Rc<RefCell<HashMap<String, ReactionProposal>>>,
287 reaction_proposals: Rc<RefCell<HashMap<String, ReactionProposal>>>,
287 receipt_fade: receipt_fade::ReceiptFade,
288 receipt_fade: receipt_fade::ReceiptFade,
289 appear_tracker: appear_tracker::AppearTracker,
288 find_panel: Option<Entity<FindPanel>>,
290 find_panel: Option<Entity<FindPanel>>,
289 find_subscription: Option<Subscription>,
291 find_subscription: Option<Subscription>,
290 group_name: GroupNameSlot,
292 group_name: GroupNameSlot,
@@ -638,6 +640,7 @@ impl ConversationView {
638 agent_runs: Rc::new(RefCell::new(HashMap::new())),
640 agent_runs: Rc::new(RefCell::new(HashMap::new())),
639 reaction_proposals: Rc::new(RefCell::new(HashMap::new())),
641 reaction_proposals: Rc::new(RefCell::new(HashMap::new())),
640 receipt_fade: receipt_fade::ReceiptFade::default(),
642 receipt_fade: receipt_fade::ReceiptFade::default(),
643 appear_tracker: appear_tracker::AppearTracker::default(),
641 find_panel: None,
644 find_panel: None,
642 find_subscription: None,
645 find_subscription: None,
643 _subscriptions: subscriptions,
646 _subscriptions: subscriptions,
@@ -1781,6 +1784,8 @@ impl ConversationView {
1781 let placeholders = records.is_empty()
1784 let placeholders = records.is_empty()
1782 && (loading_post || current.sync.messages && thread_root.is_none());
1785 && (loading_post || current.sync.messages && thread_root.is_none());
1783 let in_feed = current.mode == ViewMode::ThreadList;
1786 let in_feed = current.mode == ViewMode::ThreadList;
1787 let appear_scope = format!("{conversation_id}/{:?}", current.mode);
1788 let appear_live = !placeholders && !in_feed && window.is_none() && !current.sync.messages;
1784 let mut rows = match &current.mode {
1789 let mut rows = match &current.mode {
1785 _ if placeholders => {
1790 _ if placeholders => {
1786 current.has_older = false;
1791 current.has_older = false;
@@ -1856,6 +1861,7 @@ impl ConversationView {
1856 }
1861 }
1857 }
1862 }
1858 assign_series(&mut rows);
1863 assign_series(&mut rows);
1864 self.track_appearing(&rows, &appear_scope, appear_live, reset);
1859 self.apply_rows(rows, reset, cx);
1865 self.apply_rows(rows, reset, cx);
1860 if self.is_detached() {
1866 if self.is_detached() {
1861 self.scroller
1867 self.scroller
@@ -2245,6 +2251,29 @@ impl ConversationView {
2245 .apply(&conversation_id, settled, receipts, Instant::now())
2251 .apply(&conversation_id, settled, receipts, Instant::now())
2246 }
2252 }
2247
2253
2254 fn track_appearing(&mut self, rows: &[Row], scope: &str, live: bool, reset: bool) {
2255 let sending: HashSet<&str> = self.pending.iter().map(|row| row.key.as_str()).collect();
2256 let candidates: Vec<appear_tracker::Candidate> = rows
2257 .iter()
2258 .filter_map(|row| match row {
2259 Row::Message(message) => Some(appear_tracker::Candidate {
2260 key: &message.key,
2261 own: message.own,
2262 origin: if sending.contains(message.key.as_str()) {
2263 appear_tracker::Origin::Sending
2264 } else if message.key.starts_with(SCHEDULED_KEY_PREFIX) {
2265 appear_tracker::Origin::Scheduled
2266 } else {
2267 appear_tracker::Origin::Server
2268 },
2269 }),
2270 _ => None,
2271 })
2272 .collect();
2273 self.appear_tracker
2274 .apply(scope, live, reset, &candidates, Instant::now());
2275 }
2276
2248 fn schedule_fade_end(&mut self, cx: &mut Context<Self>) {
2277 fn schedule_fade_end(&mut self, cx: &mut Context<Self>) {
2249 cx.spawn(async move |this, cx| {
2278 cx.spawn(async move |this, cx| {
2250 cx.background_executor()
2279 cx.background_executor()
@@ -3988,6 +4017,7 @@ impl Render for ConversationView {
3988 agent_runs: self.agent_runs.clone(),
4017 agent_runs: self.agent_runs.clone(),
3989 reaction_proposals: self.reaction_proposals.clone(),
4018 reaction_proposals: self.reaction_proposals.clone(),
3990 forward_selection: forward_selection.clone(),
4019 forward_selection: forward_selection.clone(),
4020 appearing: self.appear_tracker.entering(Instant::now()),
3991 };
4021 };
3992 let pin_banner = self
4022 let pin_banner = self
3993 .render_pin_banner(cx)
4023 .render_pin_banner(cx)
crates/app/src/views/conversation/appear_tracker.rs added +265 −0
@@ -0,0 +1,265 @@
1 use std::collections::{HashMap, HashSet};
2 use std::time::{Duration, Instant};
3
4 use crate::views::message_appear::AppearKind;
5
6 const REPLACEMENT_GRACE: Duration = Duration::from_secs(15);
7 const SETTLE_MARGIN: Duration = Duration::from_millis(150);
8
9 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
10 pub(super) enum Origin {
11 Server,
12 Sending,
13 Scheduled,
14 }
15
16 pub(super) struct Candidate<'a> {
17 pub key: &'a str,
18 pub own: bool,
19 pub origin: Origin,
20 }
21
22 /// Marks messages that arrive while the chat is open, never the first paint, a sync, older history or the server copy of a sent message.
23 #[derive(Default)]
24 pub(super) struct AppearTracker {
25 scope: String,
26 was_live: bool,
27 known: HashSet<String>,
28 local: HashSet<String>,
29 unmatched_sending: HashSet<String>,
30 claimed: HashSet<String>,
31 released_credits: Vec<Instant>,
32 entering: HashMap<String, (AppearKind, Instant)>,
33 }
34
35 impl AppearTracker {
36 pub(super) fn apply(
37 &mut self,
38 scope: &str,
39 live: bool,
40 reset: bool,
41 rows: &[Candidate],
42 now: Instant,
43 ) {
44 let animate = live && !reset && self.was_live && self.scope == scope;
45 if self.scope != scope {
46 self.scope = scope.to_owned();
47 self.local.clear();
48 self.unmatched_sending.clear();
49 self.claimed.clear();
50 self.released_credits.clear();
51 self.entering.clear();
52 }
53 self.was_live = live;
54 self.release_departed(rows, now);
55 if animate {
56 self.mark_new(rows, now);
57 }
58 self.known = rows.iter().map(|row| row.key.to_owned()).collect();
59 self.local = rows
60 .iter()
61 .filter(|row| row.origin != Origin::Server)
62 .map(|row| row.key.to_owned())
63 .collect();
64 self.entering.retain(|_, (kind, since)| {
65 now.duration_since(*since) < kind.duration() + SETTLE_MARGIN
66 });
67 }
68
69 pub(super) fn entering(&self, now: Instant) -> HashMap<String, AppearKind> {
70 self.entering
71 .iter()
72 .filter(|(_, (kind, since))| now.duration_since(*since) < kind.duration() + SETTLE_MARGIN)
73 .map(|(key, (kind, _))| (key.clone(), *kind))
74 .collect()
75 }
76
77 fn release_departed(&mut self, rows: &[Candidate], now: Instant) {
78 let present: HashSet<&str> = rows.iter().map(|row| row.key).collect();
79 let departed: Vec<String> = self
80 .local
81 .iter()
82 .filter(|key| !present.contains(key.as_str()))
83 .cloned()
84 .collect();
85 for key in departed {
86 self.unmatched_sending.remove(&key);
87 if !self.claimed.remove(&key) {
88 self.released_credits.push(now);
89 }
90 }
91 self.released_credits
92 .retain(|since| now.duration_since(*since) < REPLACEMENT_GRACE);
93 }
94
95 fn mark_new(&mut self, rows: &[Candidate], now: Instant) {
96 let newest_known_server = rows
97 .iter()
98 .rposition(|row| row.origin == Origin::Server && self.known.contains(row.key));
99 let first_new = newest_known_server.map_or(0, |index| index + 1);
100 for row in &rows[first_new..] {
101 if self.known.contains(row.key) {
102 continue;
103 }
104 let kind = if row.own {
105 AppearKind::Own
106 } else {
107 AppearKind::Incoming
108 };
109 match row.origin {
110 Origin::Sending => {
111 self.unmatched_sending.insert(row.key.to_owned());
112 }
113 Origin::Scheduled => {}
114 Origin::Server if row.own && self.take_replacement_credit() => continue,
115 Origin::Server => {}
116 }
117 self.entering.insert(row.key.to_owned(), (kind, now));
118 }
119 }
120
121 fn take_replacement_credit(&mut self) -> bool {
122 if self.released_credits.pop().is_some() {
123 return true;
124 }
125 let Some(key) = self.unmatched_sending.iter().next().cloned() else {
126 return false;
127 };
128 self.unmatched_sending.remove(&key);
129 self.claimed.insert(key);
130 true
131 }
132 }
133
134 #[cfg(test)]
135 mod tests {
136 use super::*;
137
138 fn server<'a>(key: &'a str, own: bool) -> Candidate<'a> {
139 Candidate {
140 key,
141 own,
142 origin: Origin::Server,
143 }
144 }
145
146 fn sending(key: &str) -> Candidate<'_> {
147 Candidate {
148 key,
149 own: true,
150 origin: Origin::Sending,
151 }
152 }
153
154 fn keys(tracker: &AppearTracker, now: Instant) -> Vec<(String, AppearKind)> {
155 let mut entering: Vec<_> = tracker.entering(now).into_iter().collect();
156 entering.sort_by(|left, right| left.0.cmp(&right.0));
157 entering
158 }
159
160 fn opened(rows: &[Candidate], now: Instant) -> AppearTracker {
161 let mut tracker = AppearTracker::default();
162 tracker.apply("chat", true, false, rows, now);
163 tracker
164 }
165
166 #[test]
167 fn opening_a_chat_animates_nothing() {
168 let now = Instant::now();
169 let tracker = opened(&[server("a", false), server("b", true)], now);
170 assert!(keys(&tracker, now).is_empty());
171 }
172
173 #[test]
174 fn a_live_message_from_others_slides_in_once() {
175 let now = Instant::now();
176 let mut tracker = opened(&[server("a", false)], now);
177 tracker.apply("chat", true, false, &[server("a", false), server("b", false)], now);
178 assert_eq!(keys(&tracker, now), vec![("b".to_owned(), AppearKind::Incoming)]);
179 let later = now + Duration::from_secs(1);
180 tracker.apply("chat", true, false, &[server("a", false), server("b", false)], later);
181 assert!(keys(&tracker, later).is_empty());
182 }
183
184 #[test]
185 fn messages_fetched_while_syncing_or_right_after_do_not_animate() {
186 let now = Instant::now();
187 let mut tracker = AppearTracker::default();
188 tracker.apply("chat", true, false, &[server("a", false)], now);
189 tracker.apply("chat", false, false, &[server("a", false), server("b", false)], now);
190 tracker.apply("chat", true, false, &[server("a", false), server("b", false), server("c", false)], now);
191 assert!(keys(&tracker, now).is_empty());
192 }
193
194 #[test]
195 fn older_history_above_the_known_messages_does_not_animate() {
196 let now = Instant::now();
197 let mut tracker = opened(&[server("c", false), server("d", false)], now);
198 tracker.apply(
199 "chat",
200 true,
201 false,
202 &[server("a", false), server("b", false), server("c", false), server("d", false)],
203 now,
204 );
205 assert!(keys(&tracker, now).is_empty());
206 }
207
208 #[test]
209 fn switching_chats_drops_the_baseline() {
210 let now = Instant::now();
211 let mut tracker = opened(&[server("a", false)], now);
212 tracker.apply("other", true, false, &[server("x", false), server("y", false)], now);
213 assert!(keys(&tracker, now).is_empty());
214 }
215
216 #[test]
217 fn a_sent_message_animates_as_pending_and_not_again_as_server_copy() {
218 let now = Instant::now();
219 let mut tracker = opened(&[server("a", false)], now);
220 tracker.apply("chat", true, false, &[server("a", false), sending("local-1")], now);
221 assert_eq!(keys(&tracker, now), vec![("local-1".to_owned(), AppearKind::Own)]);
222 let later = now + Duration::from_secs(1);
223 tracker.apply("chat", true, false, &[server("a", false), server("m1", true)], later);
224 assert!(keys(&tracker, later).is_empty());
225 }
226
227 #[test]
228 fn a_server_copy_arriving_before_the_pending_row_leaves_does_not_animate() {
229 let now = Instant::now();
230 let mut tracker = opened(&[server("a", false)], now);
231 tracker.apply("chat", true, false, &[server("a", false), sending("local-1")], now);
232 let later = now + Duration::from_secs(1);
233 tracker.apply("chat", true, false, &[server("a", false), server("m1", true), sending("local-1")], later);
234 tracker.apply("chat", true, false, &[server("a", false), server("m1", true)], later);
235 tracker.apply("chat", true, false, &[server("a", false), server("m1", true), server("m2", true)], later);
236 assert_eq!(keys(&tracker, later), vec![("m2".to_owned(), AppearKind::Own)]);
237 }
238
239 #[test]
240 fn an_incoming_message_below_a_pending_row_still_animates() {
241 let now = Instant::now();
242 let mut tracker = opened(&[server("a", false), sending("local-1")], now);
243 tracker.apply("chat", true, false, &[server("a", false), server("b", false), sending("local-1")], now);
244 assert_eq!(keys(&tracker, now), vec![("b".to_owned(), AppearKind::Incoming)]);
245 }
246
247 #[test]
248 fn a_reset_rebuild_never_animates() {
249 let now = Instant::now();
250 let mut tracker = opened(&[server("a", false)], now);
251 tracker.apply("chat", true, true, &[server("a", false), server("b", false)], now);
252 assert!(keys(&tracker, now).is_empty());
253 tracker.apply("chat", true, false, &[server("a", false), server("b", false), server("c", false)], now);
254 assert_eq!(keys(&tracker, now), vec![("c".to_owned(), AppearKind::Incoming)]);
255 }
256
257 #[test]
258 fn edits_and_reactions_keep_the_key_and_do_not_animate() {
259 let now = Instant::now();
260 let rows = [server("a", false), server("b", true)];
261 let mut tracker = opened(&rows, now);
262 tracker.apply("chat", true, false, &rows, now);
263 assert!(keys(&tracker, now).is_empty());
264 }
265 }
@@ -0,0 +1,265 @@
1use std::collections::{HashMap, HashSet};
2use std::time::{Duration, Instant};
3
4use crate::views::message_appear::AppearKind;
5
6const REPLACEMENT_GRACE: Duration = Duration::from_secs(15);
7const SETTLE_MARGIN: Duration = Duration::from_millis(150);
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub(super) enum Origin {
11 Server,
12 Sending,
13 Scheduled,
14}
15
16pub(super) struct Candidate<'a> {
17 pub key: &'a str,
18 pub own: bool,
19 pub origin: Origin,
20}
21
22/// Marks messages that arrive while the chat is open, never the first paint, a sync, older history or the server copy of a sent message.
23#[derive(Default)]
24pub(super) struct AppearTracker {
25 scope: String,
26 was_live: bool,
27 known: HashSet<String>,
28 local: HashSet<String>,
29 unmatched_sending: HashSet<String>,
30 claimed: HashSet<String>,
31 released_credits: Vec<Instant>,
32 entering: HashMap<String, (AppearKind, Instant)>,
33}
34
35impl AppearTracker {
36 pub(super) fn apply(
37 &mut self,
38 scope: &str,
39 live: bool,
40 reset: bool,
41 rows: &[Candidate],
42 now: Instant,
43 ) {
44 let animate = live && !reset && self.was_live && self.scope == scope;
45 if self.scope != scope {
46 self.scope = scope.to_owned();
47 self.local.clear();
48 self.unmatched_sending.clear();
49 self.claimed.clear();
50 self.released_credits.clear();
51 self.entering.clear();
52 }
53 self.was_live = live;
54 self.release_departed(rows, now);
55 if animate {
56 self.mark_new(rows, now);
57 }
58 self.known = rows.iter().map(|row| row.key.to_owned()).collect();
59 self.local = rows
60 .iter()
61 .filter(|row| row.origin != Origin::Server)
62 .map(|row| row.key.to_owned())
63 .collect();
64 self.entering.retain(|_, (kind, since)| {
65 now.duration_since(*since) < kind.duration() + SETTLE_MARGIN
66 });
67 }
68
69 pub(super) fn entering(&self, now: Instant) -> HashMap<String, AppearKind> {
70 self.entering
71 .iter()
72 .filter(|(_, (kind, since))| now.duration_since(*since) < kind.duration() + SETTLE_MARGIN)
73 .map(|(key, (kind, _))| (key.clone(), *kind))
74 .collect()
75 }
76
77 fn release_departed(&mut self, rows: &[Candidate], now: Instant) {
78 let present: HashSet<&str> = rows.iter().map(|row| row.key).collect();
79 let departed: Vec<String> = self
80 .local
81 .iter()
82 .filter(|key| !present.contains(key.as_str()))
83 .cloned()
84 .collect();
85 for key in departed {
86 self.unmatched_sending.remove(&key);
87 if !self.claimed.remove(&key) {
88 self.released_credits.push(now);
89 }
90 }
91 self.released_credits
92 .retain(|since| now.duration_since(*since) < REPLACEMENT_GRACE);
93 }
94
95 fn mark_new(&mut self, rows: &[Candidate], now: Instant) {
96 let newest_known_server = rows
97 .iter()
98 .rposition(|row| row.origin == Origin::Server && self.known.contains(row.key));
99 let first_new = newest_known_server.map_or(0, |index| index + 1);
100 for row in &rows[first_new..] {
101 if self.known.contains(row.key) {
102 continue;
103 }
104 let kind = if row.own {
105 AppearKind::Own
106 } else {
107 AppearKind::Incoming
108 };
109 match row.origin {
110 Origin::Sending => {
111 self.unmatched_sending.insert(row.key.to_owned());
112 }
113 Origin::Scheduled => {}
114 Origin::Server if row.own && self.take_replacement_credit() => continue,
115 Origin::Server => {}
116 }
117 self.entering.insert(row.key.to_owned(), (kind, now));
118 }
119 }
120
121 fn take_replacement_credit(&mut self) -> bool {
122 if self.released_credits.pop().is_some() {
123 return true;
124 }
125 let Some(key) = self.unmatched_sending.iter().next().cloned() else {
126 return false;
127 };
128 self.unmatched_sending.remove(&key);
129 self.claimed.insert(key);
130 true
131 }
132}
133
134#[cfg(test)]
135mod tests {
136 use super::*;
137
138 fn server<'a>(key: &'a str, own: bool) -> Candidate<'a> {
139 Candidate {
140 key,
141 own,
142 origin: Origin::Server,
143 }
144 }
145
146 fn sending(key: &str) -> Candidate<'_> {
147 Candidate {
148 key,
149 own: true,
150 origin: Origin::Sending,
151 }
152 }
153
154 fn keys(tracker: &AppearTracker, now: Instant) -> Vec<(String, AppearKind)> {
155 let mut entering: Vec<_> = tracker.entering(now).into_iter().collect();
156 entering.sort_by(|left, right| left.0.cmp(&right.0));
157 entering
158 }
159
160 fn opened(rows: &[Candidate], now: Instant) -> AppearTracker {
161 let mut tracker = AppearTracker::default();
162 tracker.apply("chat", true, false, rows, now);
163 tracker
164 }
165
166 #[test]
167 fn opening_a_chat_animates_nothing() {
168 let now = Instant::now();
169 let tracker = opened(&[server("a", false), server("b", true)], now);
170 assert!(keys(&tracker, now).is_empty());
171 }
172
173 #[test]
174 fn a_live_message_from_others_slides_in_once() {
175 let now = Instant::now();
176 let mut tracker = opened(&[server("a", false)], now);
177 tracker.apply("chat", true, false, &[server("a", false), server("b", false)], now);
178 assert_eq!(keys(&tracker, now), vec![("b".to_owned(), AppearKind::Incoming)]);
179 let later = now + Duration::from_secs(1);
180 tracker.apply("chat", true, false, &[server("a", false), server("b", false)], later);
181 assert!(keys(&tracker, later).is_empty());
182 }
183
184 #[test]
185 fn messages_fetched_while_syncing_or_right_after_do_not_animate() {
186 let now = Instant::now();
187 let mut tracker = AppearTracker::default();
188 tracker.apply("chat", true, false, &[server("a", false)], now);
189 tracker.apply("chat", false, false, &[server("a", false), server("b", false)], now);
190 tracker.apply("chat", true, false, &[server("a", false), server("b", false), server("c", false)], now);
191 assert!(keys(&tracker, now).is_empty());
192 }
193
194 #[test]
195 fn older_history_above_the_known_messages_does_not_animate() {
196 let now = Instant::now();
197 let mut tracker = opened(&[server("c", false), server("d", false)], now);
198 tracker.apply(
199 "chat",
200 true,
201 false,
202 &[server("a", false), server("b", false), server("c", false), server("d", false)],
203 now,
204 );
205 assert!(keys(&tracker, now).is_empty());
206 }
207
208 #[test]
209 fn switching_chats_drops_the_baseline() {
210 let now = Instant::now();
211 let mut tracker = opened(&[server("a", false)], now);
212 tracker.apply("other", true, false, &[server("x", false), server("y", false)], now);
213 assert!(keys(&tracker, now).is_empty());
214 }
215
216 #[test]
217 fn a_sent_message_animates_as_pending_and_not_again_as_server_copy() {
218 let now = Instant::now();
219 let mut tracker = opened(&[server("a", false)], now);
220 tracker.apply("chat", true, false, &[server("a", false), sending("local-1")], now);
221 assert_eq!(keys(&tracker, now), vec![("local-1".to_owned(), AppearKind::Own)]);
222 let later = now + Duration::from_secs(1);
223 tracker.apply("chat", true, false, &[server("a", false), server("m1", true)], later);
224 assert!(keys(&tracker, later).is_empty());
225 }
226
227 #[test]
228 fn a_server_copy_arriving_before_the_pending_row_leaves_does_not_animate() {
229 let now = Instant::now();
230 let mut tracker = opened(&[server("a", false)], now);
231 tracker.apply("chat", true, false, &[server("a", false), sending("local-1")], now);
232 let later = now + Duration::from_secs(1);
233 tracker.apply("chat", true, false, &[server("a", false), server("m1", true), sending("local-1")], later);
234 tracker.apply("chat", true, false, &[server("a", false), server("m1", true)], later);
235 tracker.apply("chat", true, false, &[server("a", false), server("m1", true), server("m2", true)], later);
236 assert_eq!(keys(&tracker, later), vec![("m2".to_owned(), AppearKind::Own)]);
237 }
238
239 #[test]
240 fn an_incoming_message_below_a_pending_row_still_animates() {
241 let now = Instant::now();
242 let mut tracker = opened(&[server("a", false), sending("local-1")], now);
243 tracker.apply("chat", true, false, &[server("a", false), server("b", false), sending("local-1")], now);
244 assert_eq!(keys(&tracker, now), vec![("b".to_owned(), AppearKind::Incoming)]);
245 }
246
247 #[test]
248 fn a_reset_rebuild_never_animates() {
249 let now = Instant::now();
250 let mut tracker = opened(&[server("a", false)], now);
251 tracker.apply("chat", true, true, &[server("a", false), server("b", false)], now);
252 assert!(keys(&tracker, now).is_empty());
253 tracker.apply("chat", true, false, &[server("a", false), server("b", false), server("c", false)], now);
254 assert_eq!(keys(&tracker, now), vec![("c".to_owned(), AppearKind::Incoming)]);
255 }
256
257 #[test]
258 fn edits_and_reactions_keep_the_key_and_do_not_animate() {
259 let now = Instant::now();
260 let rows = [server("a", false), server("b", true)];
261 let mut tracker = opened(&rows, now);
262 tracker.apply("chat", true, false, &rows, now);
263 assert!(keys(&tracker, now).is_empty());
264 }
265}
crates/app/src/views/conversation/render_env.rs +3 −0
@@ -21,6 +21,7 @@ use crate::views::call_card::CallActions;
21 21 use crate::views::call_notice::RecapActions;
22 22 use crate::views::composer::Composer;
23 23 use crate::views::message_context_menu::message_context_menu;
24 use crate::views::message_appear::AppearKind;
24 25 use crate::views::message_row::{DeliveryActions, RowActions, RowSelect, SharedRowAction};
25 26 use crate::views::post_card::{PostAction, PostActions};
26 27 use crate::views::reaction_proposal_pill::ReactionPill;
@@ -37,6 +38,7 @@ pub(super) struct RenderEnv {
37 38 pub agent_runs: Rc<RefCell<HashMap<String, AgentRun>>>,
38 39 pub reaction_proposals: Rc<RefCell<HashMap<String, ReactionProposal>>>,
39 40 pub forward_selection: Option<SelectionSnapshot>,
41 pub appearing: HashMap<String, AppearKind>,
40 42 }
41 43
42 44 fn boxed(action: impl Fn(&mut App) + 'static) -> Box<dyn Fn(&mut App)> {
@@ -377,6 +379,7 @@ impl RenderEnv {
377 379 undo_delete: self.undo_delete_action(message, is_real, view, cx),
378 380 compact: self.app.read(cx).account_chat_settings().message_density
379 381 == teams_core::MessageDensity::Compact,
382 appear: self.appearing.get(&message.key).copied(),
380 383 }
381 384 }
382 385
@@ -21,6 +21,7 @@ use crate::views::call_card::CallActions;
21use crate::views::call_notice::RecapActions;
21use crate::views::call_notice::RecapActions;
22use crate::views::composer::Composer;
22use crate::views::composer::Composer;
23use crate::views::message_context_menu::message_context_menu;
23use crate::views::message_context_menu::message_context_menu;
24use crate::views::message_appear::AppearKind;
24use crate::views::message_row::{DeliveryActions, RowActions, RowSelect, SharedRowAction};
25use crate::views::message_row::{DeliveryActions, RowActions, RowSelect, SharedRowAction};
25use crate::views::post_card::{PostAction, PostActions};
26use crate::views::post_card::{PostAction, PostActions};
26use crate::views::reaction_proposal_pill::ReactionPill;
27use crate::views::reaction_proposal_pill::ReactionPill;
@@ -37,6 +38,7 @@ pub(super) struct RenderEnv {
37 pub agent_runs: Rc<RefCell<HashMap<String, AgentRun>>>,
38 pub agent_runs: Rc<RefCell<HashMap<String, AgentRun>>>,
38 pub reaction_proposals: Rc<RefCell<HashMap<String, ReactionProposal>>>,
39 pub reaction_proposals: Rc<RefCell<HashMap<String, ReactionProposal>>>,
39 pub forward_selection: Option<SelectionSnapshot>,
40 pub forward_selection: Option<SelectionSnapshot>,
41 pub appearing: HashMap<String, AppearKind>,
40}
42}
41
43
42fn boxed(action: impl Fn(&mut App) + 'static) -> Box<dyn Fn(&mut App)> {
44fn boxed(action: impl Fn(&mut App) + 'static) -> Box<dyn Fn(&mut App)> {
@@ -377,6 +379,7 @@ impl RenderEnv {
377 undo_delete: self.undo_delete_action(message, is_real, view, cx),
379 undo_delete: self.undo_delete_action(message, is_real, view, cx),
378 compact: self.app.read(cx).account_chat_settings().message_density
380 compact: self.app.read(cx).account_chat_settings().message_density
379 == teams_core::MessageDensity::Compact,
381 == teams_core::MessageDensity::Compact,
382 appear: self.appearing.get(&message.key).copied(),
380 }
383 }
381 }
384 }
382
385
crates/app/src/views/message_appear.rs added +128 −0
@@ -0,0 +1,128 @@
1 use std::time::Duration;
2
3 use gpui_kit::*;
4
5 pub const OWN_APPEAR: Duration = Duration::from_millis(420);
6 pub const INCOMING_APPEAR: Duration = Duration::from_millis(340);
7
8 const OWN_RISE: f32 = 16.;
9 const OWN_DRIFT: f32 = 8.;
10 const OWN_FADE_SHARE: f32 = 0.35;
11 const INCOMING_SLIDE: f32 = 18.;
12 const INCOMING_RISE: f32 = 4.;
13 const INCOMING_FADE_SHARE: f32 = 0.6;
14 const SPRING_DECAY: f32 = 6.;
15 const SPRING_FREQUENCY: f32 = 8.;
16
17 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
18 pub enum AppearKind {
19 Own,
20 Incoming,
21 }
22
23 impl AppearKind {
24 pub fn duration(self) -> Duration {
25 match self {
26 Self::Own => OWN_APPEAR,
27 Self::Incoming => INCOMING_APPEAR,
28 }
29 }
30 }
31
32 #[derive(Debug, Clone, Copy, PartialEq)]
33 pub struct AppearFrame {
34 pub offset_x: f32,
35 pub offset_y: f32,
36 pub opacity: f32,
37 }
38
39 pub fn appear_frame(kind: AppearKind, progress: f32) -> AppearFrame {
40 let progress = progress.clamp(0., 1.);
41 match kind {
42 AppearKind::Own => {
43 let remaining = 1. - spring_settle(progress);
44 AppearFrame {
45 offset_x: remaining * OWN_DRIFT,
46 offset_y: remaining * OWN_RISE,
47 opacity: ease_out_cubic(progress / OWN_FADE_SHARE),
48 }
49 }
50 AppearKind::Incoming => {
51 let remaining = 1. - ease_out_quint(progress);
52 AppearFrame {
53 offset_x: -remaining * INCOMING_SLIDE,
54 offset_y: remaining * INCOMING_RISE,
55 opacity: ease_out_cubic(progress / INCOMING_FADE_SHARE),
56 }
57 }
58 }
59 }
60
61 pub fn appear(body: Div, message_key: &str, kind: AppearKind) -> AnimationElement<Div> {
62 body.relative().with_animation(
63 SharedString::from(format!("message-appear/{message_key}")),
64 Animation::new(kind.duration()),
65 move |body, progress| {
66 let frame = appear_frame(kind, progress);
67 body.left(px(frame.offset_x))
68 .top(px(frame.offset_y))
69 .opacity(frame.opacity)
70 },
71 )
72 }
73
74 fn spring_settle(progress: f32) -> f32 {
75 if progress >= 1. {
76 return 1.;
77 }
78 let angle = SPRING_FREQUENCY * progress;
79 let ratio = SPRING_DECAY / SPRING_FREQUENCY;
80 1. - (-SPRING_DECAY * progress).exp() * (angle.cos() + ratio * angle.sin())
81 }
82
83 fn ease_out_cubic(progress: f32) -> f32 {
84 1. - (1. - progress.clamp(0., 1.)).powi(3)
85 }
86
87 fn ease_out_quint(progress: f32) -> f32 {
88 1. - (1. - progress.clamp(0., 1.)).powi(5)
89 }
90
91 #[cfg(test)]
92 mod tests {
93 use super::{AppearFrame, AppearKind, OWN_RISE, appear_frame};
94
95 #[test]
96 fn both_kinds_start_hidden_and_settle_on_the_resting_place() {
97 for kind in [AppearKind::Own, AppearKind::Incoming] {
98 let start = appear_frame(kind, 0.);
99 assert_eq!(start.opacity, 0.);
100 let end = appear_frame(kind, 1.);
101 assert_eq!(
102 end,
103 AppearFrame {
104 offset_x: 0.,
105 offset_y: 0.,
106 opacity: 1.
107 }
108 );
109 }
110 }
111
112 #[test]
113 fn own_messages_rise_from_the_bottom_right_and_overshoot_slightly() {
114 let start = appear_frame(AppearKind::Own, 0.);
115 assert!(start.offset_x > 0. && start.offset_y > 0.);
116 let deepest = (1..100)
117 .map(|step| appear_frame(AppearKind::Own, step as f32 / 100.).offset_y)
118 .fold(f32::MAX, f32::min);
119 assert!(deepest < 0. && deepest > -OWN_RISE * 0.15);
120 }
121
122 #[test]
123 fn incoming_messages_slide_in_from_the_left_without_overshoot() {
124 let start = appear_frame(AppearKind::Incoming, 0.);
125 assert!(start.offset_x < 0.);
126 assert!((1..100).all(|step| appear_frame(AppearKind::Incoming, step as f32 / 100.).offset_x <= 0.));
127 }
128 }
@@ -0,0 +1,128 @@
1use std::time::Duration;
2
3use gpui_kit::*;
4
5pub const OWN_APPEAR: Duration = Duration::from_millis(420);
6pub const INCOMING_APPEAR: Duration = Duration::from_millis(340);
7
8const OWN_RISE: f32 = 16.;
9const OWN_DRIFT: f32 = 8.;
10const OWN_FADE_SHARE: f32 = 0.35;
11const INCOMING_SLIDE: f32 = 18.;
12const INCOMING_RISE: f32 = 4.;
13const INCOMING_FADE_SHARE: f32 = 0.6;
14const SPRING_DECAY: f32 = 6.;
15const SPRING_FREQUENCY: f32 = 8.;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum AppearKind {
19 Own,
20 Incoming,
21}
22
23impl AppearKind {
24 pub fn duration(self) -> Duration {
25 match self {
26 Self::Own => OWN_APPEAR,
27 Self::Incoming => INCOMING_APPEAR,
28 }
29 }
30}
31
32#[derive(Debug, Clone, Copy, PartialEq)]
33pub struct AppearFrame {
34 pub offset_x: f32,
35 pub offset_y: f32,
36 pub opacity: f32,
37}
38
39pub fn appear_frame(kind: AppearKind, progress: f32) -> AppearFrame {
40 let progress = progress.clamp(0., 1.);
41 match kind {
42 AppearKind::Own => {
43 let remaining = 1. - spring_settle(progress);
44 AppearFrame {
45 offset_x: remaining * OWN_DRIFT,
46 offset_y: remaining * OWN_RISE,
47 opacity: ease_out_cubic(progress / OWN_FADE_SHARE),
48 }
49 }
50 AppearKind::Incoming => {
51 let remaining = 1. - ease_out_quint(progress);
52 AppearFrame {
53 offset_x: -remaining * INCOMING_SLIDE,
54 offset_y: remaining * INCOMING_RISE,
55 opacity: ease_out_cubic(progress / INCOMING_FADE_SHARE),
56 }
57 }
58 }
59}
60
61pub fn appear(body: Div, message_key: &str, kind: AppearKind) -> AnimationElement<Div> {
62 body.relative().with_animation(
63 SharedString::from(format!("message-appear/{message_key}")),
64 Animation::new(kind.duration()),
65 move |body, progress| {
66 let frame = appear_frame(kind, progress);
67 body.left(px(frame.offset_x))
68 .top(px(frame.offset_y))
69 .opacity(frame.opacity)
70 },
71 )
72}
73
74fn spring_settle(progress: f32) -> f32 {
75 if progress >= 1. {
76 return 1.;
77 }
78 let angle = SPRING_FREQUENCY * progress;
79 let ratio = SPRING_DECAY / SPRING_FREQUENCY;
80 1. - (-SPRING_DECAY * progress).exp() * (angle.cos() + ratio * angle.sin())
81}
82
83fn ease_out_cubic(progress: f32) -> f32 {
84 1. - (1. - progress.clamp(0., 1.)).powi(3)
85}
86
87fn ease_out_quint(progress: f32) -> f32 {
88 1. - (1. - progress.clamp(0., 1.)).powi(5)
89}
90
91#[cfg(test)]
92mod tests {
93 use super::{AppearFrame, AppearKind, OWN_RISE, appear_frame};
94
95 #[test]
96 fn both_kinds_start_hidden_and_settle_on_the_resting_place() {
97 for kind in [AppearKind::Own, AppearKind::Incoming] {
98 let start = appear_frame(kind, 0.);
99 assert_eq!(start.opacity, 0.);
100 let end = appear_frame(kind, 1.);
101 assert_eq!(
102 end,
103 AppearFrame {
104 offset_x: 0.,
105 offset_y: 0.,
106 opacity: 1.
107 }
108 );
109 }
110 }
111
112 #[test]
113 fn own_messages_rise_from_the_bottom_right_and_overshoot_slightly() {
114 let start = appear_frame(AppearKind::Own, 0.);
115 assert!(start.offset_x > 0. && start.offset_y > 0.);
116 let deepest = (1..100)
117 .map(|step| appear_frame(AppearKind::Own, step as f32 / 100.).offset_y)
118 .fold(f32::MAX, f32::min);
119 assert!(deepest < 0. && deepest > -OWN_RISE * 0.15);
120 }
121
122 #[test]
123 fn incoming_messages_slide_in_from_the_left_without_overshoot() {
124 let start = appear_frame(AppearKind::Incoming, 0.);
125 assert!(start.offset_x < 0.);
126 assert!((1..100).all(|step| appear_frame(AppearKind::Incoming, step as f32 / 100.).offset_x <= 0.));
127 }
128}
crates/app/src/views/message_row.rs +8 −1
@@ -11,6 +11,7 @@ use super::avatar::{bot_avatar, person_avatar};
11 11 use super::call_card::{CallActions, call_card_row};
12 12 use super::jumbo_emoji::jumbo_row;
13 13 use super::link_preview::{link_preview_card, meeting_join_chip};
14 use super::message_appear::{AppearKind, appear};
14 15 use super::message_actions::{MessageMenu, ReactionAdder, message_toolbar};
15 16 use super::profile_card::opens_profile;
16 17 use super::reaction_picker::PickHandler;
@@ -127,6 +128,7 @@ pub struct RowActions {
127 128 pub reaction_proposal: Option<ReactionPill>,
128 129 pub undo_delete: SharedRowAction,
129 130 pub compact: bool,
131 pub appear: Option<AppearKind>,
130 132 }
131 133
132 134 pub fn row_spacing(has_previous: bool, compact: bool) -> f32 {
@@ -816,6 +818,7 @@ pub fn render_message_row(
816 818 reaction_proposal,
817 819 undo_delete,
818 820 compact,
821 appear: appear_kind,
819 822 } = actions;
820 823 let extras = BubbleExtras {
821 824 hover: hovered,
@@ -848,10 +851,14 @@ pub fn render_message_row(
848 851 } else {
849 852 others_row(row, index, directory, bot.as_ref(), delivery, extras, cx)
850 853 };
854 let body = match appear_kind.filter(|_| !cx.reduce_motion()) {
855 Some(kind) => appear(body, &row.key, kind).into_any_element(),
856 None => body.into_any_element(),
857 };
851 858 let checked = select.as_ref().is_some_and(|select| select.checked);
852 859 let body = match select {
853 860 Some(select) => selectable_body(select, index, body).into_any_element(),
854 None => body.into_any_element(),
861 None => body,
855 862 };
856 863 container
857 864 .child(
@@ -11,6 +11,7 @@ use super::avatar::{bot_avatar, person_avatar};
11use super::call_card::{CallActions, call_card_row};
11use super::call_card::{CallActions, call_card_row};
12use super::jumbo_emoji::jumbo_row;
12use super::jumbo_emoji::jumbo_row;
13use super::link_preview::{link_preview_card, meeting_join_chip};
13use super::link_preview::{link_preview_card, meeting_join_chip};
14use super::message_appear::{AppearKind, appear};
14use super::message_actions::{MessageMenu, ReactionAdder, message_toolbar};
15use super::message_actions::{MessageMenu, ReactionAdder, message_toolbar};
15use super::profile_card::opens_profile;
16use super::profile_card::opens_profile;
16use super::reaction_picker::PickHandler;
17use super::reaction_picker::PickHandler;
@@ -127,6 +128,7 @@ pub struct RowActions {
127 pub reaction_proposal: Option<ReactionPill>,
128 pub reaction_proposal: Option<ReactionPill>,
128 pub undo_delete: SharedRowAction,
129 pub undo_delete: SharedRowAction,
129 pub compact: bool,
130 pub compact: bool,
131 pub appear: Option<AppearKind>,
130}
132}
131
133
132pub fn row_spacing(has_previous: bool, compact: bool) -> f32 {
134pub fn row_spacing(has_previous: bool, compact: bool) -> f32 {
@@ -816,6 +818,7 @@ pub fn render_message_row(
816 reaction_proposal,
818 reaction_proposal,
817 undo_delete,
819 undo_delete,
818 compact,
820 compact,
821 appear: appear_kind,
819 } = actions;
822 } = actions;
820 let extras = BubbleExtras {
823 let extras = BubbleExtras {
821 hover: hovered,
824 hover: hovered,
@@ -848,10 +851,14 @@ pub fn render_message_row(
848 } else {
851 } else {
849 others_row(row, index, directory, bot.as_ref(), delivery, extras, cx)
852 others_row(row, index, directory, bot.as_ref(), delivery, extras, cx)
850 };
853 };
854 let body = match appear_kind.filter(|_| !cx.reduce_motion()) {
855 Some(kind) => appear(body, &row.key, kind).into_any_element(),
856 None => body.into_any_element(),
857 };
851 let checked = select.as_ref().is_some_and(|select| select.checked);
858 let checked = select.as_ref().is_some_and(|select| select.checked);
852 let body = match select {
859 let body = match select {
853 Some(select) => selectable_body(select, index, body).into_any_element(),
860 Some(select) => selectable_body(select, index, body).into_any_element(),
854 None => body.into_any_element(),
861 None => body,
855 };
862 };
856 container
863 container
857 .child(
864 .child(
crates/app/src/views/mod.rs +1 −0
@@ -62,6 +62,7 @@ pub mod join_dialog;
62 62 pub mod jumbo_emoji;
63 63 pub mod link_preview;
64 64 pub mod message_actions;
65 pub mod message_appear;
65 66 pub mod message_context_menu;
66 67 pub mod message_row;
67 68 pub mod new_chat;
@@ -62,6 +62,7 @@ pub mod join_dialog;
62pub mod jumbo_emoji;
62pub mod jumbo_emoji;
63pub mod link_preview;
63pub mod link_preview;
64pub mod message_actions;
64pub mod message_actions;
65pub mod message_appear;
65pub mod message_context_menu;
66pub mod message_context_menu;
66pub mod message_row;
67pub mod message_row;
67pub mod new_chat;
68pub mod new_chat;