Skip to main content

euv_core/scene/event/
impl.rs

1use super::*;
2
3/// Safe handler lookup for `EventHandlers`.
4impl EventHandlers {
5    /// Constructs an empty `EventHandlers` table in `const` contexts without heap allocation.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Empty event handler collection with all callback slots set to `None`.
10    #[must_use]
11    pub const fn empty() -> Self {
12        Self {
13            on_click: None,
14            on_pointer_down: None,
15            on_pointer_move: None,
16            on_pointer_up: None,
17            on_pointer_enter: None,
18            on_pointer_leave: None,
19            on_scroll: None,
20            on_key_down: None,
21            on_change: None,
22            on_focus: None,
23            on_blur: None,
24        }
25    }
26
27    /// Returns `true` when zero event callbacks are registered in this collection.
28    ///
29    /// # Arguments
30    ///
31    /// - `&Self` - Event handler collection.
32    ///
33    /// # Returns
34    ///
35    /// - `bool` - Whether every handler slot is `None`.
36    #[must_use]
37    #[inline(always)]
38    pub fn is_empty(&self) -> bool {
39        let Self {
40            on_click,
41            on_pointer_down,
42            on_pointer_move,
43            on_pointer_up,
44            on_pointer_enter,
45            on_pointer_leave,
46            on_scroll,
47            on_key_down,
48            on_change,
49            on_focus,
50            on_blur,
51        } = self;
52        on_click.is_none()
53            && on_pointer_down.is_none()
54            && on_pointer_move.is_none()
55            && on_pointer_up.is_none()
56            && on_pointer_enter.is_none()
57            && on_pointer_leave.is_none()
58            && on_scroll.is_none()
59            && on_key_down.is_none()
60            && on_change.is_none()
61            && on_focus.is_none()
62            && on_blur.is_none()
63    }
64
65    /// Clones the registered handler matching `kind`, if any.
66    ///
67    /// # Arguments
68    ///
69    /// - `&Self` - Event handler collection.
70    /// - `&UiEventKind` - Event kind being dispatched.
71    ///
72    /// # Returns
73    ///
74    /// - `Option<UiEventHandler>` - Matching callback handle.
75    #[must_use]
76    pub fn handler_for(&self, kind: &UiEventKind) -> Option<UiEventHandler> {
77        let Self {
78            on_click,
79            on_pointer_down,
80            on_pointer_move,
81            on_pointer_up,
82            on_pointer_enter,
83            on_pointer_leave,
84            on_scroll,
85            on_key_down,
86            on_change,
87            on_focus,
88            on_blur,
89        } = self;
90        match kind {
91            UiEventKind::Click(_) => on_click.clone(),
92            UiEventKind::PointerDown(_) => on_pointer_down.clone(),
93            UiEventKind::PointerMove(_) => on_pointer_move.clone(),
94            UiEventKind::PointerUp(_) => on_pointer_up.clone(),
95            UiEventKind::PointerEnter => on_pointer_enter.clone(),
96            UiEventKind::PointerLeave => on_pointer_leave.clone(),
97            UiEventKind::Scroll(_) => on_scroll.clone(),
98            UiEventKind::KeyDown(_, _) => on_key_down.clone(),
99            UiEventKind::KeyUp(_, _) | UiEventKind::TextInput(_) => None,
100            UiEventKind::Change(_) => on_change.clone(),
101            UiEventKind::Focus => on_focus.clone(),
102            UiEventKind::Blur => on_blur.clone(),
103        }
104    }
105}