aboutsummaryrefslogtreecommitdiff
path: root/src/input/scroll_swipe_gesture.rs
blob: c15ace23563abebe39d42e892b75a110c648af3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Swipe gesture from scroll events.
//!
//! Tracks when to begin, update, and end a swipe gesture from pointer axis events, also whether
//! the gesture is vertical or horizontal. Necessary because libinput only provides touchpad swipe
//! gesture events for 3+ fingers.

#[derive(Debug)]
pub struct ScrollSwipeGesture {
    ongoing: bool,
    vertical: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
    BeginUpdate,
    Update,
    End,
}

impl ScrollSwipeGesture {
    pub const fn new() -> Self {
        Self {
            ongoing: false,
            vertical: false,
        }
    }

    pub fn update(&mut self, dx: f64, dy: f64) -> Action {
        if dx == 0. && dy == 0. {
            self.ongoing = false;
            Action::End
        } else if !self.ongoing {
            self.ongoing = true;
            self.vertical = dy != 0.;
            Action::BeginUpdate
        } else {
            Action::Update
        }
    }

    pub fn reset(&mut self) -> bool {
        if self.ongoing {
            self.ongoing = false;
            true
        } else {
            false
        }
    }

    pub fn is_vertical(&self) -> bool {
        self.vertical
    }
}

impl Default for ScrollSwipeGesture {
    fn default() -> Self {
        Self::new()
    }
}

impl Action {
    pub fn begin(self) -> bool {
        self == Action::BeginUpdate
    }

    pub fn end(self) -> bool {
        self == Action::End
    }
}