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 @@
1 use std::any::Any;1 use std::any::Any;
2 use std::collections::HashSet;2 use std::collections::HashSet;
3+use std::time::Duration;
3 4
4 use niri_config::{Action, Binds, Modifiers};5 use niri_config::{Action, Binds, Modifiers};
5 use niri_ipc::LayoutSwitchTarget;6 use niri_ipc::LayoutSwitchTarget;
@@ -1242,8 +1243,13 @@ impl State {
1242 }1243 }
1243 }1244 }
1244 1245
1246+ let timestamp = Duration::from_micros(event.time());
1247+
1245 let mut handled = false;1248 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);
1247 if let Some(output) = res {1253 if let Some(output) = res {
1248 if let Some(output) = output {1254 if let Some(output) = output {
1249 self.niri.queue_redraw(output);1255 self.niri.queue_redraw(output);
src/layout/mod.rsmodified+9 -4
@@ -1603,14 +1603,18 @@ impl<W: LayoutElement> Layout<W> {
1603 }1603 }
1604 }1604 }
1605 1605
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>> {
1607 let monitors = match &mut self.monitor_set {1611 let monitors = match &mut self.monitor_set {
1608 MonitorSet::Normal { monitors, .. } => monitors,1612 MonitorSet::Normal { monitors, .. } => monitors,
1609 MonitorSet::NoOutputs { .. } => return None,1613 MonitorSet::NoOutputs { .. } => return None,
1610 };1614 };
1611 1615
1612 for monitor in monitors {1616 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) {
1614 if refresh {1618 if refresh {
1615 return Some(Some(monitor.output.clone()));1619 return Some(Some(monitor.output.clone()));
1616 } else {1620 } else {
@@ -2041,6 +2045,7 @@ mod tests {
2041 WorkspaceSwitchGestureUpdate {2045 WorkspaceSwitchGestureUpdate {
2042 #[proptest(strategy = "-400f64..400f64")]2046 #[proptest(strategy = "-400f64..400f64")]
2043 delta: f64,2047 delta: f64,
2048+ timestamp: Duration,
2044 },2049 },
2045 WorkspaceSwitchGestureEnd {2050 WorkspaceSwitchGestureEnd {
2046 cancelled: bool,2051 cancelled: bool,
@@ -2303,8 +2308,8 @@ mod tests {
2303 2308
2304 layout.workspace_switch_gesture_begin(&output);2309 layout.workspace_switch_gesture_begin(&output);
2305 }2310 }
2306- Op::WorkspaceSwitchGestureUpdate { delta } => {2311+ Op::WorkspaceSwitchGestureUpdate { delta, timestamp } => {
2307- layout.workspace_switch_gesture_update(delta);2312+ layout.workspace_switch_gesture_update(delta, timestamp);
2308 }2313 }
2309 Op::WorkspaceSwitchGestureEnd { cancelled } => {2314 Op::WorkspaceSwitchGestureEnd { cancelled } => {
2310 layout.workspace_switch_gesture_end(cancelled);2315 layout.workspace_switch_gesture_end(cancelled);
src/layout/monitor.rsmodified+21 -10
@@ -15,6 +15,7 @@ use super::workspace::{
15 use super::{LayoutElement, Options};15 use super::{LayoutElement, Options};
16 use crate::animation::Animation;16 use crate::animation::Animation;
17 use crate::render_helpers::renderer::NiriRenderer;17 use crate::render_helpers::renderer::NiriRenderer;
18+use crate::swipe_tracker::SwipeTracker;
18 use crate::utils::output_size;19 use crate::utils::output_size;
19 20
20 #[derive(Debug)]21 #[derive(Debug)]
@@ -43,6 +44,7 @@ pub struct WorkspaceSwitchGesture {
43 pub center_idx: usize,44 pub center_idx: usize,
44 /// Current, fractional workspace index.45 /// Current, fractional workspace index.
45 pub current_idx: f64,46 pub current_idx: f64,
47+ pub tracker: SwipeTracker,
46 }48 }
47 49
48 pub type MonitorRenderElement<R> =50 pub type MonitorRenderElement<R> =
@@ -703,21 +705,28 @@ impl<W: LayoutElement> Monitor<W> {
703 let gesture = WorkspaceSwitchGesture {705 let gesture = WorkspaceSwitchGesture {
704 center_idx,706 center_idx,
705 current_idx,707 current_idx,
708+ tracker: SwipeTracker::new(),
706 };709 };
707 self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture));710 self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture));
708 }711 }
709 712
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> {
711 let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else {718 let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else {
712 return None;719 return None;
713 };720 };
714 721
722+ gesture.tracker.push(delta_y, timestamp);
723+
715 // Normalize like GNOME Shell's workspace switching.724 // Normalize like GNOME Shell's workspace switching.
716- let delta_y = delta_y / 400.;725+ let pos = gesture.tracker.pos() / 400.;
717 726
718 let min = gesture.center_idx.saturating_sub(1) as f64;727 let min = gesture.center_idx.saturating_sub(1) as f64;
719 let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64;728 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);
721 730
722 if gesture.current_idx == new_idx {731 if gesture.current_idx == new_idx {
723 return Some(false);732 return Some(false);
@@ -738,15 +747,17 @@ impl<W: LayoutElement> Monitor<W> {
738 return true;747 return true;
739 }748 }
740 749
741- // FIXME: keep track of gesture velocity and use it to compute the final point and to750+ let pos = gesture.tracker.projected_end_pos() / 400.;
742- // animate to it.
743- let current_idx = gesture.current_idx;
744- let idx = current_idx.round() as usize;
745 751
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;
747 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(758 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
748- current_idx,759+ gesture.current_idx,
749- idx as f64,760+ new_idx as f64,
750 self.options.animations.workspace_switch,761 self.options.animations.workspace_switch,
751 niri_config::Animation::default_workspace_switch(),762 niri_config::Animation::default_workspace_switch(),
752 )));763 )));
src/lib.rsmodified+1 -0
@@ -15,6 +15,7 @@ pub mod layout;
15 pub mod niri;15 pub mod niri;
16 pub mod protocols;16 pub mod protocols;
17 pub mod render_helpers;17 pub mod render_helpers;
18+pub mod swipe_tracker;
18 pub mod ui;19 pub mod ui;
19 pub mod utils;20 pub mod utils;
20 pub mod window;21 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+}