forge
Explore
Log in

Don't you dare go Hollow

layout/scrolling: Extract activate_idx()

Browse files at this commit

f836d1c28ae4930f9b649f24db09130a2b6d6e18Ivan MolodetskikhParent: 4f05a74
src/layout/scrolling.rsmodified+31 -22
@@ -793,7 +793,8 @@ impl<W: LayoutElement> ScrollingSpace<W> {
793 793
794 let target_column = &mut self.columns[col_idx];794 let target_column = &mut self.columns[col_idx];
795 let tile_idx = tile_idx.unwrap_or(target_column.tiles.len());795 let tile_idx = tile_idx.unwrap_or(target_column.tiles.len());
796- let was_fullscreen = target_column.tiles[target_column.active_tile_idx].is_fullscreen();796+ let mut prev_active_tile_idx = target_column.active_tile_idx;
797+ let was_fullscreen = target_column.tiles[prev_active_tile_idx].is_fullscreen();
797 798
798 target_column.add_tile_at(tile_idx, tile, true);799 target_column.add_tile_at(tile_idx, tile, true);
799 self.data[col_idx].update(target_column);800 self.data[col_idx].update(target_column);
@@ -805,13 +806,16 @@ impl<W: LayoutElement> ScrollingSpace<W> {
805 self.view_offset_before_fullscreen = None;806 self.view_offset_before_fullscreen = None;
806 }807 }
807 808
809+ if tile_idx <= prev_active_tile_idx {
810+ target_column.active_tile_idx += 1;
811+ prev_active_tile_idx += 1;
812+ }
813+
808 if activate {814 if activate {
809- target_column.active_tile_idx = tile_idx;815+ target_column.activate_idx(tile_idx);
810 if self.active_column_idx != col_idx {816 if self.active_column_idx != col_idx {
811 self.activate_column(col_idx);817 self.activate_column(col_idx);
812 }818 }
813- } else if tile_idx <= target_column.active_tile_idx {
814- target_column.active_tile_idx += 1;
815 }819 }
816 820
817 // Adding a wider window into a column increases its width now (even if the window will821 // Adding a wider window into a column increases its width now (even if the window will
@@ -988,8 +992,12 @@ impl<W: LayoutElement> ScrollingSpace<W> {
988 if tile_idx < column.active_tile_idx {992 if tile_idx < column.active_tile_idx {
989 // A tile above was removed; preserve the current position.993 // A tile above was removed; preserve the current position.
990 column.active_tile_idx -= 1;994 column.active_tile_idx -= 1;
991- } else {995+ } else if tile_idx == column.active_tile_idx {
992- column.active_tile_idx = min(column.active_tile_idx, column.tiles.len() - 1);996+ // The active tile was removed, so the active tile index shifted to the next tile.
997+ if tile_idx == column.tiles.len() {
998+ // The bottom tile was removed and it was active, update active idx to remain valid.
999+ column.activate_idx(tile_idx - 1);
1000+ }
993 }1001 }
994 1002
995 column.update_tile_sizes_with_transaction(true, transaction);1003 column.update_tile_sizes_with_transaction(true, transaction);
@@ -3308,9 +3316,19 @@ impl<W: LayoutElement> Column<W> {
3308 .position(|win| win.id() == window)3316 .position(|win| win.id() == window)
3309 }3317 }
3310 3318
3319+ fn activate_idx(&mut self, idx: usize) -> bool {
3320+ if self.active_tile_idx == idx {
3321+ return false;
3322+ }
3323+
3324+ self.active_tile_idx = idx;
3325+
3326+ true
3327+ }
3328+
3311 fn activate_window(&mut self, window: &W::Id) {3329 fn activate_window(&mut self, window: &W::Id) {
3312 let idx = self.position(window).unwrap();3330 let idx = self.position(window).unwrap();
3313- self.active_tile_idx = idx;3331+ self.activate_idx(idx);
3314 }3332 }
3315 3333
3316 fn add_tile_at(&mut self, idx: usize, mut tile: Tile<W>, animate: bool) {3334 fn add_tile_at(&mut self, idx: usize, mut tile: Tile<W>, animate: bool) {
@@ -3635,33 +3653,24 @@ impl<W: LayoutElement> Column<W> {
3635 }3653 }
3636 3654
3637 fn focus_index(&mut self, index: u8) {3655 fn focus_index(&mut self, index: u8) {
3638- self.active_tile_idx = min(usize::from(index.saturating_sub(1)), self.tiles.len() - 1);3656+ let idx = min(usize::from(index.saturating_sub(1)), self.tiles.len() - 1);
3657+ self.activate_idx(idx);
3639 }3658 }
3640 3659
3641 fn focus_up(&mut self) -> bool {3660 fn focus_up(&mut self) -> bool {
3642- if self.active_tile_idx == 0 {3661+ self.activate_idx(self.active_tile_idx.saturating_sub(1))
3643- return false;
3644- }
3645-
3646- self.active_tile_idx -= 1;
3647- true
3648 }3662 }
3649 3663
3650 fn focus_down(&mut self) -> bool {3664 fn focus_down(&mut self) -> bool {
3651- if self.active_tile_idx == self.tiles.len() - 1 {3665+ self.activate_idx(min(self.active_tile_idx + 1, self.tiles.len() - 1))
3652- return false;
3653- }
3654-
3655- self.active_tile_idx += 1;
3656- true
3657 }3666 }
3658 3667
3659 fn focus_top(&mut self) {3668 fn focus_top(&mut self) {
3660- self.active_tile_idx = 0;3669+ self.activate_idx(0);
3661 }3670 }
3662 3671
3663 fn focus_bottom(&mut self) {3672 fn focus_bottom(&mut self) {
3664- self.active_tile_idx = self.tiles.len() - 1;3673+ self.activate_idx(self.tiles.len() - 1);
3665 }3674 }
3666 3675
3667 fn move_up(&mut self) -> bool {3676 fn move_up(&mut self) -> bool {