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_cancel: None,
18            on_pointer_enter: None,
19            on_pointer_leave: None,
20            on_scroll: None,
21            on_key_down: None,
22            on_change: None,
23            on_focus: None,
24            on_blur: None,
25        }
26    }
27
28    /// Returns `true` when zero event callbacks are registered in this collection.
29    ///
30    /// # Arguments
31    ///
32    /// - `&Self` - Event handler collection.
33    ///
34    /// # Returns
35    ///
36    /// - `bool` - Whether every handler slot is `None`.
37    #[must_use]
38    #[inline(always)]
39    pub fn is_empty(&self) -> bool {
40        let Self {
41            on_click,
42            on_pointer_down,
43            on_pointer_move,
44            on_pointer_up,
45            on_pointer_cancel,
46            on_pointer_enter,
47            on_pointer_leave,
48            on_scroll,
49            on_key_down,
50            on_change,
51            on_focus,
52            on_blur,
53        } = self;
54        on_click.is_none()
55            && on_pointer_down.is_none()
56            && on_pointer_move.is_none()
57            && on_pointer_up.is_none()
58            && on_pointer_cancel.is_none()
59            && on_pointer_enter.is_none()
60            && on_pointer_leave.is_none()
61            && on_scroll.is_none()
62            && on_key_down.is_none()
63            && on_change.is_none()
64            && on_focus.is_none()
65            && on_blur.is_none()
66    }
67
68    /// Clones the registered handler matching `kind`, if any.
69    ///
70    /// # Arguments
71    ///
72    /// - `&Self` - Event handler collection.
73    /// - `&UiEventKind` - Event kind being dispatched.
74    ///
75    /// # Returns
76    ///
77    /// - `Option<UiEventHandler>` - Matching callback handle.
78    #[must_use]
79    pub fn handler_for(&self, kind: &UiEventKind) -> Option<UiEventHandler> {
80        let Self {
81            on_click,
82            on_pointer_down,
83            on_pointer_move,
84            on_pointer_up,
85            on_pointer_cancel,
86            on_pointer_enter,
87            on_pointer_leave,
88            on_scroll,
89            on_key_down,
90            on_change,
91            on_focus,
92            on_blur,
93        } = self;
94        match kind {
95            UiEventKind::Click(_) => on_click.clone(),
96            UiEventKind::PointerDown(_) => on_pointer_down.clone(),
97            UiEventKind::PointerMove(_) => on_pointer_move.clone(),
98            UiEventKind::PointerUp(_) => on_pointer_up.clone(),
99            UiEventKind::PointerCancel => on_pointer_cancel.clone(),
100            UiEventKind::PointerEnter => on_pointer_enter.clone(),
101            UiEventKind::PointerLeave => on_pointer_leave.clone(),
102            UiEventKind::Scroll(_) => on_scroll.clone(),
103            UiEventKind::KeyDown(_, _) => on_key_down.clone(),
104            UiEventKind::KeyUp(_, _) | UiEventKind::TextInput(_) => None,
105            UiEventKind::Change(_) => on_change.clone(),
106            UiEventKind::Focus => on_focus.clone(),
107            UiEventKind::Blur => on_blur.clone(),
108        }
109    }
110}