nuit_core/compose/gesture/
drag.rs

1use nuit_derive::Bind;
2
3use crate::{Context, DragEvent, GestureEvent, GestureNode, IdPath};
4
5use super::Gesture;
6
7/// A gesture recognizing a drag.
8#[derive(Bind)]
9pub struct DragGesture<F> {
10    minimum_distance: f64,
11    action: F,
12}
13
14impl<F> DragGesture<F> where F: Fn(&DragEvent) {
15    /// Creates a drag gesture that executes the given action on a drag further
16    /// than the given minimum distance.
17    pub const fn new(minimum_distance: f64, action: F) -> Self {
18        Self { minimum_distance, action }
19    }
20
21    /// Creates a drag gesture with the default minimum distance.
22    pub const fn new_default(action: F) -> Self {
23        Self { minimum_distance: 10.0, action }
24    }
25}
26
27impl<F> Gesture for DragGesture<F> where F: Fn(&DragEvent) {
28    fn fire(&self, event: &GestureEvent, event_path: &IdPath, _context: &Context) {
29        assert!(event_path.is_root());
30        if let GestureEvent::Drag { drag } = event {
31            (self.action)(drag);
32        } else {
33            eprintln!("Warning: Ignoring non-drag gesture event {:?} targeted to DragGesture at {:?}", event, event_path)
34        }
35    }
36
37    fn render(&self, _context: &Context) -> GestureNode {
38        GestureNode::Drag { minimum_distance: self.minimum_distance }
39    }
40}