Implement minimizing view movement on changes
Browse files at this commit219ca08836adcd91f42ca24ec20f643af17dd270Parent: c8b85b82 changed files +68 -7
- src/animation.rs +4 -0
- src/layout.rs +64 -7
src/animation.rsmodified+4 -0
| @@ -46,4 +46,8 @@ impl Animation { | ||
| 46 | 46 | let x = (passed / total).clamp(0., 1.); |
| 47 | 47 | EaseOutCubic.y(x) * (self.to - self.from) + self.from |
| 48 | 48 | } |
| 49 | + | |
| 50 | + pub fn to(&self) -> f64 { | |
| 51 | + self.to | |
| 52 | + } | |
| 49 | 53 | } |
src/layout.rsmodified+64 -7
| @@ -1340,14 +1340,21 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 1340 | 1340 | |
| 1341 | 1341 | self.active_column_idx = idx; |
| 1342 | 1342 | |
| 1343 | - self.view_offset = 0; | |
| 1344 | - let new_x = self.view_pos(); | |
| 1345 | - | |
| 1343 | + let new_x = self.column_x(idx) - PADDING; | |
| 1344 | + let new_view_offset = compute_new_view_offset( | |
| 1345 | + current_x, | |
| 1346 | + self.view_size.w, | |
| 1347 | + new_x, | |
| 1348 | + self.columns[idx].size().w, | |
| 1349 | + ); | |
| 1350 | + | |
| 1351 | + let from_view_offset = current_x - new_x; | |
| 1346 | 1352 | self.view_offset_anim = Some(Animation::new( |
| 1347 | - (current_x - new_x) as f64, | |
| 1348 | - 0., | |
| 1353 | + from_view_offset as f64, | |
| 1354 | + new_view_offset as f64, | |
| 1349 | 1355 | Duration::from_millis(250), |
| 1350 | 1356 | )); |
| 1357 | + self.view_offset = from_view_offset; | |
| 1351 | 1358 | } |
| 1352 | 1359 | |
| 1353 | 1360 | fn has_windows(&self) -> bool { |
| @@ -1419,12 +1426,41 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 1419 | 1426 | } |
| 1420 | 1427 | |
| 1421 | 1428 | fn update_window(&mut self, window: &W) { |
| 1422 | - let column = self | |
| 1429 | + let (idx, column) = self | |
| 1423 | 1430 | .columns |
| 1424 | 1431 | .iter_mut() |
| 1425 | - .find(|col| col.contains(window)) | |
| 1432 | + .enumerate() | |
| 1433 | + .find(|(_, col)| col.contains(window)) | |
| 1426 | 1434 | .unwrap(); |
| 1427 | 1435 | column.update_window_sizes(self.view_size); |
| 1436 | + | |
| 1437 | + if idx == self.active_column_idx { | |
| 1438 | + // We might need to move the view to ensure the resized window is still visible. | |
| 1439 | + let current_x = self.view_pos(); | |
| 1440 | + let new_x = self.column_x(idx) - PADDING; | |
| 1441 | + | |
| 1442 | + let new_view_offset = compute_new_view_offset( | |
| 1443 | + current_x, | |
| 1444 | + self.view_size.w, | |
| 1445 | + new_x, | |
| 1446 | + self.columns[idx].size().w, | |
| 1447 | + ); | |
| 1448 | + | |
| 1449 | + let cur_view_offset = self | |
| 1450 | + .view_offset_anim | |
| 1451 | + .as_ref() | |
| 1452 | + .map(|a| a.to().round() as i32) | |
| 1453 | + .unwrap_or(self.view_offset); | |
| 1454 | + if cur_view_offset != new_view_offset { | |
| 1455 | + let from_view_offset = current_x - new_x; | |
| 1456 | + self.view_offset_anim = Some(Animation::new( | |
| 1457 | + from_view_offset as f64, | |
| 1458 | + new_view_offset as f64, | |
| 1459 | + Duration::from_millis(250), | |
| 1460 | + )); | |
| 1461 | + self.view_offset = from_view_offset; | |
| 1462 | + } | |
| 1463 | + } | |
| 1428 | 1464 | } |
| 1429 | 1465 | |
| 1430 | 1466 | fn activate_window(&mut self, window: &W) { |
| @@ -1974,6 +2010,27 @@ pub fn configure_new_window(view_size: Size<i32, Logical>, window: &Window) { | ||
| 1974 | 2010 | }); |
| 1975 | 2011 | } |
| 1976 | 2012 | |
| 2013 | +fn compute_new_view_offset(cur_x: i32, view_width: i32, new_x: i32, new_col_width: i32) -> i32 { | |
| 2014 | + // If the column is wider than the view, always left-align it. | |
| 2015 | + if new_col_width + PADDING * 2 >= view_width { | |
| 2016 | + return 0; | |
| 2017 | + } | |
| 2018 | + | |
| 2019 | + // If the column is already fully visible, leave the view as is. | |
| 2020 | + if new_x >= cur_x && new_x + new_col_width + PADDING * 2 <= cur_x + view_width { | |
| 2021 | + return -(new_x - cur_x); | |
| 2022 | + } | |
| 2023 | + | |
| 2024 | + // Otherwise, prefer the aligment that results in less motion from the current position. | |
| 2025 | + let dist_to_left = cur_x.abs_diff(new_x); | |
| 2026 | + let dist_to_right = (cur_x + view_width).abs_diff(new_x + new_col_width + PADDING * 2); | |
| 2027 | + if dist_to_left <= dist_to_right { | |
| 2028 | + 0 | |
| 2029 | + } else { | |
| 2030 | + -(view_width - new_col_width - PADDING * 2) | |
| 2031 | + } | |
| 2032 | +} | |
| 2033 | + | |
| 1977 | 2034 | #[cfg(test)] |
| 1978 | 2035 | mod tests { |
| 1979 | 2036 | use std::cell::Cell; |