aboutsummaryrefslogtreecommitdiff
path: root/src/layout
diff options
context:
space:
mode:
authorIvan Molodetskikh <yalterz@gmail.com>2024-03-02 14:33:22 +0400
committerIvan Molodetskikh <yalterz@gmail.com>2024-03-02 14:33:22 +0400
commite0ec6e5b11bc222c252157beb064104f4c60dfbe (patch)
tree04fd4807424fa75b1dd36e87f9e3bc5b94d91c05 /src/layout
parent93243d77728c3a0f7d314ed916b5f1a273861990 (diff)
downloadniri-e0ec6e5b11bc222c252157beb064104f4c60dfbe.tar.gz
niri-e0ec6e5b11bc222c252157beb064104f4c60dfbe.tar.bz2
niri-e0ec6e5b11bc222c252157beb064104f4c60dfbe.zip
Make vertical touchpad swipe inertial
Values and implementation are heavily inspired by AdwSwipeTracker.
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/mod.rs13
-rw-r--r--src/layout/monitor.rs31
2 files changed, 30 insertions, 14 deletions
diff --git a/src/layout/mod.rs b/src/layout/mod.rs
index fb86ccac..6ec3ca8c 100644
--- a/src/layout/mod.rs
+++ b/src/layout/mod.rs
@@ -1603,14 +1603,18 @@ impl<W: LayoutElement> Layout<W> {
}
}
- pub fn workspace_switch_gesture_update(&mut self, delta_y: f64) -> Option<Option<Output>> {
+ pub fn workspace_switch_gesture_update(
+ &mut self,
+ delta_y: f64,
+ timestamp: Duration,
+ ) -> Option<Option<Output>> {
let monitors = match &mut self.monitor_set {
MonitorSet::Normal { monitors, .. } => monitors,
MonitorSet::NoOutputs { .. } => return None,
};
for monitor in monitors {
- if let Some(refresh) = monitor.workspace_switch_gesture_update(delta_y) {
+ if let Some(refresh) = monitor.workspace_switch_gesture_update(delta_y, timestamp) {
if refresh {
return Some(Some(monitor.output.clone()));
} else {
@@ -2041,6 +2045,7 @@ mod tests {
WorkspaceSwitchGestureUpdate {
#[proptest(strategy = "-400f64..400f64")]
delta: f64,
+ timestamp: Duration,
},
WorkspaceSwitchGestureEnd {
cancelled: bool,
@@ -2303,8 +2308,8 @@ mod tests {
layout.workspace_switch_gesture_begin(&output);
}
- Op::WorkspaceSwitchGestureUpdate { delta } => {
- layout.workspace_switch_gesture_update(delta);
+ Op::WorkspaceSwitchGestureUpdate { delta, timestamp } => {
+ layout.workspace_switch_gesture_update(delta, timestamp);
}
Op::WorkspaceSwitchGestureEnd { cancelled } => {
layout.workspace_switch_gesture_end(cancelled);
diff --git a/src/layout/monitor.rs b/src/layout/monitor.rs
index 70a6981d..ee02a204 100644
--- a/src/layout/monitor.rs
+++ b/src/layout/monitor.rs
@@ -15,6 +15,7 @@ use super::workspace::{
use super::{LayoutElement, Options};
use crate::animation::Animation;
use crate::render_helpers::renderer::NiriRenderer;
+use crate::swipe_tracker::SwipeTracker;
use crate::utils::output_size;
#[derive(Debug)]
@@ -43,6 +44,7 @@ pub struct WorkspaceSwitchGesture {
pub center_idx: usize,
/// Current, fractional workspace index.
pub current_idx: f64,
+ pub tracker: SwipeTracker,
}
pub type MonitorRenderElement<R> =
@@ -703,21 +705,28 @@ impl<W: LayoutElement> Monitor<W> {
let gesture = WorkspaceSwitchGesture {
center_idx,
current_idx,
+ tracker: SwipeTracker::new(),
};
self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture));
}
- pub fn workspace_switch_gesture_update(&mut self, delta_y: f64) -> Option<bool> {
+ pub fn workspace_switch_gesture_update(
+ &mut self,
+ delta_y: f64,
+ timestamp: Duration,
+ ) -> Option<bool> {
let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else {
return None;
};
+ gesture.tracker.push(delta_y, timestamp);
+
// Normalize like GNOME Shell's workspace switching.
- let delta_y = delta_y / 400.;
+ let pos = gesture.tracker.pos() / 400.;
let min = gesture.center_idx.saturating_sub(1) as f64;
let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64;
- let new_idx = (gesture.current_idx + delta_y).clamp(min, max);
+ let new_idx = (gesture.center_idx as f64 + pos).clamp(min, max);
if gesture.current_idx == new_idx {
return Some(false);
@@ -738,15 +747,17 @@ impl<W: LayoutElement> Monitor<W> {
return true;
}
- // FIXME: keep track of gesture velocity and use it to compute the final point and to
- // animate to it.
- let current_idx = gesture.current_idx;
- let idx = current_idx.round() as usize;
+ let pos = gesture.tracker.projected_end_pos() / 400.;
- self.active_workspace_idx = idx;
+ let min = gesture.center_idx.saturating_sub(1) as f64;
+ let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64;
+ let new_idx = (gesture.center_idx as f64 + pos).clamp(min, max);
+ let new_idx = new_idx.round() as usize;
+
+ self.active_workspace_idx = new_idx;
self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
- current_idx,
- idx as f64,
+ gesture.current_idx,
+ new_idx as f64,
self.options.animations.workspace_switch,
niri_config::Animation::default_workspace_switch(),
)));