nuit_core/compose/view/wrapper/
gestured.rs

1use nuit_derive::Bind;
2
3use crate::{Context, Event, Gesture, Id, IdPath, IdentifyExt, Node, View};
4
5/// A view recognizing a gesture.
6#[derive(Debug, Clone, PartialEq, Bind)]
7pub struct Gestured<T, G> {
8    wrapped: T,
9    gesture: G,
10}
11
12impl<T, G> Gestured<T, G> {
13    pub fn new(wrapped: T, gesture: G) -> Self {
14        Self { wrapped, gesture }
15    }
16}
17
18impl<T, G> View for Gestured<T, G> where T: View, G: Gesture {
19    fn fire(&self, event: &Event, event_path: &IdPath, context: &Context) {
20        if let Some(head) = event_path.head() {
21            match head {
22                Id::Index(0) => self.wrapped.fire(event, event_path.tail(), &context.child(0)),
23                Id::Index(1) => match event {
24                    Event::Gesture { gesture } => self.gesture.fire(gesture, event_path.tail(), &context.child(1)),
25                    _ => eprintln!("Warning: Non-gesture event {:?} targeted to id path {:?} in a gesture is ignored", event, event_path),
26                },
27                i => panic!("Cannot fire event for child id {} on Gestured, which has two childs", i),
28            }
29        }
30    }
31
32    fn render(&self, context: &Context) -> Node {
33        Node::Gestured {
34            wrapped: Box::new(self.wrapped.render(&context.child(0)).identify(0)),
35            gesture: self.gesture.render(&context.child(1)).identify(1),
36        }
37    }
38}