Implement mouse wheel bindings
Browse files at this commit4b2fdd0776485fcf9f560c72e98c00dff318f9e7Parent: 969519b4 changed files +100 -0
- src/input.rs +58 -0
- src/lib.rs +1 -0
- src/niri.rs +5 -0
- src/wheel_tracker.rs +36 -0
src/input.rsmodified+58 -0
| @@ -1081,6 +1081,64 @@ impl State { | ||
| 1081 | 1081 | let horizontal_amount_v120 = event.amount_v120(Axis::Horizontal); |
| 1082 | 1082 | let vertical_amount_v120 = event.amount_v120(Axis::Vertical); |
| 1083 | 1083 | |
| 1084 | + // Handle wheel bindings. | |
| 1085 | + if source == AxisSource::Wheel { | |
| 1086 | + let comp_mod = self.backend.mod_key(); | |
| 1087 | + let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); | |
| 1088 | + | |
| 1089 | + if let Some(v120) = horizontal_amount_v120 { | |
| 1090 | + let config = self.niri.config.borrow(); | |
| 1091 | + let bindings = &config.binds; | |
| 1092 | + let action_left = bound_action(bindings, comp_mod, Trigger::WheelLeft, mods); | |
| 1093 | + let action_right = bound_action(bindings, comp_mod, Trigger::WheelRight, mods); | |
| 1094 | + drop(config); | |
| 1095 | + | |
| 1096 | + // If we have a bind with current modifiers along the scroll direction, then | |
| 1097 | + // accumulate and don't pass to Wayland. If there's no bind, reset the accumulator. | |
| 1098 | + if action_left.is_some() || action_right.is_some() { | |
| 1099 | + let ticks = self.niri.horizontal_wheel_tracker.accumulate(v120); | |
| 1100 | + if let Some(right) = action_right { | |
| 1101 | + for _ in 0..ticks { | |
| 1102 | + self.do_action(right.clone()); | |
| 1103 | + } | |
| 1104 | + } | |
| 1105 | + if let Some(left) = action_left { | |
| 1106 | + for _ in ticks..0 { | |
| 1107 | + self.do_action(left.clone()); | |
| 1108 | + } | |
| 1109 | + } | |
| 1110 | + return; | |
| 1111 | + } else { | |
| 1112 | + self.niri.horizontal_wheel_tracker.reset(); | |
| 1113 | + } | |
| 1114 | + } | |
| 1115 | + | |
| 1116 | + if let Some(v120) = vertical_amount_v120 { | |
| 1117 | + let config = self.niri.config.borrow(); | |
| 1118 | + let bindings = &config.binds; | |
| 1119 | + let action_up = bound_action(bindings, comp_mod, Trigger::WheelUp, mods); | |
| 1120 | + let action_down = bound_action(bindings, comp_mod, Trigger::WheelDown, mods); | |
| 1121 | + drop(config); | |
| 1122 | + | |
| 1123 | + if action_up.is_some() || action_down.is_some() { | |
| 1124 | + let ticks = self.niri.vertical_wheel_tracker.accumulate(v120); | |
| 1125 | + if let Some(down) = action_down { | |
| 1126 | + for _ in 0..ticks { | |
| 1127 | + self.do_action(down.clone()); | |
| 1128 | + } | |
| 1129 | + } | |
| 1130 | + if let Some(up) = action_up { | |
| 1131 | + for _ in ticks..0 { | |
| 1132 | + self.do_action(up.clone()); | |
| 1133 | + } | |
| 1134 | + } | |
| 1135 | + return; | |
| 1136 | + } else { | |
| 1137 | + self.niri.vertical_wheel_tracker.reset(); | |
| 1138 | + } | |
| 1139 | + } | |
| 1140 | + } | |
| 1141 | + | |
| 1084 | 1142 | let horizontal_amount = event.amount(Axis::Horizontal).unwrap_or_else(|| { |
| 1085 | 1143 | // Winit backend, discrete scrolling. |
| 1086 | 1144 | horizontal_amount_v120.unwrap_or(0.0) / 120. * 15. |
src/lib.rsmodified+1 -0
| @@ -19,6 +19,7 @@ pub mod rubber_band; | ||
| 19 | 19 | pub mod swipe_tracker; |
| 20 | 20 | pub mod ui; |
| 21 | 21 | pub mod utils; |
| 22 | +pub mod wheel_tracker; | |
| 22 | 23 | pub mod window; |
| 23 | 24 | |
| 24 | 25 | #[cfg(not(feature = "xdp-gnome-screencast"))] |
src/niri.rsmodified+5 -0
| @@ -114,6 +114,7 @@ use crate::utils::spawning::CHILD_ENV; | ||
| 114 | 114 | use crate::utils::{ |
| 115 | 115 | center, center_f64, get_monotonic_time, make_screenshot_path, output_size, write_png_rgba8, |
| 116 | 116 | }; |
| 117 | +use crate::wheel_tracker::WheelTracker; | |
| 117 | 118 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped}; |
| 118 | 119 | use crate::{animation, niri_render_elements}; |
| 119 | 120 | |
| @@ -204,6 +205,8 @@ pub struct Niri { | ||
| 204 | 205 | pub pointer_focus: PointerFocus, |
| 205 | 206 | pub tablet_cursor_location: Option<Point<f64, Logical>>, |
| 206 | 207 | pub gesture_swipe_3f_cumulative: Option<(f64, f64)>, |
| 208 | + pub vertical_wheel_tracker: WheelTracker, | |
| 209 | + pub horizontal_wheel_tracker: WheelTracker, | |
| 207 | 210 | |
| 208 | 211 | pub lock_state: LockState, |
| 209 | 212 | |
| @@ -1263,6 +1266,8 @@ impl Niri { | ||
| 1263 | 1266 | pointer_focus: PointerFocus::default(), |
| 1264 | 1267 | tablet_cursor_location: None, |
| 1265 | 1268 | gesture_swipe_3f_cumulative: None, |
| 1269 | + vertical_wheel_tracker: WheelTracker::new(), | |
| 1270 | + horizontal_wheel_tracker: WheelTracker::new(), | |
| 1266 | 1271 | |
| 1267 | 1272 | lock_state: LockState::Unlocked, |
| 1268 | 1273 |
src/wheel_tracker.rsadded+36 -0
| @@ -0,0 +1,36 @@ | ||
| 1 | +pub struct WheelTracker { | |
| 2 | + last: f64, | |
| 3 | + acc: f64, | |
| 4 | +} | |
| 5 | + | |
| 6 | +impl WheelTracker { | |
| 7 | + #[allow(clippy::new_without_default)] | |
| 8 | + pub fn new() -> Self { | |
| 9 | + Self { last: 0., acc: 0. } | |
| 10 | + } | |
| 11 | + | |
| 12 | + pub fn accumulate(&mut self, amount_v120: f64) -> i8 { | |
| 13 | + let changed_direction = | |
| 14 | + (self.last > 0. && amount_v120 < 0.) || (self.last < 0. && amount_v120 > 0.); | |
| 15 | + if changed_direction { | |
| 16 | + self.acc = 0. | |
| 17 | + } | |
| 18 | + | |
| 19 | + self.last = amount_v120; | |
| 20 | + self.acc += amount_v120; | |
| 21 | + | |
| 22 | + let mut ticks = 0; | |
| 23 | + if self.acc.abs() >= 120. { | |
| 24 | + let clamped = self.acc.clamp(-127. * 120., 127. * 120.); | |
| 25 | + ticks = (clamped as i16 / 120) as i8; | |
| 26 | + self.acc %= 120.; | |
| 27 | + } | |
| 28 | + | |
| 29 | + ticks | |
| 30 | + } | |
| 31 | + | |
| 32 | + pub fn reset(&mut self) { | |
| 33 | + self.last = 0.; | |
| 34 | + self.acc = 0.; | |
| 35 | + } | |
| 36 | +} |