Skip to main content

xrack_runtime/
swipe_gesture.rs

1use crate::{Length, Property};
2use std::{cell::Cell, fmt, rc::Rc};
3
4#[derive(Clone, Copy, Debug, Default, PartialEq)]
5pub struct SwipeSample {
6    pub pressed_x: Length,
7    pub pressed_y: Length,
8    pub current_x: Length,
9    pub current_y: Length,
10}
11
12#[derive(Clone, Default)]
13pub struct SwipeHandle(Rc<Properties>);
14
15#[derive(Default)]
16struct Properties {
17    swiping: Property<bool>,
18    pressed_x: Property<Length>,
19    pressed_y: Property<Length>,
20    current_x: Property<Length>,
21    current_y: Property<Length>,
22    cancel: Cell<bool>,
23}
24
25impl fmt::Debug for SwipeHandle {
26    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
27        formatter
28            .debug_struct("SwipeHandle")
29            .finish_non_exhaustive()
30    }
31}
32
33impl PartialEq for SwipeHandle {
34    fn eq(&self, other: &Self) -> bool {
35        Rc::ptr_eq(&self.0, &other.0)
36    }
37}
38
39impl SwipeHandle {
40    pub fn swiping(&self) -> bool {
41        self.0.swiping.get(|| false)
42    }
43    pub fn pressed_x(&self) -> Length {
44        self.0.pressed_x.get(Length::default)
45    }
46    pub fn pressed_y(&self) -> Length {
47        self.0.pressed_y.get(Length::default)
48    }
49    pub fn current_x(&self) -> Length {
50        self.0.current_x.get(Length::default)
51    }
52    pub fn current_y(&self) -> Length {
53        self.0.current_y.get(Length::default)
54    }
55
56    pub fn cancel(&self) {
57        if self.swiping() {
58            self.0.cancel.set(true);
59        }
60    }
61
62    #[doc(hidden)]
63    pub fn cancel_requested(&self) -> bool {
64        self.0.cancel.get()
65    }
66
67    #[doc(hidden)]
68    pub fn publish(&self, sample: SwipeSample, swiping: bool) {
69        if !swiping {
70            self.0.cancel.set(false);
71        }
72        self.0.pressed_x.set_if_changed(sample.pressed_x);
73        self.0.pressed_y.set_if_changed(sample.pressed_y);
74        self.0.current_x.set_if_changed(sample.current_x);
75        self.0.current_y.set_if_changed(sample.current_y);
76        self.0.swiping.set_if_changed(swiping);
77    }
78}