| 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 | } |