forge
Explore
Log in

Don't you dare go Hollow

Make vertical touchpad swipe inertial

Browse files at this commit

e0ec6e5b11bc222c252157beb064104f4c60dfbeIvan MolodetskikhParent: 93243d7
Values and implementation are heavily inspired by AdwSwipeTracker.
src/input.rsmodified+7 -1
@@ -1,5 +1,6 @@
11 use std::any::Any;
22 use std::collections::HashSet;
3+use std::time::Duration;
34
45 use niri_config::{Action, Binds, Modifiers};
56 use niri_ipc::LayoutSwitchTarget;
@@ -1242,8 +1243,13 @@ impl State {
12421243 }
12431244 }
12441245
1246+ let timestamp = Duration::from_micros(event.time());
1247+
12451248 let mut handled = false;
1246- let res = self.niri.layout.workspace_switch_gesture_update(delta_y);
1249+ let res = self
1250+ .niri
1251+ .layout
1252+ .workspace_switch_gesture_update(delta_y, timestamp);
12471253 if let Some(output) = res {
12481254 if let Some(output) = output {
12491255 self.niri.queue_redraw(output);
src/layout/mod.rsmodified+9 -4
@@ -1603,14 +1603,18 @@ impl<W: LayoutElement> Layout<W> {
16031603 }
16041604 }
16051605
1606- pub fn workspace_switch_gesture_update(&mut self, delta_y: f64) -> Option<Option<Output>> {
1606+ pub fn workspace_switch_gesture_update(
1607+ &mut self,
1608+ delta_y: f64,
1609+ timestamp: Duration,
1610+ ) -> Option<Option<Output>> {
16071611 let monitors = match &mut self.monitor_set {
16081612 MonitorSet::Normal { monitors, .. } => monitors,
16091613 MonitorSet::NoOutputs { .. } => return None,
16101614 };
16111615
16121616 for monitor in monitors {
1613- if let Some(refresh) = monitor.workspace_switch_gesture_update(delta_y) {
1617+ if let Some(refresh) = monitor.workspace_switch_gesture_update(delta_y, timestamp) {
16141618 if refresh {
16151619 return Some(Some(monitor.output.clone()));
16161620 } else {
@@ -2041,6 +2045,7 @@ mod tests {
20412045 WorkspaceSwitchGestureUpdate {
20422046 #[proptest(strategy = "-400f64..400f64")]
20432047 delta: f64,
2048+ timestamp: Duration,
20442049 },
20452050 WorkspaceSwitchGestureEnd {
20462051 cancelled: bool,
@@ -2303,8 +2308,8 @@ mod tests {
23032308
23042309 layout.workspace_switch_gesture_begin(&output);
23052310 }
2306- Op::WorkspaceSwitchGestureUpdate { delta } => {
2307- layout.workspace_switch_gesture_update(delta);
2311+ Op::WorkspaceSwitchGestureUpdate { delta, timestamp } => {
2312+ layout.workspace_switch_gesture_update(delta, timestamp);
23082313 }
23092314 Op::WorkspaceSwitchGestureEnd { cancelled } => {
23102315 layout.workspace_switch_gesture_end(cancelled);
src/layout/monitor.rsmodified+21 -10
@@ -15,6 +15,7 @@ use super::workspace::{
1515 use super::{LayoutElement, Options};
1616 use crate::animation::Animation;
1717 use crate::render_helpers::renderer::NiriRenderer;
18+use crate::swipe_tracker::SwipeTracker;
1819 use crate::utils::output_size;
1920
2021 #[derive(Debug)]
@@ -43,6 +44,7 @@ pub struct WorkspaceSwitchGesture {
4344 pub center_idx: usize,
4445 /// Current, fractional workspace index.
4546 pub current_idx: f64,
47+ pub tracker: SwipeTracker,
4648 }
4749
4850 pub type MonitorRenderElement<R> =
@@ -703,21 +705,28 @@ impl<W: LayoutElement> Monitor<W> {
703705 let gesture = WorkspaceSwitchGesture {
704706 center_idx,
705707 current_idx,
708+ tracker: SwipeTracker::new(),
706709 };
707710 self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture));
708711 }
709712
710- pub fn workspace_switch_gesture_update(&mut self, delta_y: f64) -> Option<bool> {
713+ pub fn workspace_switch_gesture_update(
714+ &mut self,
715+ delta_y: f64,
716+ timestamp: Duration,
717+ ) -> Option<bool> {
711718 let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else {
712719 return None;
713720 };
714721
722+ gesture.tracker.push(delta_y, timestamp);
723+
715724 // Normalize like GNOME Shell's workspace switching.
716- let delta_y = delta_y / 400.;
725+ let pos = gesture.tracker.pos() / 400.;
717726
718727 let min = gesture.center_idx.saturating_sub(1) as f64;
719728 let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64;
720- let new_idx = (gesture.current_idx + delta_y).clamp(min, max);
729+ let new_idx = (gesture.center_idx as f64 + pos).clamp(min, max);
721730
722731 if gesture.current_idx == new_idx {
723732 return Some(false);
@@ -738,15 +747,17 @@ impl<W: LayoutElement> Monitor<W> {
738747 return true;
739748 }
740749
741- // FIXME: keep track of gesture velocity and use it to compute the final point and to
742- // animate to it.
743- let current_idx = gesture.current_idx;
744- let idx = current_idx.round() as usize;
750+ let pos = gesture.tracker.projected_end_pos() / 400.;
745751
746- self.active_workspace_idx = idx;
752+ let min = gesture.center_idx.saturating_sub(1) as f64;
753+ let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64;
754+ let new_idx = (gesture.center_idx as f64 + pos).clamp(min, max);
755+ let new_idx = new_idx.round() as usize;
756+
757+ self.active_workspace_idx = new_idx;
747758 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
748- current_idx,
749- idx as f64,
759+ gesture.current_idx,
760+ new_idx as f64,
750761 self.options.animations.workspace_switch,
751762 niri_config::Animation::default_workspace_switch(),
752763 )));
src/lib.rsmodified+1 -0
@@ -15,6 +15,7 @@ pub mod layout;
1515 pub mod niri;
1616 pub mod protocols;
1717 pub mod render_helpers;
18+pub mod swipe_tracker;
1819 pub mod ui;
1920 pub mod utils;
2021 pub mod window;
src/swipe_tracker.rsadded+87 -0
@@ -0,0 +1,87 @@
1+use std::collections::VecDeque;
2+use std::time::Duration;
3+
4+const HISTORY_LIMIT: Duration = Duration::from_millis(150);
5+const DECELERATION_TOUCHPAD: f64 = 0.997;
6+
7+#[derive(Debug)]
8+pub struct SwipeTracker {
9+ history: VecDeque<Event>,
10+ pos: f64,
11+}
12+
13+#[derive(Debug, Clone, Copy)]
14+struct Event {
15+ delta: f64,
16+ timestamp: Duration,
17+}
18+
19+impl SwipeTracker {
20+ #[allow(clippy::new_without_default)]
21+ pub fn new() -> Self {
22+ Self {
23+ history: VecDeque::new(),
24+ pos: 0.,
25+ }
26+ }
27+
28+ /// Pushes a new reading into the tracker.
29+ pub fn push(&mut self, delta: f64, timestamp: Duration) {
30+ // For the events that we care about, timestamps should always increase
31+ // monotonically.
32+ if let Some(last) = self.history.back() {
33+ if timestamp < last.timestamp {
34+ trace!(
35+ "ignoring event with timestamp {timestamp:?} earlier than last {:?}",
36+ last.timestamp
37+ );
38+ return;
39+ }
40+ }
41+
42+ self.history.push_back(Event { delta, timestamp });
43+ self.pos += delta;
44+
45+ self.retain_recent();
46+ }
47+
48+ /// Returns the current gesture position.
49+ pub fn pos(&self) -> f64 {
50+ self.pos
51+ }
52+
53+ /// Computes the current gesture velocity.
54+ pub fn velocity(&self) -> f64 {
55+ let (Some(first), Some(last)) = (self.history.front(), self.history.back()) else {
56+ return 0.;
57+ };
58+
59+ let total_time = (last.timestamp - first.timestamp).as_secs_f64();
60+ if total_time == 0. {
61+ return 0.;
62+ }
63+
64+ let total_delta = self.history.iter().map(|event| event.delta).sum::<f64>();
65+ total_delta / total_time
66+ }
67+
68+ /// Computes the gesture end position after decelerating to a halt.
69+ pub fn projected_end_pos(&self) -> f64 {
70+ let vel = self.velocity();
71+ self.pos - vel / (1000. * DECELERATION_TOUCHPAD.ln())
72+ }
73+
74+ fn retain_recent(&mut self) {
75+ let Some(&Event { timestamp, .. }) = self.history.back() else {
76+ return;
77+ };
78+
79+ while let Some(first) = self.history.front() {
80+ if timestamp <= first.timestamp + HISTORY_LIMIT {
81+ break;
82+ }
83+
84+ let _ = self.history.pop_front();
85+ }
86+ }
87+}