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