1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
//! Build mouse events.
mod button;
mod event;
use crate::Point;
pub use button::Button;
pub use event::{Event, ScrollDelta};
use std::time::{Duration, SystemTime};
/// enum to track the type of the last click
#[derive(Debug, Copy, Clone)]
pub enum Interaction {
/// Last Click was a single click
Click(Point),
/// Last Click was a double click
DoubleClick(Point),
/// Last Click was a triple click
TripleClick(Point),
}
/// Compiler bully
#[derive(Debug, Copy, Clone)]
pub struct State {
last_click: Option<Interaction>,
last_click_timestamp: Option<SystemTime>,
}
impl Default for State {
fn default() -> Self {
State {
last_click: None,
last_click_timestamp: None,
}
}
}
impl State {
/// processes left click to check for double/triple clicks
/// return amount of repetitive mouse clicks
/// (1 -> double click, 2 -> triple click)
pub fn update(&mut self, position: Point) -> Interaction {
self.last_click = match self.last_click {
None => Some(Interaction::Click(position)),
Some(x) => match x {
Interaction::Click(p) if self.process_click(p, position) => {
Some(Interaction::DoubleClick(position))
}
Interaction::DoubleClick(p)
if self.process_click(p, position) =>
{
Some(Interaction::TripleClick(position))
}
_ => Some(Interaction::Click(position)),
},
};
self.last_click_timestamp = Some(SystemTime::now());
self.last_click.unwrap_or(Interaction::Click(position))
}
fn process_click(&self, old_position: Point, new_position: Point) -> bool {
old_position == new_position
&& SystemTime::now()
.duration_since(
self.last_click_timestamp.unwrap_or(SystemTime::UNIX_EPOCH),
)
.unwrap_or(Duration::from_secs(1))
.as_millis()
<= 500
}
}
|