Skip to main content

euv_core/event/handler/
impl.rs

1use crate::*;
2
3/// Implementation of event handler construction and invocation.
4impl NativeEventHandler {
5    /// Creates a new event handler from an `NativeEventName` enum and callback.
6    ///
7    /// # Arguments
8    ///
9    /// - `NativeEventName` - The event name enum variant.
10    /// - `FnMut(Event) + 'static` - The callback to invoke when the event fires.
11    ///
12    /// # Returns
13    ///
14    /// - `Self` - A new event handler.
15    pub fn create<F>(event_name: NativeEventName, callback: F) -> Self
16    where
17        F: FnMut(Event) + 'static,
18    {
19        let callback_inner: Rc<RefCell<NativeEventCallbackInner>> = Rc::new(RefCell::new(
20            NativeEventCallbackInner::new(Box::new(callback)),
21        ));
22        NativeEventHandler::new(event_name.as_str(), callback_inner)
23    }
24
25    /// Invokes the underlying callback with the given event.
26    ///
27    /// # Arguments
28    ///
29    /// - `Event` - The event to pass to the callback.
30    pub fn handle(&self, event: Event) {
31        let mut inner: RefMut<NativeEventCallbackInner> = self.get_callback().borrow_mut();
32        (inner.get_mut_callback())(event);
33    }
34}