freya_core/
events_combos.rs1use std::time::{
2 Duration,
3 Instant,
4};
5
6use torin::prelude::CursorPoint;
7
8use crate::{
9 integration::ScopeId,
10 prelude::{
11 State,
12 *,
13 },
14};
15
16#[derive(Clone, Copy, PartialEq)]
17pub struct EventsCombos {
18 pub(crate) last_press: State<Option<(Instant, CursorPoint, u8)>>,
19}
20
21impl EventsCombos {
22 pub fn get() -> Self {
23 match try_consume_root_context() {
24 Some(rt) => rt,
25 None => {
26 let context_menu_state = EventsCombos {
27 last_press: State::create_in_scope(None, ScopeId::ROOT),
28 };
29 provide_context_for_scope_id(context_menu_state, ScopeId::ROOT);
30 context_menu_state
31 }
32 }
33 }
34
35 pub fn pressed(location: CursorPoint) -> PressEventType {
36 let mut combos = Self::get();
37 let (event_type, click_count) = match &*combos.last_press.read() {
38 Some((inst, last_location, count)) if inst.elapsed() <= MULTI_PRESS_ELAPSED => {
39 if last_location.distance_to(location) <= LOCATION_THRESHOLD {
40 match count {
41 1 => (PressEventType::Double, 2),
42 2 => (PressEventType::Triple, 3),
43 _ => (PressEventType::Single, 1),
44 }
45 } else {
46 (PressEventType::Single, 1)
47 }
48 }
49 _ => (PressEventType::Single, 1),
50 };
51 combos
52 .last_press
53 .set(Some((Instant::now(), location, click_count)));
54 event_type
55 }
56}
57
58const LOCATION_THRESHOLD: f64 = 5.0;
59const MULTI_PRESS_ELAPSED: Duration = Duration::from_millis(500);
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62pub enum PressEventType {
63 Single,
64 Double,
65 Triple,
66}
67
68impl PressEventType {
69 pub fn is_single(&self) -> bool {
70 matches!(self, Self::Single)
71 }
72
73 pub fn is_double(&self) -> bool {
74 matches!(self, Self::Double)
75 }
76
77 pub fn is_triple(&self) -> bool {
78 matches!(self, Self::Triple)
79 }
80}