use std::collections::VecDeque;
use std::time::Duration;
use tokio::time::Instant;
pub const FRAME: Duration = Duration::from_millis(16);
pub const GLOW_FRAME: Duration = Duration::from_millis(50);
pub const LIGHTS: Duration = Duration::from_millis(1000);
pub const POPUP: Duration = Duration::from_millis(150);
pub const GLOW: Duration = Duration::from_millis(1500);
pub const SLIDE: Duration = Duration::from_millis(200);
const MAX_GLOWING: usize = 32;
#[derive(Debug, Default)]
pub struct Motion {
pub focused: Option<Instant>,
popup: Option<Instant>,
popup_was_up: bool,
arrived: VecDeque<(i64, i64, Instant)>,
}
fn running(at: Instant, length: Duration, now: Instant) -> Option<Duration> {
let elapsed = now.saturating_duration_since(at);
(elapsed < length).then_some(elapsed)
}
impl Motion {
pub fn lights(&self, now: Instant) -> Option<Duration> {
self.focused.and_then(|at| running(at, LIGHTS, now))
}
pub fn popup(&mut self, up: bool, animate: bool, now: Instant) -> Option<Duration> {
if up && !self.popup_was_up && animate {
self.popup = Some(now);
}
self.popup_was_up = up;
if !up {
self.popup = None;
}
self.popup
.filter(|_| up && animate)
.and_then(|at| running(at, POPUP, now))
}
pub fn stop(&mut self) {
self.focused = None;
self.popup = None;
self.arrived.clear();
}
pub fn arrived(&mut self, chat_id: i64, message_id: i64, now: Instant) {
if self.arrived.len() >= MAX_GLOWING {
self.arrived.pop_front();
}
self.arrived.push_back((chat_id, message_id, now));
}
pub fn glowing(&self, chat_id: i64, now: Instant) -> Vec<(i64, Duration)> {
self.arrived
.iter()
.filter(|&&(chat, _, _)| chat == chat_id)
.filter_map(|&(_, id, at)| Some((id, running(at, GLOW, now)?)))
.collect()
}
pub fn next_frame(&mut self, now: Instant, open: Option<i64>) -> Option<Instant> {
let moving = |at: Instant, length: Duration, frame: Duration| {
running(at, length + frame, now).is_some()
};
self.focused = self.focused.filter(|&at| moving(at, LIGHTS, FRAME));
self.popup = self.popup.filter(|&at| moving(at, POPUP, FRAME));
self.arrived
.retain(|&(chat, _, at)| Some(chat) == open && moving(at, GLOW, GLOW_FRAME));
if self.focused.is_some() || self.popup.is_some() {
Some(now + FRAME)
} else {
(!self.arrived.is_empty()).then_some(now + GLOW_FRAME)
}
}
}
pub fn toast_out(shown: Instant, until: Instant, now: Instant) -> f32 {
let left = |d: Duration| 1.0 - (d.as_secs_f32() / SLIDE.as_secs_f32()).min(1.0);
let coming = left(now.saturating_duration_since(shown));
let going = left(until.saturating_duration_since(now));
let out = coming.max(going);
out * out
}
pub fn toast_frame(shown: Instant, until: Instant, now: Instant) -> Option<Instant> {
let coming = running(shown, SLIDE + FRAME, now).is_some();
let going = until.saturating_duration_since(now) < SLIDE;
(coming || going).then_some(now + FRAME)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_popup_fades_in_once_when_it_comes_up() {
let mut motion = Motion::default();
let start = Instant::now();
assert_eq!(motion.popup(false, true, start), None);
assert_eq!(motion.popup(true, true, start), Some(Duration::ZERO));
let later = start + POPUP / 2;
assert_eq!(motion.popup(true, true, later), Some(POPUP / 2));
assert_eq!(motion.popup(true, true, start + POPUP), None, "faded in");
assert_eq!(
motion.popup(true, true, start + POPUP * 3),
None,
"still up: another popup in its place doesn't fade again"
);
assert_eq!(motion.popup(false, true, start + POPUP * 4), None);
let again = start + POPUP * 5;
assert_eq!(
motion.popup(true, true, again),
Some(Duration::ZERO),
"a new one"
);
}
#[test]
fn a_popup_that_isnt_animated_wakes_nothing() {
let mut motion = Motion::default();
let start = Instant::now();
assert_eq!(motion.popup(true, false, start), None);
assert_eq!(motion.next_frame(start, None), None, "no frames to draw");
assert_eq!(motion.popup(true, true, start + FRAME), None);
assert_eq!(motion.next_frame(start + FRAME, None), None);
}
#[test]
fn frames_go_on_until_one_shows_everything_done() {
let mut motion = Motion::default();
let start = Instant::now();
assert_eq!(motion.next_frame(start, Some(1)), None, "nothing moves");
motion.focused = Some(start);
motion.arrived(1, 10, start);
assert_eq!(motion.next_frame(start, Some(1)), Some(start + FRAME));
assert_eq!(motion.lights(start + LIGHTS / 2), Some(LIGHTS / 2));
let after_lights = start + LIGHTS;
assert_eq!(motion.lights(after_lights), None);
assert!(motion.next_frame(after_lights, Some(1)).is_some());
assert!(motion.focused.is_some());
let later = after_lights + FRAME;
assert_eq!(
motion.next_frame(later, Some(1)),
Some(later + GLOW_FRAME),
"only a glow left, which needs fewer frames"
);
assert_eq!(motion.focused, None);
assert_eq!(motion.glowing(1, after_lights), [(10, LIGHTS)]);
assert!(motion.glowing(2, after_lights).is_empty(), "another chat's");
assert!(
motion.next_frame(start + GLOW, Some(1)).is_some(),
"its last frame"
);
assert_eq!(motion.next_frame(start + GLOW + GLOW_FRAME, Some(1)), None);
assert!(motion.glowing(1, start).is_empty(), "dropped");
}
#[test]
fn leaving_a_chat_stops_its_glows() {
let mut motion = Motion::default();
let start = Instant::now();
motion.arrived(1, 10, start);
assert!(motion.next_frame(start, Some(1)).is_some());
assert_eq!(motion.next_frame(start, Some(2)), None, "another chat open");
assert!(motion.glowing(1, start).is_empty());
}
#[test]
fn a_busy_chat_keeps_only_the_newest_glowing() {
let mut motion = Motion::default();
let now = Instant::now();
for id in 0..100 {
motion.arrived(1, id, now);
}
let glowing = motion.glowing(1, now);
assert_eq!(glowing.len(), MAX_GLOWING);
assert_eq!(glowing[0].0, 100 - MAX_GLOWING as i64);
}
#[test]
fn the_toast_slides_in_then_out() {
let shown = Instant::now();
let until = shown + Duration::from_secs(2);
assert_eq!(toast_out(shown, until, shown), 1.0, "out of sight at first");
let half = toast_out(shown, until, shown + SLIDE / 2);
assert!(half > 0.0 && half < 0.5, "most of the way in: {half}");
assert_eq!(toast_out(shown, until, shown + SLIDE), 0.0);
assert_eq!(toast_out(shown, until, shown + Duration::from_secs(1)), 0.0);
assert!(
toast_out(shown, until, until - SLIDE / 2) > 0.0,
"on its way out"
);
assert_eq!(toast_out(shown, until, until), 1.0);
assert!(toast_frame(shown, until, shown).is_some());
assert!(toast_frame(shown, until, shown + SLIDE + FRAME).is_none());
assert!(toast_frame(shown, until, until - SLIDE / 2).is_some());
}
}