freya_core/events/
name.rs

1#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
2pub enum EventName {
3    // Platform Mouse
4    MouseUp,
5    MouseDown,
6    MouseMove,
7
8    // Platform Mouse or Touch
9    PointerPress,
10    PointerDown,
11    PointerEnter,
12    PointerLeave,
13
14    // Platform Keyboard
15    KeyDown,
16    KeyUp,
17
18    // Platform Touch
19    TouchCancel,
20    TouchStart,
21    TouchMove,
22    TouchEnd,
23
24    GlobalPointerMove,
25    GlobalPointerPress,
26    GlobalPointerDown,
27
28    GlobalKeyDown,
29    GlobalKeyUp,
30
31    GlobalFileHover,
32    GlobalFileHoverCancelled,
33
34    CaptureGlobalPointerMove,
35    CaptureGlobalPointerPress,
36
37    Wheel,
38
39    Sized,
40
41    FileDrop,
42
43    ImePreedit,
44}
45
46use std::collections::HashSet;
47
48impl PartialOrd for EventName {
49    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
50        Some(self.cmp(other))
51    }
52}
53
54impl Ord for EventName {
55    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
56        match self {
57            // Capture events have max priority
58            e if e.is_capture() => std::cmp::Ordering::Less,
59            // Left events have more priority over non-left
60            e if e.is_left() => std::cmp::Ordering::Less,
61            e => {
62                if e == other {
63                    std::cmp::Ordering::Equal
64                } else {
65                    std::cmp::Ordering::Greater
66                }
67            }
68        }
69    }
70}
71
72impl EventName {
73    /// Check if this even captures others or not
74    pub fn is_capture(&self) -> bool {
75        matches!(
76            &self,
77            Self::CaptureGlobalPointerMove | Self::CaptureGlobalPointerPress
78        )
79    }
80
81    /// Check if this is a global pointer event
82    pub fn is_global_pointer(&self) -> bool {
83        matches!(
84            self,
85            Self::GlobalPointerMove
86                | Self::GlobalPointerPress
87                | Self::GlobalPointerDown
88                | Self::CaptureGlobalPointerMove
89                | Self::CaptureGlobalPointerPress
90        )
91    }
92
93    pub fn is_left(&self) -> bool {
94        matches!(&self, Self::PointerLeave)
95    }
96
97    pub fn is_down(&self) -> bool {
98        matches!(self, Self::PointerDown)
99    }
100
101    pub fn is_press(&self) -> bool {
102        matches!(self, Self::PointerPress)
103    }
104}
105
106impl ragnarok::NameOfEvent for EventName {
107    fn get_global_events(&self) -> HashSet<Self> {
108        match self {
109            Self::MouseUp | Self::TouchEnd => {
110                HashSet::from([Self::GlobalPointerPress, Self::CaptureGlobalPointerPress])
111            }
112            Self::MouseDown | Self::TouchStart => HashSet::from([Self::GlobalPointerDown]),
113            Self::MouseMove | Self::TouchMove => {
114                HashSet::from([Self::GlobalPointerMove, Self::CaptureGlobalPointerMove])
115            }
116
117            Self::KeyDown => HashSet::from([Self::GlobalKeyDown]),
118            Self::KeyUp => HashSet::from([Self::GlobalKeyUp]),
119
120            Self::GlobalFileHover => HashSet::from([Self::GlobalFileHover]),
121            Self::GlobalFileHoverCancelled => HashSet::from([Self::GlobalFileHoverCancelled]),
122            _ => HashSet::new(),
123        }
124    }
125
126    fn get_derived_events(&self) -> HashSet<Self> {
127        let mut events = HashSet::new();
128
129        events.insert(*self);
130
131        match self {
132            Self::MouseMove | Self::TouchMove => {
133                events.insert(Self::PointerEnter);
134            }
135            Self::MouseDown | Self::TouchStart => {
136                events.insert(Self::PointerDown);
137            }
138            Self::MouseUp | Self::TouchEnd => {
139                events.insert(Self::PointerPress);
140            }
141            _ => {}
142        }
143
144        events
145    }
146
147    fn get_cancellable_events(&self) -> HashSet<Self> {
148        let mut events = HashSet::new();
149
150        events.insert(*self);
151
152        match self {
153            Self::KeyDown => {
154                events.insert(Self::GlobalKeyDown);
155            }
156            Self::KeyUp => {
157                events.insert(Self::GlobalKeyUp);
158            }
159            Self::MouseUp | Self::TouchEnd => {
160                events.extend([Self::PointerPress, Self::GlobalPointerPress])
161            }
162            Self::PointerPress => events.extend([Self::MouseUp, Self::GlobalPointerPress]),
163            Self::MouseDown | Self::TouchStart => {
164                events.extend([Self::PointerDown, Self::GlobalPointerDown])
165            }
166            Self::PointerDown => events.extend([Self::MouseDown, Self::GlobalPointerDown]),
167            Self::CaptureGlobalPointerMove => {
168                events.extend([
169                    Self::MouseMove,
170                    Self::TouchMove,
171                    Self::PointerEnter,
172                    Self::GlobalPointerMove,
173                ]);
174            }
175            Self::CaptureGlobalPointerPress => {
176                events.extend([
177                    Self::MouseUp,
178                    Self::TouchEnd,
179                    Self::PointerPress,
180                    Self::GlobalPointerPress,
181                ]);
182            }
183
184            _ => {}
185        }
186
187        events
188    }
189
190    fn is_global(&self) -> bool {
191        matches!(
192            self,
193            Self::GlobalKeyDown
194                | Self::GlobalKeyUp
195                | Self::GlobalPointerPress
196                | Self::GlobalPointerDown
197                | Self::GlobalPointerMove
198                | Self::GlobalFileHover
199                | Self::GlobalFileHoverCancelled
200        )
201    }
202
203    fn is_moved(&self) -> bool {
204        matches!(
205            &self,
206            Self::MouseMove
207                | Self::TouchMove
208                | Self::CaptureGlobalPointerMove
209                | Self::GlobalPointerMove
210        )
211    }
212
213    fn does_bubble(&self) -> bool {
214        !self.is_moved()
215            && !self.is_enter()
216            && !self.is_left()
217            && !self.is_global()
218            && !self.is_capture()
219    }
220
221    fn does_go_through_solid(&self) -> bool {
222        // TODO
223        false
224    }
225
226    fn is_enter(&self) -> bool {
227        matches!(&self, Self::PointerEnter)
228    }
229
230    fn is_pressed(&self) -> bool {
231        matches!(self, Self::MouseDown | Self::PointerDown)
232    }
233
234    fn is_released(&self) -> bool {
235        matches!(&self, Self::PointerPress)
236    }
237
238    fn new_leave() -> Self {
239        Self::PointerLeave
240    }
241}