moly_kit/utils/makepad/
hits.rs

1//! Extensions to Makepad's `Hit` type. Useful place to detect common UX patterns.
2
3use makepad_widgets::*;
4
5pub trait HitExt {
6    /// If the primary pointer action happened, returns the position where it happened.
7    fn primary_pointer_action_pos(&self) -> Option<DVec2>;
8    /// If the secondary pointer action happened, returns the position where it happened.
9    fn secondary_pointer_action_pos(&self) -> Option<DVec2>;
10    /// This was a left mouse click or a simple touch screen tap.
11    fn is_primary_pointer_action(&self) -> bool;
12    /// This was a right mouse click or a long press on touch screen.
13    fn is_secondary_pointer_action(&self) -> bool;
14}
15
16impl HitExt for Hit {
17    fn primary_pointer_action_pos(&self) -> Option<DVec2> {
18        match self {
19            Hit::FingerUp(fu)
20                if fu.was_tap()
21                    && ((fu.is_mouse() && fu.mouse_button().unwrap().is_primary())
22                        || fu.is_touch()) =>
23            {
24                Some(fu.abs)
25            }
26            _ => None,
27        }
28    }
29
30    fn secondary_pointer_action_pos(&self) -> Option<DVec2> {
31        match self {
32            Hit::FingerUp(fu)
33                if fu.was_tap() && fu.is_mouse() && fu.mouse_button().unwrap().is_secondary() =>
34            {
35                Some(fu.abs)
36            }
37            Hit::FingerLongPress(flp) => Some(flp.abs),
38            _ => None,
39        }
40    }
41
42    fn is_primary_pointer_action(&self) -> bool {
43        self.primary_pointer_action_pos().is_some()
44    }
45
46    fn is_secondary_pointer_action(&self) -> bool {
47        self.secondary_pointer_action_pos().is_some()
48    }
49}