Implement rubber banding for the vertical gesture
Browse files at this commitf9127616b02eb12b462aca3e48fa27bf0479a4b0Parent: ae89b2e3 changed files +59 -3
- src/layout/monitor.rs +19 -3
- src/lib.rs +1 -0
- src/rubber_band.rs +39 -0
src/layout/monitor.rsmodified+19 -3
| @@ -15,12 +15,18 @@ use super::workspace::{ | ||
| 15 | 15 | use super::{LayoutElement, Options}; |
| 16 | 16 | use crate::animation::Animation; |
| 17 | 17 | use crate::render_helpers::renderer::NiriRenderer; |
| 18 | +use crate::rubber_band::RubberBand; | |
| 18 | 19 | use crate::swipe_tracker::SwipeTracker; |
| 19 | 20 | use crate::utils::output_size; |
| 20 | 21 | |
| 21 | 22 | /// Amount of touchpad movement to scroll the height of one workspace. |
| 22 | 23 | const WORKSPACE_GESTURE_MOVEMENT: f64 = 300.; |
| 23 | 24 | |
| 25 | +const WORKSPACE_GESTURE_RUBBER_BAND: RubberBand = RubberBand { | |
| 26 | + stiffness: 0.5, | |
| 27 | + limit: 0.05, | |
| 28 | +}; | |
| 29 | + | |
| 24 | 30 | #[derive(Debug)] |
| 25 | 31 | pub struct Monitor<W: LayoutElement> { |
| 26 | 32 | /// Output for this monitor. |
| @@ -762,7 +768,8 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 762 | 768 | |
| 763 | 769 | let min = gesture.center_idx.saturating_sub(1) as f64; |
| 764 | 770 | let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64; |
| 765 | - let new_idx = (gesture.center_idx as f64 + pos).clamp(min, max); | |
| 771 | + let new_idx = gesture.center_idx as f64 + pos; | |
| 772 | + let new_idx = WORKSPACE_GESTURE_RUBBER_BAND.clamp(min, max, new_idx); | |
| 766 | 773 | |
| 767 | 774 | if gesture.current_idx == new_idx { |
| 768 | 775 | return Some(false); |
| @@ -783,14 +790,23 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 783 | 790 | return true; |
| 784 | 791 | } |
| 785 | 792 | |
| 786 | - let velocity = gesture.tracker.velocity() / WORKSPACE_GESTURE_MOVEMENT; | |
| 793 | + let mut velocity = gesture.tracker.velocity() / WORKSPACE_GESTURE_MOVEMENT; | |
| 794 | + let current_pos = gesture.tracker.pos() / WORKSPACE_GESTURE_MOVEMENT; | |
| 787 | 795 | let pos = gesture.tracker.projected_end_pos() / WORKSPACE_GESTURE_MOVEMENT; |
| 788 | 796 | |
| 789 | 797 | let min = gesture.center_idx.saturating_sub(1) as f64; |
| 790 | 798 | let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64; |
| 791 | - let new_idx = (gesture.center_idx as f64 + pos).clamp(min, max); | |
| 799 | + let new_idx = gesture.center_idx as f64 + pos; | |
| 800 | + | |
| 801 | + let new_idx = WORKSPACE_GESTURE_RUBBER_BAND.clamp(min, max, new_idx); | |
| 792 | 802 | let new_idx = new_idx.round() as usize; |
| 793 | 803 | |
| 804 | + velocity *= WORKSPACE_GESTURE_RUBBER_BAND.clamp_derivative( | |
| 805 | + min, | |
| 806 | + max, | |
| 807 | + gesture.center_idx as f64 + current_pos, | |
| 808 | + ); | |
| 809 | + | |
| 794 | 810 | self.active_workspace_idx = new_idx; |
| 795 | 811 | self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new( |
| 796 | 812 | gesture.current_idx, |
src/lib.rsmodified+1 -0
| @@ -15,6 +15,7 @@ pub mod layout; | ||
| 15 | 15 | pub mod niri; |
| 16 | 16 | pub mod protocols; |
| 17 | 17 | pub mod render_helpers; |
| 18 | +pub mod rubber_band; | |
| 18 | 19 | pub mod swipe_tracker; |
| 19 | 20 | pub mod ui; |
| 20 | 21 | pub mod utils; |
src/rubber_band.rsadded+39 -0
| @@ -0,0 +1,39 @@ | ||
| 1 | +#[derive(Debug, Clone, Copy)] | |
| 2 | +pub struct RubberBand { | |
| 3 | + pub stiffness: f64, | |
| 4 | + pub limit: f64, | |
| 5 | +} | |
| 6 | + | |
| 7 | +impl RubberBand { | |
| 8 | + pub fn band(&self, x: f64) -> f64 { | |
| 9 | + let c = self.stiffness; | |
| 10 | + let d = self.limit; | |
| 11 | + | |
| 12 | + (1. - (1. / (x * c / d + 1.))) * d | |
| 13 | + } | |
| 14 | + | |
| 15 | + pub fn derivative(&self, x: f64) -> f64 { | |
| 16 | + let c = self.stiffness; | |
| 17 | + let d = self.limit; | |
| 18 | + | |
| 19 | + c * d * d / (c * x + d).powi(2) | |
| 20 | + } | |
| 21 | + | |
| 22 | + pub fn clamp(&self, min: f64, max: f64, x: f64) -> f64 { | |
| 23 | + let clamped = x.clamp(min, max); | |
| 24 | + let sign = if x < clamped { -1. } else { 1. }; | |
| 25 | + let diff = (x - clamped).abs(); | |
| 26 | + | |
| 27 | + clamped + sign * self.band(diff) | |
| 28 | + } | |
| 29 | + | |
| 30 | + pub fn clamp_derivative(&self, min: f64, max: f64, x: f64) -> f64 { | |
| 31 | + if min <= x && x <= max { | |
| 32 | + return 1.; | |
| 33 | + } | |
| 34 | + | |
| 35 | + let clamped = x.clamp(min, max); | |
| 36 | + let diff = (x - clamped).abs(); | |
| 37 | + self.derivative(diff) | |
| 38 | + } | |
| 39 | +} |