Skip to main content

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