| 1 | use std::time::Duration; |
| 2 | |
| 3 | use gpui_kit::*; |
| 4 | |
| 5 | pub const OWN_APPEAR: Duration = Duration::from_millis(320); |
| 6 | pub const INCOMING_APPEAR: Duration = Duration::from_millis(240); |
| 7 | |
| 8 | const OWN_RISE: f32 = 56.; |
| 9 | const OWN_START_OPACITY: f32 = 0.5; |
| 10 | const OWN_FADE_SHARE: f32 = 0.4; |
| 11 | |
| 12 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 13 | pub enum AppearKind { |
| 14 | Own, |
| 15 | Incoming, |
| 16 | } |
| 17 | |
| 18 | impl AppearKind { |
| 19 | pub fn duration(self) -> Duration { |
| 20 | match self { |
| 21 | Self::Own => OWN_APPEAR, |
| 22 | Self::Incoming => INCOMING_APPEAR, |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 28 | pub struct AppearFrame { |
| 29 | pub offset_x: f32, |
| 30 | pub offset_y: f32, |
| 31 | pub opacity: f32, |
| 32 | } |
| 33 | |
| 34 | pub fn appear_frame(kind: AppearKind, progress: f32) -> AppearFrame { |
| 35 | let progress = progress.clamp(0., 1.); |
| 36 | match kind { |
| 37 | AppearKind::Own => { |
| 38 | let fade = ease_out_cubic(progress / OWN_FADE_SHARE); |
| 39 | AppearFrame { |
| 40 | offset_x: 0., |
| 41 | offset_y: (1. - ease_out_quart(progress)) * OWN_RISE, |
| 42 | opacity: OWN_START_OPACITY + (1. - OWN_START_OPACITY) * fade, |
| 43 | } |
| 44 | } |
| 45 | AppearKind::Incoming => AppearFrame { |
| 46 | offset_x: 0., |
| 47 | offset_y: 0., |
| 48 | opacity: ease_out_cubic(progress), |
| 49 | }, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub fn appear(body: Div, message_key: &str, kind: AppearKind) -> AnimationElement<Div> { |
| 54 | body.relative().with_animation( |
| 55 | SharedString::from(format!("message-appear/{message_key}")), |
| 56 | Animation::new(kind.duration()), |
| 57 | move |body, progress| { |
| 58 | let frame = appear_frame(kind, progress); |
| 59 | body.left(px(frame.offset_x)) |
| 60 | .top(px(frame.offset_y)) |
| 61 | .opacity(frame.opacity) |
| 62 | }, |
| 63 | ) |
| 64 | } |
| 65 | |
| 66 | fn ease_out_cubic(progress: f32) -> f32 { |
| 67 | 1. - (1. - progress.clamp(0., 1.)).powi(3) |
| 68 | } |
| 69 | |
| 70 | fn ease_out_quart(progress: f32) -> f32 { |
| 71 | 1. - (1. - progress.clamp(0., 1.)).powi(4) |
| 72 | } |
| 73 | |
| 74 | #[cfg(test)] |
| 75 | mod tests { |
| 76 | use super::{AppearFrame, AppearKind, OWN_RISE, appear_frame}; |
| 77 | |
| 78 | #[test] |
| 79 | fn both_kinds_settle_on_the_resting_place() { |
| 80 | for kind in [AppearKind::Own, AppearKind::Incoming] { |
| 81 | assert_eq!( |
| 82 | appear_frame(kind, 1.), |
| 83 | AppearFrame { |
| 84 | offset_x: 0., |
| 85 | offset_y: 0., |
| 86 | opacity: 1. |
| 87 | } |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn own_messages_glide_up_from_behind_the_composer_without_overshoot() { |
| 94 | let start = appear_frame(AppearKind::Own, 0.); |
| 95 | assert_eq!(start.offset_y, OWN_RISE); |
| 96 | assert!((1..100).all(|step| { |
| 97 | let frame = appear_frame(AppearKind::Own, step as f32 / 100.); |
| 98 | frame.offset_y >= 0. && frame.offset_x == 0. |
| 99 | })); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn incoming_messages_only_fade_in() { |
| 104 | assert_eq!(appear_frame(AppearKind::Incoming, 0.).opacity, 0.); |
| 105 | assert!((0..=100).all(|step| { |
| 106 | let frame = appear_frame(AppearKind::Incoming, step as f32 / 100.); |
| 107 | frame.offset_x == 0. && frame.offset_y == 0. |
| 108 | })); |
| 109 | } |
| 110 | } |