Skip to main content

kova_native_input/
click.rs

1//! Multi-click (double / triple click) detection.
2
3use crate::MouseButton;
4use kova_native_core::Point;
5use std::time::{Duration, Instant};
6
7/// Counts consecutive clicks that are close in time and space.
8#[derive(Clone, Debug)]
9pub struct ClickTracker {
10    last: Option<(MouseButton, Point, Instant)>,
11    count: u32,
12    /// Maximum delay between clicks of a multi-click.
13    pub interval: Duration,
14    /// Maximum pointer travel between clicks of a multi-click.
15    pub slop: f32,
16}
17
18impl Default for ClickTracker {
19    fn default() -> Self {
20        ClickTracker {
21            last: None,
22            count: 0,
23            interval: Duration::from_millis(500),
24            slop: 4.0,
25        }
26    }
27}
28
29impl ClickTracker {
30    /// Registers a button press and returns its click count (1, 2, 3, ...).
31    pub fn press(&mut self, button: MouseButton, position: Point, now: Instant) -> u32 {
32        let continues = self.last.is_some_and(|(b, p, t)| {
33            b == button
34                && now.saturating_duration_since(t) <= self.interval
35                && p.distance(position) <= self.slop
36        });
37        self.count = if continues { self.count + 1 } else { 1 };
38        self.last = Some((button, position, now));
39        self.count
40    }
41
42    pub fn count(&self) -> u32 {
43        self.count
44    }
45
46    pub fn reset(&mut self) {
47        self.last = None;
48        self.count = 0;
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use kova_native_core::point;
56
57    #[test]
58    fn double_and_triple_click() {
59        let mut t = ClickTracker::default();
60        let t0 = Instant::now();
61        assert_eq!(t.press(MouseButton::Left, point(10.0, 10.0), t0), 1);
62        assert_eq!(
63            t.press(
64                MouseButton::Left,
65                point(11.0, 10.0),
66                t0 + Duration::from_millis(200)
67            ),
68            2
69        );
70        assert_eq!(
71            t.press(
72                MouseButton::Left,
73                point(11.0, 11.0),
74                t0 + Duration::from_millis(400)
75            ),
76            3
77        );
78        // Too slow.
79        assert_eq!(
80            t.press(
81                MouseButton::Left,
82                point(11.0, 11.0),
83                t0 + Duration::from_millis(1400)
84            ),
85            1
86        );
87        // Too far.
88        assert_eq!(
89            t.press(
90                MouseButton::Left,
91                point(50.0, 11.0),
92                t0 + Duration::from_millis(1500)
93            ),
94            1
95        );
96        // Different button.
97        assert_eq!(
98            t.press(
99                MouseButton::Right,
100                point(50.0, 11.0),
101                t0 + Duration::from_millis(1600)
102            ),
103            1
104        );
105    }
106}