rustimal 0.7.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! Time-based tweens: fixed duration, ease-out, retargetable mid-flight.
//! Being driven by timestamps (not per-frame deltas) keeps motion steady
//! regardless of frame pacing or how long the app was idle before.

use std::time::Instant;

use crate::layout::Rect;

pub trait Lerp: Copy + PartialEq {
    fn lerp(a: Self, b: Self, t: f32) -> Self;
}

impl Lerp for f32 {
    fn lerp(a: f32, b: f32, t: f32) -> f32 {
        a + (b - a) * t
    }
}

impl Lerp for Rect {
    fn lerp(a: Rect, b: Rect, t: f32) -> Rect {
        a.lerp(&b, t)
    }
}

/// Cubic ease-out: quick start, gentle settle.
fn ease_out(t: f32) -> f32 {
    let u = 1.0 - t;
    1.0 - u * u * u
}

#[derive(Clone, Copy, Debug)]
pub struct Tween<T: Lerp> {
    from: T,
    to: T,
    start: Instant,
    dur: f32,
}

impl<T: Lerp> Tween<T> {
    pub fn new(value: T) -> Self {
        Self { from: value, to: value, start: Instant::now(), dur: 0.0 }
    }

    pub fn target(&self) -> T {
        self.to
    }

    /// Animate from the current value to `to`. No-op if already heading there.
    pub fn set(&mut self, to: T, dur: f32, now: Instant) {
        if to != self.to {
            self.from = self.get(now);
            self.to = to;
            self.start = now;
            self.dur = dur;
        }
    }

    /// Animate from an explicit starting value.
    #[allow(clippy::wrong_self_convention)]
    pub fn from_to(&mut self, from: T, to: T, dur: f32, now: Instant) {
        self.from = from;
        self.to = to;
        self.start = now;
        self.dur = dur;
    }

    pub fn snap(&mut self, value: T) {
        self.from = value;
        self.to = value;
        self.dur = 0.0;
    }

    pub fn get(&self, now: Instant) -> T {
        if self.dur <= 0.0 {
            return self.to;
        }
        let t = now.saturating_duration_since(self.start).as_secs_f32() / self.dur;
        if t >= 1.0 { self.to } else { T::lerp(self.from, self.to, ease_out(t)) }
    }

    pub fn active(&self, now: Instant) -> bool {
        self.dur > 0.0 && now.saturating_duration_since(self.start).as_secs_f32() < self.dur
    }
}