forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9ff2f83Ivan Molodetskikh1 year ago2099 commits

scroll_swipe_gesture.rs69 lines, 1.4 KiBHistoryRawDownload
1//! Swipe gesture from scroll events.
2//!
3//! Tracks when to begin, update, and end a swipe gesture from pointer axis events, also whether
4//! the gesture is vertical or horizontal. Necessary because libinput only provides touchpad swipe
5//! gesture events for 3+ fingers.
6
7#[derive(Debug)]
8pub struct ScrollSwipeGesture {
9 ongoing: bool,
10 vertical: bool,
11}
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum Action {
15 BeginUpdate,
16 Update,
17 End,
18}
19
20impl ScrollSwipeGesture {
21 pub const fn new() -> Self {
22 Self {
23 ongoing: false,
24 vertical: false,
25 }
26 }
27
28 pub fn update(&mut self, dx: f64, dy: f64) -> Action {
29 if dx == 0. && dy == 0. {
30 self.ongoing = false;
31 Action::End
32 } else if !self.ongoing {
33 self.ongoing = true;
34 self.vertical = dy != 0.;
35 Action::BeginUpdate
36 } else {
37 Action::Update
38 }
39 }
40
41 pub fn reset(&mut self) -> bool {
42 if self.ongoing {
43 self.ongoing = false;
44 true
45 } else {
46 false
47 }
48 }
49
50 pub fn is_vertical(&self) -> bool {
51 self.vertical
52 }
53}
54
55impl Default for ScrollSwipeGesture {
56 fn default() -> Self {
57 Self::new()
58 }
59}
60
61impl Action {
62 pub fn begin(self) -> bool {
63 self == Action::BeginUpdate
64 }
65
66 pub fn end(self) -> bool {
67 self == Action::End
68 }
69}