use std::time::Duration;
use web_time::Instant;
const DRAG_SLOP: f32 = 6.0;
const FRICTION_TAU: f32 = 0.35;
const MIN_FLING: f32 = 50.0;
const STOP_SPEED: f32 = 10.0;
const VELOCITY_WINDOW: Duration = Duration::from_millis(100);
#[derive(Default)]
pub struct Drag {
start: Option<(f32, f32)>,
dragging: bool,
samples: Vec<(Instant, f32)>,
fling: Option<Fling>,
}
#[derive(Clone, Copy)]
struct Fling {
start: Instant,
from: f32,
v: f32,
}
impl Drag {
pub fn down(&mut self, pos: f32, offset: f32, now: Instant) {
self.fling = None;
self.start = Some((pos, offset));
self.dragging = false;
self.samples = vec![(now, pos)];
}
pub fn moved(&mut self, pos: f32, now: Instant) -> Option<f32> {
let (start, from) = self.start?;
self.samples
.retain(|(t, _)| now.saturating_duration_since(*t) <= VELOCITY_WINDOW);
self.samples.push((now, pos));
self.dragging |= (pos - start).abs() > DRAG_SLOP;
self.dragging.then_some(from - (pos - start))
}
pub fn up(&mut self, pos: f32, offset: f32, now: Instant) -> bool {
let dragged = std::mem::take(&mut self.dragging);
if dragged {
self.samples.push((now, pos));
let v = -velocity(&self.samples, now);
if v.abs() > MIN_FLING {
self.fling = Some(Fling {
start: now,
from: offset,
v,
});
}
}
self.start = None;
self.samples.clear();
dragged
}
pub fn cancel(&mut self) {
self.start = None;
self.dragging = false;
self.samples.clear();
}
pub fn stop(&mut self) {
self.fling = None;
}
pub fn shift(&mut self, d: f32) {
if let Some((_, from)) = &mut self.start {
*from += d;
}
if let Some(f) = &mut self.fling {
f.from += d;
}
}
pub fn pressed(&self) -> bool {
self.start.is_some()
}
pub fn flinging(&self) -> bool {
self.fling.is_some()
}
pub fn fling(&mut self, now: Instant, max: f32) -> Option<f32> {
let f = self.fling?;
let t = now.saturating_duration_since(f.start).as_secs_f32();
let decay = (-t / FRICTION_TAU).exp();
let free = f.from + f.v * FRICTION_TAU * (1.0 - decay);
let offset = free.clamp(0.0, max.max(0.0));
if offset != free || (f.v * decay).abs() <= STOP_SPEED {
self.fling = None;
}
Some(offset)
}
}
fn velocity(samples: &[(Instant, f32)], now: Instant) -> f32 {
let mut recent = samples
.iter()
.filter(|(t, _)| now.saturating_duration_since(*t) <= VELOCITY_WINDOW);
let (Some((t0, p0)), Some((t1, p1))) = (recent.next(), recent.next_back()) else {
return 0.0;
};
let dt = t1.saturating_duration_since(*t0).as_secs_f32();
if dt > 0.0 { (p1 - p0) / dt } else { 0.0 }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slop_then_follow_then_fling_to_the_edge() {
let t0 = Instant::now();
let at = |ms| t0 + Duration::from_millis(ms);
let mut d = Drag::default();
d.down(100.0, 40.0, at(0));
assert_eq!(d.moved(104.0, at(10)), None, "within slop: still a tap");
assert_eq!(d.moved(90.0, at(20)), Some(50.0));
d.shift(-10.0);
assert_eq!(d.moved(90.0, at(30)), Some(40.0));
d.shift(10.0);
assert_eq!(d.moved(40.0, at(60)), Some(100.0));
assert!(d.up(40.0, 100.0, at(60)));
let mid = d.fling(at(100), 1000.0).unwrap();
assert!(mid > 100.0, "{mid}");
assert_eq!(d.fling(at(2000), 300.0), Some(300.0), "stops at the edge");
assert_eq!(d.fling(at(2016), 300.0), None);
}
#[test]
fn tap_and_paused_drag_dont_fling() {
let t0 = Instant::now();
let at = |ms| t0 + Duration::from_millis(ms);
let mut d = Drag::default();
d.down(0.0, 0.0, at(0));
assert!(d.pressed());
assert!(!d.up(0.0, 0.0, at(50)), "a tap");
assert!(!d.pressed());
d.down(0.0, 0.0, at(100));
d.moved(-80.0, at(120));
assert!(d.up(-80.0, 80.0, at(400)));
assert!(!d.flinging());
}
}