use crate::input::pointer::MouseButton;
use crate::types::WidgetId;
pub const DRAG_THRESHOLD_PX: f64 = 6.0;
pub const DOUBLE_CLICK_WINDOW_S: f64 = 0.35;
pub const MULTI_CLICK_MAX_DIST_PX: f64 = 6.0;
pub const MAX_CLICK_COUNT: u8 = 3;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PressOrigin {
pub x: f64,
pub y: f64,
pub button: MouseButton,
pub t: f64,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Release {
pub x: f64,
pub y: f64,
pub t: f64,
pub button: MouseButton,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CookState {
pub press_origin: Option<PressOrigin>,
pub drag_armed: bool,
pub last_release: Option<Release>,
pub click_count: u8,
pub grabbed: Option<WidgetId>,
pub last_pos: Option<(f64, f64)>,
}
pub fn next_click_count(
previous: u8,
last_release: Option<Release>,
x: f64,
y: f64,
button: MouseButton,
now: f64,
) -> u8 {
let continues = match last_release {
Some(r) => {
r.button == button
&& now - r.t <= DOUBLE_CLICK_WINDOW_S
&& now >= r.t
&& (x - r.x).hypot(y - r.y) <= MULTI_CLICK_MAX_DIST_PX
}
None => false,
};
if continues {
previous.saturating_add(1).clamp(1, MAX_CLICK_COUNT)
} else {
1
}
}
pub fn crosses_drag_threshold(origin: &PressOrigin, x: f64, y: f64) -> bool {
(x - origin.x).hypot(y - origin.y) >= DRAG_THRESHOLD_PX
}
impl CookState {
pub fn press(&mut self, x: f64, y: f64, button: MouseButton, now: f64) -> u8 {
self.click_count = next_click_count(self.click_count, self.last_release, x, y, button, now);
self.press_origin = Some(PressOrigin { x, y, button, t: now });
self.drag_armed = false;
self.last_pos = Some((x, y));
self.click_count
}
pub fn motion(&mut self, x: f64, y: f64) -> Motion {
let (dx, dy) = match self.last_pos {
Some((px, py)) => (x - px, y - py),
None => (0.0, 0.0),
};
self.last_pos = Some((x, y));
let was_armed = self.drag_armed;
if let Some(origin) = &self.press_origin {
if !self.drag_armed && crosses_drag_threshold(origin, x, y) {
self.drag_armed = true;
}
}
Motion { dx, dy, drag_armed: self.drag_armed, drag_started: self.drag_armed && !was_armed }
}
pub fn release(&mut self, x: f64, y: f64, button: MouseButton, now: f64) -> bool {
let was_drag = self.drag_armed;
self.last_release = Some(Release { x, y, t: now, button });
self.press_origin = None;
self.drag_armed = false;
self.last_pos = Some((x, y));
was_drag
}
pub fn grab(&mut self, id: WidgetId) {
self.grabbed = Some(id);
}
pub fn release_grab(&mut self) {
self.grabbed = None;
}
pub fn is_double(&self) -> bool {
self.click_count == 2
}
pub fn is_triple(&self) -> bool {
self.click_count == MAX_CLICK_COUNT
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Motion {
pub dx: f64,
pub dy: f64,
pub drag_armed: bool,
pub drag_started: bool,
}
#[cfg(test)]
mod tests {
use super::*;
const L: MouseButton = MouseButton::Left;
fn click(state: &mut CookState, x: f64, y: f64, t: f64) -> u8 {
let n = state.press(x, y, L, t);
state.release(x, y, L, t + 0.05);
n
}
#[test]
fn first_press_is_a_single_click() {
let mut s = CookState::default();
assert_eq!(s.click_count, 0);
assert_eq!(s.press(10.0, 10.0, L, 0.0), 1);
}
#[test]
fn quick_presses_count_up_to_three_and_stay_there() {
let mut s = CookState::default();
assert_eq!(click(&mut s, 10.0, 10.0, 0.0), 1);
assert_eq!(click(&mut s, 11.0, 10.0, 0.2), 2);
assert!(s.is_double());
assert_eq!(click(&mut s, 11.0, 11.0, 0.4), 3);
assert!(s.is_triple());
assert_eq!(click(&mut s, 11.0, 11.0, 0.6), 3);
}
#[test]
fn a_slow_second_press_restarts_the_sequence() {
let mut s = CookState::default();
click(&mut s, 10.0, 10.0, 0.0);
assert_eq!(s.press(10.0, 10.0, L, 0.5), 1);
}
#[test]
fn the_window_edge_is_inclusive() {
let r = Release { x: 0.0, y: 0.0, t: 0.0, button: L };
assert_eq!(next_click_count(1, Some(r), 0.0, 0.0, L, DOUBLE_CLICK_WINDOW_S), 2);
assert_eq!(next_click_count(1, Some(r), 0.0, 0.0, L, DOUBLE_CLICK_WINDOW_S + 1e-9), 1);
}
#[test]
fn a_far_or_other_button_press_restarts_the_sequence() {
let mut s = CookState::default();
click(&mut s, 10.0, 10.0, 0.0);
assert_eq!(s.press(10.0 + MULTI_CLICK_MAX_DIST_PX + 0.1, 10.0, L, 0.1), 1);
s.release(16.1, 10.0, L, 0.15);
assert_eq!(s.press(16.1, 10.0, MouseButton::Right, 0.2), 1);
}
#[test]
fn a_clock_going_backwards_does_not_continue_a_sequence() {
let r = Release { x: 0.0, y: 0.0, t: 5.0, button: L };
assert_eq!(next_click_count(1, Some(r), 0.0, 0.0, L, 4.9), 1);
}
#[test]
fn drag_arms_once_at_the_threshold_and_release_reports_it() {
let mut s = CookState::default();
s.press(0.0, 0.0, L, 0.0);
let m = s.motion(3.0, 0.0);
assert!(!m.drag_armed && !m.drag_started);
assert_eq!((m.dx, m.dy), (3.0, 0.0));
let m = s.motion(DRAG_THRESHOLD_PX, 0.0);
assert!(m.drag_armed && m.drag_started);
let m = s.motion(1.0, 0.0);
assert!(m.drag_armed && !m.drag_started);
assert!(s.release(1.0, 0.0, L, 0.3), "release after a drag reports the drag");
assert!(!s.drag_armed);
assert!(s.press_origin.is_none());
}
#[test]
fn motion_without_a_press_never_arms() {
let mut s = CookState::default();
let m = s.motion(100.0, 100.0);
assert!(!m.drag_armed);
assert_eq!((m.dx, m.dy), (0.0, 0.0));
let m = s.motion(110.0, 100.0);
assert_eq!((m.dx, m.dy), (10.0, 0.0));
assert!(!m.drag_armed);
}
#[test]
fn a_plain_click_release_is_not_a_drag() {
let mut s = CookState::default();
s.press(0.0, 0.0, L, 0.0);
s.motion(2.0, 2.0);
assert!(!s.release(2.0, 2.0, L, 0.1));
}
#[test]
fn grab_is_set_and_cleared() {
let mut s = CookState::default();
s.grab(WidgetId::new("splitter"));
assert_eq!(s.grabbed, Some(WidgetId::new("splitter")));
s.release_grab();
assert_eq!(s.grabbed, None);
}
}