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            e => {
64                if e == other {
65                    std::cmp::Ordering::Equal
66                } else {
67                    std::cmp::Ordering::Greater
68                }
69            }
70        }
71    }
72}
73
74impl EventName {
75    /// Check if this even captures others or not
76    pub fn is_capture(&self) -> bool {
77        matches!(
78            &self,
79            Self::CaptureGlobalPointerMove | Self::CaptureGlobalPointerPress
80        )
81    }
82
83    /// Check if this is a global pointer event
84    pub fn is_global_pointer(&self) -> bool {
85        matches!(
86            self,
87            Self::GlobalPointerMove
88                | Self::GlobalPointerPress
89                | Self::GlobalPointerDown
90                | Self::CaptureGlobalPointerMove
91                | Self::CaptureGlobalPointerPress
92        )
93    }
94
95    pub fn is_left(&self) -> bool {
96        matches!(&self, Self::PointerLeave | Self::PointerOut)
97    }
98
99    pub fn is_down(&self) -> bool {
100        matches!(self, Self::PointerDown)
101    }
102
103    pub fn is_press(&self) -> bool {
104        matches!(self, Self::PointerPress)
105    }
106}
107
108impl ragnarok::NameOfEvent for EventName {
109    fn get_global_events(&self) -> HashSet<Self> {
110        match self {
111            Self::MouseUp | Self::TouchEnd => {
112                HashSet::from([Self::GlobalPointerPress, Self::CaptureGlobalPointerPress])
113            }
114            Self::MouseDown | Self::TouchStart => HashSet::from([Self::GlobalPointerDown]),
115            Self::MouseMove | Self::TouchMove => {
116                HashSet::from([Self::GlobalPointerMove, Self::CaptureGlobalPointerMove])
117            }
118
119            Self::KeyDown => HashSet::from([Self::GlobalKeyDown]),
120            Self::KeyUp => HashSet::from([Self::GlobalKeyUp]),
121
122            Self::GlobalFileHover => HashSet::from([Self::GlobalFileHover]),
123            Self::GlobalFileHoverCancelled => HashSet::from([Self::GlobalFileHoverCancelled]),
124            _ => HashSet::new(),
125        }
126    }
127
128    fn get_derived_events(&self) -> HashSet<Self> {
129        let mut events = HashSet::new();
130
131        events.insert(*self);
132
133        match self {
134            Self::MouseMove | Self::TouchMove => {
135                events.insert(Self::PointerEnter);
136                events.insert(Self::PointerOver);
137            }
138            Self::MouseDown | Self::TouchStart => {
139                events.insert(Self::PointerDown);
140            }
141            Self::MouseUp | Self::TouchEnd => {
142                events.insert(Self::PointerPress);
143            }
144            // PointerOut is synthesized as the leave event; it also derives PointerLeave
145            // so that both events are emitted when a node stops being hovered.
146            Self::PointerOut => {
147                events.insert(Self::PointerLeave);
148            }
149            _ => {}
150        }
151
152        events
153    }
154
155    fn get_cancellable_events(&self) -> HashSet<Self> {
156        let mut events = HashSet::new();
157
158        events.insert(*self);
159
160        match self {
161            Self::KeyDown => {
162                events.insert(Self::GlobalKeyDown);
163            }
164            Self::KeyUp => {
165                events.insert(Self::GlobalKeyUp);
166            }
167            Self::MouseUp | Self::TouchEnd => {
168                events.extend([Self::PointerPress, Self::GlobalPointerPress])
169            }
170            Self::PointerPress => events.extend([Self::MouseUp, Self::GlobalPointerPress]),
171            Self::MouseDown | Self::TouchStart => {
172                events.extend([Self::PointerDown, Self::GlobalPointerDown])
173            }
174            Self::PointerDown => events.extend([Self::MouseDown, Self::GlobalPointerDown]),
175            Self::CaptureGlobalPointerMove => {
176                events.extend([
177                    Self::MouseMove,
178                    Self::TouchMove,
179                    Self::PointerEnter,
180                    Self::PointerOver,
181                    Self::GlobalPointerMove,
182                ]);
183            }
184            Self::CaptureGlobalPointerPress => {
185                events.extend([
186                    Self::MouseUp,
187                    Self::TouchEnd,
188                    Self::PointerPress,
189                    Self::GlobalPointerPress,
190                ]);
191            }
192
193            _ => {}
194        }
195
196        events
197    }
198
199    fn is_global(&self) -> bool {
200        matches!(
201            self,
202            Self::GlobalKeyDown
203                | Self::GlobalKeyUp
204                | Self::GlobalPointerPress
205                | Self::GlobalPointerDown
206                | Self::GlobalPointerMove
207                | Self::GlobalFileHover
208                | Self::GlobalFileHoverCancelled
209        )
210    }
211
212    fn is_moved(&self) -> bool {
213        matches!(
214            &self,
215            Self::MouseMove
216                | Self::TouchMove
217                | Self::CaptureGlobalPointerMove
218                | Self::GlobalPointerMove
219        )
220    }
221
222    fn does_bubble(&self) -> bool {
223        (!self.is_moved()
224            && !self.is_enter()
225            && !self.is_left()
226            && !self.is_global()
227            && !self.is_capture())
228            || self.is_exclusive_enter()
229            || self.is_exclusive_leave()
230    }
231
232    fn does_go_through_solid(&self) -> bool {
233        // TODO
234        false
235    }
236
237    fn is_enter(&self) -> bool {
238        matches!(&self, Self::PointerEnter | Self::PointerOver)
239    }
240
241    fn is_pressed(&self) -> bool {
242        matches!(self, Self::MouseDown | Self::PointerDown)
243    }
244
245    fn is_released(&self) -> bool {
246        matches!(&self, Self::PointerPress)
247    }
248
249    fn is_exclusive_enter(&self) -> bool {
250        matches!(&self, Self::PointerEnter)
251    }
252
253    fn is_exclusive_leave(&self) -> bool {
254        matches!(&self, Self::PointerLeave)
255    }
256
257    fn new_leave() -> Self {
258        Self::PointerOut
259    }
260
261    fn new_exclusive_leave() -> Self {
262        Self::PointerLeave
263    }
264
265    fn new_exclusive_enter() -> Self {
266        Self::PointerEnter
267    }
268}