forge
Explore
Log in

Don't you dare go Hollow

Implement minimizing view movement on changes

Browse files at this commit

219ca08836adcd91f42ca24ec20f643af17dd270Ivan MolodetskikhParent: c8b85b8
Before this commit, windows were always left-aligned.

Now, when changing focus, if the window is fully visible, the view
doesn't move. If the window is not fully visible, the view is moved the
minimum amount.
src/animation.rsmodified+4 -0
@@ -46,4 +46,8 @@ impl Animation {
4646 let x = (passed / total).clamp(0., 1.);
4747 EaseOutCubic.y(x) * (self.to - self.from) + self.from
4848 }
49+
50+ pub fn to(&self) -> f64 {
51+ self.to
52+ }
4953 }
src/layout.rsmodified+64 -7
@@ -1340,14 +1340,21 @@ impl<W: LayoutElement> Workspace<W> {
13401340
13411341 self.active_column_idx = idx;
13421342
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;
13461352 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,
13491355 Duration::from_millis(250),
13501356 ));
1357+ self.view_offset = from_view_offset;
13511358 }
13521359
13531360 fn has_windows(&self) -> bool {
@@ -1419,12 +1426,41 @@ impl<W: LayoutElement> Workspace<W> {
14191426 }
14201427
14211428 fn update_window(&mut self, window: &W) {
1422- let column = self
1429+ let (idx, column) = self
14231430 .columns
14241431 .iter_mut()
1425- .find(|col| col.contains(window))
1432+ .enumerate()
1433+ .find(|(_, col)| col.contains(window))
14261434 .unwrap();
14271435 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+ }
14281464 }
14291465
14301466 fn activate_window(&mut self, window: &W) {
@@ -1974,6 +2010,27 @@ pub fn configure_new_window(view_size: Size<i32, Logical>, window: &Window) {
19742010 });
19752011 }
19762012
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+
19772034 #[cfg(test)]
19782035 mod tests {
19792036 use std::cell::Cell;