use std::time::{Duration, Instant};
pub fn ease_out_cubic(t: f32) -> f32 {
1.0 - (1.0 - t).powi(3)
}
pub fn ease_in_cubic(t: f32) -> f32 {
t * t * t
}
pub fn lerp_f32(from: f32, to: f32, t: f32) -> f32 {
from + (to - from) * t
}
pub fn progress(since: Instant, duration: Duration) -> f32 {
progress_at(since, duration, Instant::now())
}
fn progress_at(since: Instant, duration: Duration, now: Instant) -> f32 {
if duration.is_zero() {
return 1.0;
}
(now.saturating_duration_since(since).as_secs_f32() / duration.as_secs_f32()).clamp(0.0, 1.0)
}
pub const MODAL_ENTER: Duration = Duration::from_millis(220);
pub const MODAL_EXIT: Duration = Duration::from_millis(160);
pub fn modal_opacity(enter_at: Option<Instant>, exit_at: Option<Instant>) -> (f32, bool) {
modal_opacity_at(enter_at, exit_at, Instant::now(), MODAL_ENTER, MODAL_EXIT)
}
pub(crate) fn modal_opacity_at(
enter_at: Option<Instant>,
exit_at: Option<Instant>,
now: Instant,
enter_duration: Duration,
exit_duration: Duration,
) -> (f32, bool) {
if let Some(since) = exit_at {
let opacity = enter_at
.map(|enter| ease_out_cubic(progress_at(enter, enter_duration, since)))
.unwrap_or(1.0);
let t = progress_at(since, exit_duration, now);
(opacity * (1.0 - ease_in_cubic(t)), t < 1.0)
} else if let Some(since) = enter_at {
let t = progress_at(since, enter_duration, now);
(ease_out_cubic(t), t < 1.0)
} else {
(1.0, false)
}
}
pub fn slowed(duration: Duration) -> Duration {
static SCALE: std::sync::OnceLock<f64> = std::sync::OnceLock::new();
let scale = *SCALE.get_or_init(|| {
std::env::var("VAMPIR_SLOW_MOTION")
.ok()
.and_then(|value| value.parse::<f64>().ok())
.filter(|scale| scale.is_finite() && *scale > 0.0)
.unwrap_or(1.0)
});
duration.mul_f64(scale)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn closing_mid_entrance_starts_at_the_visible_opacity() {
let enter = Instant::now();
let exit = enter + MODAL_ENTER / 2;
let fade = |now| modal_opacity_at(Some(enter), Some(exit), now, MODAL_ENTER, MODAL_EXIT);
assert_eq!(fade(exit), (0.875, true));
assert_eq!(fade(exit + MODAL_EXIT / 2), (0.875 * 0.875, true));
assert_eq!(fade(exit + MODAL_EXIT), (0.0, false));
}
#[test]
fn an_immediate_close_never_flashes_the_panel() {
let now = Instant::now();
assert_eq!(
modal_opacity_at(Some(now), Some(now), now, MODAL_ENTER, MODAL_EXIT).0,
0.0,
);
assert_eq!(
modal_opacity_at(None, Some(now), now, MODAL_ENTER, MODAL_EXIT),
(1.0, true),
);
}
#[test]
fn zero_duration_and_future_starts_have_defined_progress() {
let now = Instant::now();
assert_eq!(progress_at(now, Duration::ZERO, now), 1.0);
assert_eq!(progress_at(now + MODAL_ENTER, MODAL_ENTER, now), 0.0);
}
}