diff options
| author | Ivan Molodetskikh <yalterz@gmail.com> | 2025-04-25 09:48:54 +0300 |
|---|---|---|
| committer | Ivan Molodetskikh <yalterz@gmail.com> | 2025-04-25 02:00:18 -0700 |
| commit | 59de6918b3c7f3bc8e988046b3fef2cead322f9a (patch) | |
| tree | 5927c18cfe0486e0582a31cf09f0f244fe4708a2 /src/input/scroll_swipe_gesture.rs | |
| parent | bd3d55438993a898f94500bea194377fb93004f3 (diff) | |
| download | niri-59de6918b3c7f3bc8e988046b3fef2cead322f9a.tar.gz niri-59de6918b3c7f3bc8e988046b3fef2cead322f9a.tar.bz2 niri-59de6918b3c7f3bc8e988046b3fef2cead322f9a.zip | |
overview: Add two-finger touchpad scroll
Diffstat (limited to 'src/input/scroll_swipe_gesture.rs')
| -rw-r--r-- | src/input/scroll_swipe_gesture.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/input/scroll_swipe_gesture.rs b/src/input/scroll_swipe_gesture.rs new file mode 100644 index 00000000..c15ace23 --- /dev/null +++ b/src/input/scroll_swipe_gesture.rs @@ -0,0 +1,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 + } +} |
