Remake horizontal gesture to snap with inertia
Browse files at this commit2e51efd3a3d799862a24bfac2d33ec7b7413f276Parent: caea0542 changed files +135 -64
- src/input.rs +2 -4
- src/layout/workspace.rs +133 -60
src/input.rsmodified+2 -4
| @@ -1219,12 +1219,10 @@ impl State { | |||
| 1219 | let mut delta_x = event.delta_x(); | 1219 | let mut delta_x = event.delta_x(); |
| 1220 | let mut delta_y = event.delta_y(); | 1220 | let mut delta_y = event.delta_y(); |
| 1221 | 1221 | ||
| 1222 | - // FIXME: remove once X is also unaccelerated. | ||
| 1223 | - let delta_y_accel = delta_y; | ||
| 1224 | - | ||
| 1225 | if let Some(libinput_event) = | 1222 | if let Some(libinput_event) = |
| 1226 | (&event as &dyn Any).downcast_ref::<input::event::gesture::GestureSwipeUpdateEvent>() | 1223 | (&event as &dyn Any).downcast_ref::<input::event::gesture::GestureSwipeUpdateEvent>() |
| 1227 | { | 1224 | { |
| 1225 | + delta_x = libinput_event.dx_unaccelerated(); | ||
| 1228 | delta_y = libinput_event.dy_unaccelerated(); | 1226 | delta_y = libinput_event.dy_unaccelerated(); |
| 1229 | } | 1227 | } |
| 1230 | 1228 | ||
| @@ -1238,7 +1236,7 @@ impl State { | |||
| 1238 | 1236 | ||
| 1239 | if let Some((cx, cy)) = &mut self.niri.gesture_swipe_3f_cumulative { | 1237 | if let Some((cx, cy)) = &mut self.niri.gesture_swipe_3f_cumulative { |
| 1240 | *cx += delta_x; | 1238 | *cx += delta_x; |
| 1241 | - *cy += delta_y_accel; | 1239 | + *cy += delta_y; |
| 1242 | 1240 | ||
| 1243 | // Check if the gesture moved far enough to decide. Threshold copied from GNOME Shell. | 1241 | // Check if the gesture moved far enough to decide. Threshold copied from GNOME Shell. |
| 1244 | let (cx, cy) = (*cx, *cy); | 1242 | let (cx, cy) = (*cx, *cy); |
src/layout/workspace.rsmodified+133 -60
| @@ -20,6 +20,9 @@ use crate::render_helpers::renderer::NiriRenderer; | |||
| 20 | use crate::swipe_tracker::SwipeTracker; | 20 | use crate::swipe_tracker::SwipeTracker; |
| 21 | use crate::utils::output_size; | 21 | use crate::utils::output_size; |
| 22 | 22 | ||
| 23 | +/// Amount of touchpad movement to scroll the view for the width of one working area. | ||
| 24 | +const VIEW_GESTURE_WORKING_AREA_MOVEMENT: f64 = 1200.; | ||
| 25 | + | ||
| 23 | #[derive(Debug)] | 26 | #[derive(Debug)] |
| 24 | pub struct Workspace<W: LayoutElement> { | 27 | pub struct Workspace<W: LayoutElement> { |
| 25 | /// The original output of this workspace. | 28 | /// The original output of this workspace. |
| @@ -1250,92 +1253,162 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1250 | 1253 | ||
| 1251 | gesture.tracker.push(delta_x, timestamp); | 1254 | gesture.tracker.push(delta_x, timestamp); |
| 1252 | 1255 | ||
| 1253 | - let view_offset = gesture.tracker.pos() + gesture.delta_from_tracker; | 1256 | + let norm_factor = self.working_area.size.w as f64 / VIEW_GESTURE_WORKING_AREA_MOVEMENT; |
| 1257 | + let pos = gesture.tracker.pos() * norm_factor; | ||
| 1258 | + let view_offset = pos + gesture.delta_from_tracker; | ||
| 1254 | gesture.current_view_offset = view_offset; | 1259 | gesture.current_view_offset = view_offset; |
| 1255 | 1260 | ||
| 1261 | + Some(true) | ||
| 1262 | + } | ||
| 1263 | + | ||
| 1264 | + pub fn view_offset_gesture_end(&mut self, _cancelled: bool) -> bool { | ||
| 1265 | + let Some(ViewOffsetAdjustment::Gesture(gesture)) = &self.view_offset_adj else { | ||
| 1266 | + return false; | ||
| 1267 | + }; | ||
| 1268 | + | ||
| 1269 | + // We do not handle cancelling, just like GNOME Shell doesn't. For this gesture, proper | ||
| 1270 | + // cancelling would require keeping track of the original active column, and then updating | ||
| 1271 | + // it in all the right places (adding columns, removing columns, etc.) -- quite a bit of | ||
| 1272 | + // effort and bug potential. | ||
| 1273 | + | ||
| 1274 | + let norm_factor = self.working_area.size.w as f64 / VIEW_GESTURE_WORKING_AREA_MOVEMENT; | ||
| 1275 | + let pos = gesture.tracker.pos() * norm_factor; | ||
| 1276 | + let current_view_offset = pos + gesture.delta_from_tracker; | ||
| 1277 | + | ||
| 1256 | if self.columns.is_empty() { | 1278 | if self.columns.is_empty() { |
| 1257 | - return Some(true); | 1279 | + self.view_offset = current_view_offset.round() as i32; |
| 1280 | + self.view_offset_adj = None; | ||
| 1281 | + return true; | ||
| 1258 | } | 1282 | } |
| 1259 | 1283 | ||
| 1260 | - // Check if moving too slow to switch focus. | 1284 | + // Figure out where the gesture would stop after deceleration. |
| 1261 | - if delta_x.abs() < 1. { | 1285 | + let end_pos = gesture.tracker.projected_end_pos() * norm_factor; |
| 1262 | - return Some(true); | 1286 | + let target_view_offset = end_pos + gesture.delta_from_tracker; |
| 1287 | + | ||
| 1288 | + // FIXME: the following logic needs to be different for center column = always. | ||
| 1289 | + | ||
| 1290 | + // Compute the snapping points. These are where the view aligns with column boundaries on | ||
| 1291 | + // either side. | ||
| 1292 | + struct Snap { | ||
| 1293 | + // View position relative to x = 0 (the first column). | ||
| 1294 | + view_pos: i32, | ||
| 1295 | + // Column to activate for this snapping point. | ||
| 1296 | + col_idx: usize, | ||
| 1263 | } | 1297 | } |
| 1264 | 1298 | ||
| 1265 | - // Switch focus to the furthest visible window towards the gesture direction. | 1299 | + let mut snapping_points = Vec::new(); |
| 1266 | 1300 | ||
| 1267 | - // Make an iterator over column indices towards the gesture direction. | 1301 | + let view_width = self.view_size.w; |
| 1268 | - let mut idxs_forward = 0..self.columns.len(); | 1302 | + let mut push = |col_idx, left, right| { |
| 1269 | - let mut idxs_back = idxs_forward.clone().rev(); | 1303 | + snapping_points.push(Snap { |
| 1270 | - let idxs = if delta_x < 0. { | 1304 | + view_pos: left, |
| 1271 | - &mut idxs_back as &mut dyn Iterator<Item = usize> | 1305 | + col_idx, |
| 1272 | - } else { | 1306 | + }); |
| 1273 | - &mut idxs_forward as &mut dyn Iterator<Item = usize> | 1307 | + snapping_points.push(Snap { |
| 1308 | + view_pos: right - view_width, | ||
| 1309 | + col_idx, | ||
| 1310 | + }); | ||
| 1274 | }; | 1311 | }; |
| 1275 | 1312 | ||
| 1276 | - let active_col_x = self.column_x(self.active_column_idx); | 1313 | + let left_strut = self.working_area.loc.x; |
| 1277 | - let mut new_col_idx = self.active_column_idx; | 1314 | + let right_strut = self.view_size.w - self.working_area.size.w - self.working_area.loc.x; |
| 1278 | - let mut new_col_x = active_col_x; | ||
| 1279 | 1315 | ||
| 1280 | - for col_idx in idxs { | 1316 | + let mut col_x = 0; |
| 1281 | - let col = &self.columns[col_idx]; | 1317 | + for (col_idx, col) in self.columns.iter().enumerate() { |
| 1282 | - let col_x = self.column_x(col_idx); | ||
| 1283 | let col_w = col.width(); | 1318 | let col_w = col.width(); |
| 1284 | 1319 | ||
| 1285 | - let mut area_for_col = if col.is_fullscreen { | 1320 | + // Normal columns align with the working area, but fullscreen columns align with the |
| 1286 | - Rectangle::from_loc_and_size((0, 0), self.view_size) | 1321 | + // view size. |
| 1322 | + if col.is_fullscreen { | ||
| 1323 | + let left = col_x; | ||
| 1324 | + let right = col_x + col_w; | ||
| 1325 | + push(col_idx, left, right); | ||
| 1287 | } else { | 1326 | } else { |
| 1288 | - self.working_area | 1327 | + // Logic from compute_new_view_offset. |
| 1289 | - }; | 1328 | + let padding = ((self.working_area.size.w - col_w) / 2).clamp(0, self.options.gaps); |
| 1290 | - | 1329 | + let left = col_x - padding - left_strut; |
| 1291 | - area_for_col.loc.x += active_col_x + view_offset.round() as i32; | 1330 | + let right = col_x + col_w + padding + right_strut; |
| 1292 | - | 1331 | + push(col_idx, left, right); |
| 1293 | - // Check if the column is already past the working area. | ||
| 1294 | - if (delta_x >= 0. && area_for_col.loc.x + area_for_col.size.w <= col_x) | ||
| 1295 | - || (delta_x < 0. && col_x + col_w <= area_for_col.loc.x) | ||
| 1296 | - { | ||
| 1297 | - break; | ||
| 1298 | } | 1332 | } |
| 1299 | 1333 | ||
| 1300 | - new_col_idx = col_idx; | 1334 | + col_x += col_w + self.options.gaps; |
| 1301 | - new_col_x = col_x; | ||
| 1302 | } | 1335 | } |
| 1303 | 1336 | ||
| 1304 | - let Some(ViewOffsetAdjustment::Gesture(gesture)) = &mut self.view_offset_adj else { | 1337 | + // Find the closest snapping point. |
| 1305 | - unreachable!(); | 1338 | + snapping_points.sort_by_key(|snap| snap.view_pos); |
| 1306 | - }; | ||
| 1307 | - | ||
| 1308 | - let delta = (active_col_x - new_col_x) as f64; | ||
| 1309 | - gesture.delta_from_tracker += delta; | ||
| 1310 | - gesture.current_view_offset += delta; | ||
| 1311 | - self.active_column_idx = new_col_idx; | ||
| 1312 | 1339 | ||
| 1313 | - Some(true) | 1340 | + let active_col_x = self.column_x(self.active_column_idx); |
| 1314 | - } | 1341 | + let target_view_pos = (active_col_x as f64 + target_view_offset).round() as i32; |
| 1342 | + let target_snap = snapping_points | ||
| 1343 | + .iter() | ||
| 1344 | + .min_by_key(|snap| snap.view_pos.abs_diff(target_view_pos)) | ||
| 1345 | + .unwrap(); | ||
| 1315 | 1346 | ||
| 1316 | - pub fn view_offset_gesture_end(&mut self, _cancelled: bool) -> bool { | 1347 | + let mut new_col_idx = target_snap.col_idx; |
| 1317 | - let Some(ViewOffsetAdjustment::Gesture(gesture)) = &self.view_offset_adj else { | ||
| 1318 | - return false; | ||
| 1319 | - }; | ||
| 1320 | 1348 | ||
| 1321 | - // We do not handle cancelling, just like GNOME Shell doesn't. For this gesture, proper | 1349 | + // Focus the furthest window towards the direction of the gesture. |
| 1322 | - // cancelling would require keeping track of the original active column, and then updating | 1350 | + if target_view_offset >= current_view_offset { |
| 1323 | - // it in all the right places (adding columns, removing columns, etc.) -- quite a bit of | 1351 | + for col_idx in (new_col_idx + 1)..self.columns.len() { |
| 1324 | - // effort and bug potential. | 1352 | + let col = &self.columns[col_idx]; |
| 1353 | + let col_x = self.column_x(col_idx); | ||
| 1354 | + let col_w = col.width(); | ||
| 1325 | 1355 | ||
| 1326 | - // FIXME: keep track of gesture velocity and use it to compute the final point and | 1356 | + if col.is_fullscreen { |
| 1327 | - // to animate to it. | 1357 | + if target_snap.view_pos + self.view_size.w < col_x + col_w { |
| 1328 | - let offset = gesture.tracker.pos() + gesture.delta_from_tracker; | 1358 | + break; |
| 1329 | - let offset = offset.round() as i32; | 1359 | + } |
| 1360 | + } else { | ||
| 1361 | + let padding = | ||
| 1362 | + ((self.working_area.size.w - col_w) / 2).clamp(0, self.options.gaps); | ||
| 1363 | + if target_snap.view_pos + left_strut + self.working_area.size.w | ||
| 1364 | + < col_x + col_w + padding | ||
| 1365 | + { | ||
| 1366 | + break; | ||
| 1367 | + } | ||
| 1368 | + } | ||
| 1330 | 1369 | ||
| 1331 | - self.view_offset = offset; | 1370 | + new_col_idx = col_idx; |
| 1332 | - self.view_offset_adj = None; | 1371 | + } |
| 1372 | + } else { | ||
| 1373 | + for col_idx in (0..new_col_idx).rev() { | ||
| 1374 | + let col = &self.columns[col_idx]; | ||
| 1375 | + let col_x = self.column_x(col_idx); | ||
| 1376 | + let col_w = col.width(); | ||
| 1377 | + | ||
| 1378 | + if col.is_fullscreen { | ||
| 1379 | + if col_x < target_snap.view_pos { | ||
| 1380 | + break; | ||
| 1381 | + } | ||
| 1382 | + } else { | ||
| 1383 | + let padding = | ||
| 1384 | + ((self.working_area.size.w - col_w) / 2).clamp(0, self.options.gaps); | ||
| 1385 | + if col_x - padding < target_snap.view_pos + left_strut { | ||
| 1386 | + break; | ||
| 1387 | + } | ||
| 1388 | + } | ||
| 1333 | 1389 | ||
| 1334 | - if !self.columns.is_empty() { | 1390 | + new_col_idx = col_idx; |
| 1335 | - let current_x = self.view_pos(); | 1391 | + } |
| 1336 | - self.animate_view_offset_to_column(current_x, self.active_column_idx, None); | ||
| 1337 | } | 1392 | } |
| 1338 | 1393 | ||
| 1394 | + let new_col_x = self.column_x(new_col_idx); | ||
| 1395 | + let delta = (active_col_x - new_col_x) as f64; | ||
| 1396 | + self.view_offset = (current_view_offset + delta).round() as i32; | ||
| 1397 | + self.active_column_idx = new_col_idx; | ||
| 1398 | + | ||
| 1399 | + let target_view_offset = target_snap.view_pos - new_col_x; | ||
| 1400 | + | ||
| 1401 | + self.view_offset_adj = Some(ViewOffsetAdjustment::Animation(Animation::new( | ||
| 1402 | + current_view_offset + delta, | ||
| 1403 | + target_view_offset as f64, | ||
| 1404 | + self.options.animations.horizontal_view_movement, | ||
| 1405 | + niri_config::Animation::default_horizontal_view_movement(), | ||
| 1406 | + ))); | ||
| 1407 | + | ||
| 1408 | + // HACK: deal with things like snapping to the right edge of a larger-than-view window and | ||
| 1409 | + // center column = always. | ||
| 1410 | + self.animate_view_offset_to_column(self.view_pos(), new_col_idx, None); | ||
| 1411 | + | ||
| 1339 | true | 1412 | true |
| 1340 | } | 1413 | } |
| 1341 | } | 1414 | } |