nuit_core/compose/view/event/
handler.rs

1use nuit_derive::Bind;
2
3use crate::{View, Node, Context, Event, IdPath};
4
5/// A view that handles events using a provided closure.
6#[derive(Debug, Clone, Bind, PartialEq, Eq)]
7pub struct Handler<T, F> {
8    wrapped: T,
9    handle_event: F
10}
11
12impl<T, F> Handler<T, F> {
13    pub fn new(wrapped: T, handle_event: F) -> Self {
14        Self {
15            wrapped,
16            handle_event,
17        }
18    }
19}
20
21impl<T, F> View for Handler<T, F> where T: View, F: Fn(Event) {
22    fn fire(&self, event: &Event, event_path: &IdPath, context: &Context) {
23        if event_path.is_root() {
24            (self.handle_event)(event.clone());
25        }
26        self.wrapped.fire(event, event_path, context);
27    }
28
29    fn render(&self, context: &Context) -> Node {
30        self.wrapped.render(&context)
31    }
32}