Skip to main content

iced_cube/
click.rs

1//! Counting single, double and triple clicks on a clock the caller passes in.
2//!
3//! iced's own click counter reads the real clock, so components that count
4//! clicks use this one with the time from the app's clock instead. That lets
5//! a test harness on a virtual clock time double clicks exactly.
6
7use iced::mouse::Button;
8use iced::time::{Duration, Instant};
9use iced::{Point, Transformation};
10
11use std::ops::Mul;
12
13/// The longest gap between two presses that still counts them together.
14pub const INTERVAL: Duration = Duration::from_millis(300);
15
16/// How far apart, in logical pixels, two presses can be and still count
17/// together.
18pub const DISTANCE: f32 = 6.0;
19
20/// A mouse click and how many came before it in a row.
21#[derive(Debug, Clone, Copy, PartialEq)]
22pub struct Click {
23    kind: Kind,
24    button: Button,
25    position: Point,
26    time: Instant,
27}
28
29/// The kind of click.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub enum Kind {
32    Single,
33    Double,
34    Triple,
35}
36
37impl Kind {
38    fn next(self) -> Kind {
39        match self {
40            Kind::Single => Kind::Double,
41            Kind::Double => Kind::Triple,
42            Kind::Triple => Kind::Double,
43        }
44    }
45}
46
47impl Click {
48    /// A press of `button` at `position` at the instant `now`, counted
49    /// against the `previous` click.
50    pub fn new(position: Point, button: Button, previous: Option<Click>, now: Instant) -> Click {
51        let kind = match previous {
52            Some(previous)
53                if previous.button == button && previous.is_consecutive(position, now) =>
54            {
55                previous.kind.next()
56            }
57            _ => Kind::Single,
58        };
59        Click {
60            kind,
61            button,
62            position,
63            time: now,
64        }
65    }
66
67    pub fn kind(&self) -> Kind {
68        self.kind
69    }
70
71    pub fn position(&self) -> Point {
72        self.position
73    }
74
75    fn is_consecutive(&self, position: Point, now: Instant) -> bool {
76        let Some(gap) = now.checked_duration_since(self.time) else {
77            return false;
78        };
79        !gap.is_zero() && gap <= INTERVAL && self.position.distance(position) < DISTANCE
80    }
81}
82
83impl Mul<Transformation> for Click {
84    type Output = Click;
85
86    fn mul(self, transformation: Transformation) -> Click {
87        Click {
88            position: self.position * transformation,
89            ..self
90        }
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    const AT: Point = Point::new(10.0, 10.0);
99
100    fn presses(start: Instant, gaps_ms: &[u64]) -> Vec<Kind> {
101        let mut last = None;
102        let mut now = start;
103        let mut kinds = Vec::new();
104        for gap in gaps_ms {
105            now += Duration::from_millis(*gap);
106            let click = Click::new(AT, Button::Left, last, now);
107            kinds.push(click.kind());
108            last = Some(click);
109        }
110        kinds
111    }
112
113    #[test]
114    fn quick_presses_count_up_to_triple_and_then_alternate() {
115        let kinds = presses(Instant::now(), &[0, 100, 100, 100]);
116        assert_eq!(
117            kinds,
118            vec![Kind::Single, Kind::Double, Kind::Triple, Kind::Double]
119        );
120    }
121
122    #[test]
123    fn a_gap_longer_than_the_interval_starts_again() {
124        let kinds = presses(Instant::now(), &[0, 300, 301]);
125        assert_eq!(kinds, vec![Kind::Single, Kind::Double, Kind::Single]);
126    }
127
128    #[test]
129    fn presses_at_the_same_instant_do_not_count_together() {
130        let kinds = presses(Instant::now(), &[0, 0]);
131        assert_eq!(kinds, vec![Kind::Single, Kind::Single]);
132    }
133
134    #[test]
135    fn an_earlier_instant_does_not_count_together() {
136        let start = Instant::now() + Duration::from_secs(1);
137        let first = Click::new(AT, Button::Left, None, start);
138        let earlier = start - Duration::from_millis(50);
139        let second = Click::new(AT, Button::Left, Some(first), earlier);
140        assert_eq!(second.kind(), Kind::Single);
141    }
142
143    #[test]
144    fn a_far_press_or_another_button_starts_again() {
145        let start = Instant::now();
146        let first = Click::new(AT, Button::Left, None, start);
147        let later = start + Duration::from_millis(100);
148        let far = Click::new(Point::new(16.0, 10.0), Button::Left, Some(first), later);
149        assert_eq!(far.kind(), Kind::Single);
150        let near = Click::new(Point::new(15.0, 10.0), Button::Left, Some(first), later);
151        assert_eq!(near.kind(), Kind::Double);
152        let right = Click::new(AT, Button::Right, Some(first), later);
153        assert_eq!(right.kind(), Kind::Single);
154    }
155
156    #[test]
157    fn a_transformation_moves_the_position_and_keeps_the_count() {
158        let start = Instant::now();
159        let first = Click::new(AT, Button::Left, None, start);
160        let second = Click::new(AT, Button::Left, Some(first), start + INTERVAL);
161        let scaled = second * Transformation::scale(2.0);
162        assert_eq!(scaled.position(), Point::new(20.0, 20.0));
163        assert_eq!(scaled.kind(), Kind::Double);
164    }
165}