nuit_core/compose/gesture/
tap.rs

1use nuit_derive::Bind;
2
3use crate::{Context, Gesture, GestureEvent, GestureNode, IdPath};
4
5/// A gesture recognizing a tap.
6#[derive(Bind)]
7pub struct TapGesture<F> {
8    count: usize,
9    action: F,
10}
11
12impl<F> TapGesture<F> where F: Fn() {
13    /// Creates a tap gesture that executes the given action upon recognizing
14    /// the given number of taps.
15    pub const fn new(count: usize, action: F) -> Self {
16        Self { count, action }
17    }
18
19    /// Creates a tap gesture that executes the given action after recognizing a
20    /// single tap.
21    pub const fn new_single(action: F) -> Self {
22        Self::new(1, action)
23    }
24}
25
26impl<F> Gesture for TapGesture<F> where F: Fn() {
27    fn fire(&self, event: &GestureEvent, event_path: &IdPath, _context: &Context) {
28        assert!(event_path.is_root());
29        if let GestureEvent::Tap {} = event {
30            (self.action)();
31        } else {
32            eprintln!("Warning: Ignoring non-tap gesture event {:?} targeted to TapGesture at {:?}", event, event_path)
33        }
34    }
35
36    fn render(&self, _context: &Context) -> GestureNode {
37        GestureNode::Tap { count: self.count }
38    }
39}