forge
Explore
Log in

Don't you dare go Hollow

layout/workspace: Reduce code duplication in adding windows

Browse files at this commit

7d4e99b76047155c5aaa8453ff590f8e1e6befd3Ivan MolodetskikhParent: ab7d81a
src/layout/mod.rsmodified+2 -2
@@ -594,7 +594,7 @@ impl<W: LayoutElement> Layout<W> {
594594 .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name))
595595 })
596596 .unwrap();
597- ws.add_window(window, true, width, is_full_width);
597+ ws.add_window(None, window, true, width, is_full_width);
598598 None
599599 }
600600 }
@@ -672,7 +672,7 @@ impl<W: LayoutElement> Layout<W> {
672672 workspaces.push(Workspace::new_no_outputs(self.options.clone()));
673673 &mut workspaces[0]
674674 };
675- ws.add_window(window, true, width, is_full_width);
675+ ws.add_window(None, window, true, width, is_full_width);
676676 None
677677 }
678678 }
src/layout/monitor.rsmodified+2 -2
@@ -183,7 +183,7 @@ impl<W: LayoutElement> Monitor<W> {
183183 ) {
184184 let workspace = &mut self.workspaces[workspace_idx];
185185
186- workspace.add_window(window, activate, width, is_full_width);
186+ workspace.add_window(None, window, activate, width, is_full_width);
187187
188188 // After adding a new window, workspace becomes this output's own.
189189 workspace.original_output = OutputId::new(&self.output);
@@ -222,7 +222,7 @@ impl<W: LayoutElement> Monitor<W> {
222222 pub fn add_column(&mut self, workspace_idx: usize, column: Column<W>, activate: bool) {
223223 let workspace = &mut self.workspaces[workspace_idx];
224224
225- workspace.add_column(column, activate);
225+ workspace.add_column(None, column, activate, None);
226226
227227 // After adding a new window, workspace becomes this output's own.
228228 workspace.original_output = OutputId::new(&self.output);
src/layout/workspace.rsmodified+42 -170
@@ -958,31 +958,27 @@ impl<W: LayoutElement> Workspace<W> {
958958 self.windows_mut().find(|win| win.is_wl_surface(wl_surface))
959959 }
960960
961- pub fn add_window_at(
961+ pub fn add_window(
962962 &mut self,
963- col_idx: usize,
963+ col_idx: Option<usize>,
964964 window: W,
965965 activate: bool,
966966 width: ColumnWidth,
967967 is_full_width: bool,
968968 ) {
969969 let tile = Tile::new(window, self.scale.fractional_scale(), self.options.clone());
970- self.add_tile_at(col_idx, tile, activate, width, is_full_width, None);
970+ self.add_tile(col_idx, tile, activate, width, is_full_width, None);
971971 }
972972
973- fn add_tile_at(
973+ fn add_tile(
974974 &mut self,
975- col_idx: usize,
975+ col_idx: Option<usize>,
976976 tile: Tile<W>,
977977 activate: bool,
978978 width: ColumnWidth,
979979 is_full_width: bool,
980980 anim_config: Option<niri_config::Animation>,
981981 ) {
982- self.enter_output_for_window(tile.window());
983-
984- let was_empty = self.columns.is_empty();
985-
986982 let column = Column::new_with_tile(
987983 tile,
988984 self.view_size,
@@ -993,72 +989,8 @@ impl<W: LayoutElement> Workspace<W> {
993989 is_full_width,
994990 true,
995991 );
996- self.data.insert(col_idx, ColumnData::new(&column));
997- self.columns.insert(col_idx, column);
998-
999- if activate {
1000- // If this is the first window on an empty workspace, remove the effect of whatever
1001- // view_offset was left over and skip the animation.
1002- if was_empty {
1003- self.view_offset = 0.;
1004- self.view_offset_adj = None;
1005- self.view_offset =
1006- self.compute_new_view_offset_for_column(self.view_pos(), col_idx, None);
1007- }
1008-
1009- let prev_offset = (!was_empty).then(|| self.static_view_offset());
1010-
1011- let anim_config =
1012- anim_config.unwrap_or(self.options.animations.horizontal_view_movement.0);
1013- self.activate_column_with_anim_config(col_idx, anim_config);
1014- self.activate_prev_column_on_removal = prev_offset;
1015- }
1016-
1017- // Animate movement of other columns.
1018- let offset = self.column_x(col_idx + 1) - self.column_x(col_idx);
1019- let config = anim_config.unwrap_or(self.options.animations.window_movement.0);
1020- if self.active_column_idx <= col_idx {
1021- for col in &mut self.columns[col_idx + 1..] {
1022- col.animate_move_from_with_config(-offset, config);
1023- }
1024- } else {
1025- for col in &mut self.columns[..col_idx] {
1026- col.animate_move_from_with_config(offset, config);
1027- }
1028- }
1029- }
1030-
1031- pub fn add_window(
1032- &mut self,
1033- window: W,
1034- activate: bool,
1035- width: ColumnWidth,
1036- is_full_width: bool,
1037- ) {
1038- let col_idx = if self.columns.is_empty() {
1039- 0
1040- } else {
1041- self.active_column_idx + 1
1042- };
1043992
1044- self.add_window_at(col_idx, window, activate, width, is_full_width);
1045- }
1046-
1047- fn add_tile(
1048- &mut self,
1049- tile: Tile<W>,
1050- activate: bool,
1051- width: ColumnWidth,
1052- is_full_width: bool,
1053- anim_config: Option<niri_config::Animation>,
1054- ) {
1055- let col_idx = if self.columns.is_empty() {
1056- 0
1057- } else {
1058- self.active_column_idx + 1
1059- };
1060-
1061- self.add_tile_at(col_idx, tile, activate, width, is_full_width, anim_config);
993+ self.add_column(col_idx, column, activate, anim_config);
1062994 }
1063995
1064996 pub fn add_window_right_of(
@@ -1068,62 +1000,39 @@ impl<W: LayoutElement> Workspace<W> {
10681000 width: ColumnWidth,
10691001 is_full_width: bool,
10701002 ) {
1071- self.enter_output_for_window(&window);
1072-
10731003 let right_of_idx = self
10741004 .columns
10751005 .iter()
10761006 .position(|col| col.contains(right_of))
10771007 .unwrap();
1078- let idx = right_of_idx + 1;
1079-
1080- let column = Column::new(
1081- window,
1082- self.view_size,
1083- self.working_area,
1084- self.scale.fractional_scale(),
1085- self.options.clone(),
1086- width,
1087- is_full_width,
1088- true,
1089- );
1090- self.data.insert(idx, ColumnData::new(&column));
1091- self.columns.insert(idx, column);
1008+ let col_idx = right_of_idx + 1;
10921009
10931010 // Activate the new window if right_of was active.
1094- if self.active_column_idx == right_of_idx {
1095- let prev_offset = self.static_view_offset();
1096- self.activate_column(idx);
1097- self.activate_prev_column_on_removal = Some(prev_offset);
1098- } else if idx <= self.active_column_idx {
1099- self.active_column_idx += 1;
1100- }
1011+ let activate = self.active_column_idx == right_of_idx;
11011012
1102- // Animate movement of other columns.
1103- let offset = self.column_x(idx + 1) - self.column_x(idx);
1104- if self.active_column_idx <= idx {
1105- for col in &mut self.columns[idx + 1..] {
1106- col.animate_move_from(-offset);
1107- }
1108- } else {
1109- for col in &mut self.columns[..idx] {
1110- col.animate_move_from(offset);
1111- }
1112- }
1013+ self.add_window(Some(col_idx), window, activate, width, is_full_width);
11131014 }
11141015
1115- pub fn add_column(&mut self, mut column: Column<W>, activate: bool) {
1016+ pub fn add_column(
1017+ &mut self,
1018+ idx: Option<usize>,
1019+ mut column: Column<W>,
1020+ activate: bool,
1021+ anim_config: Option<niri_config::Animation>,
1022+ ) {
11161023 for tile in &column.tiles {
11171024 self.enter_output_for_window(tile.window());
11181025 }
11191026
11201027 let was_empty = self.columns.is_empty();
11211028
1122- let idx = if self.columns.is_empty() {
1123- 0
1124- } else {
1125- self.active_column_idx + 1
1126- };
1029+ let idx = idx.unwrap_or_else(|| {
1030+ if was_empty {
1031+ 0
1032+ } else {
1033+ self.active_column_idx + 1
1034+ }
1035+ });
11271036
11281037 column.update_config(self.scale.fractional_scale(), self.options.clone());
11291038 column.set_view_size(self.view_size, self.working_area);
@@ -1142,19 +1051,24 @@ impl<W: LayoutElement> Workspace<W> {
11421051
11431052 let prev_offset = (!was_empty).then(|| self.static_view_offset());
11441053
1145- self.activate_column(idx);
1054+ let anim_config =
1055+ anim_config.unwrap_or(self.options.animations.horizontal_view_movement.0);
1056+ self.activate_column_with_anim_config(idx, anim_config);
11461057 self.activate_prev_column_on_removal = prev_offset;
1058+ } else if !was_empty && idx <= self.active_column_idx {
1059+ self.active_column_idx += 1;
11471060 }
11481061
11491062 // Animate movement of other columns.
11501063 let offset = self.column_x(idx + 1) - self.column_x(idx);
1064+ let config = anim_config.unwrap_or(self.options.animations.window_movement.0);
11511065 if self.active_column_idx <= idx {
11521066 for col in &mut self.columns[idx + 1..] {
1153- col.animate_move_from(-offset);
1067+ col.animate_move_from_with_config(-offset, config);
11541068 }
11551069 } else {
11561070 for col in &mut self.columns[..idx] {
1157- col.animate_move_from(offset);
1071+ col.animate_move_from_with_config(offset, config);
11581072 }
11591073 }
11601074 }
@@ -1902,8 +1816,8 @@ impl<W: LayoutElement> Workspace<W> {
19021816 None,
19031817 );
19041818
1905- self.add_tile_at(
1906- self.active_column_idx,
1819+ self.add_tile(
1820+ Some(self.active_column_idx),
19071821 tile,
19081822 true,
19091823 width,
@@ -1984,6 +1898,7 @@ impl<W: LayoutElement> Workspace<W> {
19841898 );
19851899
19861900 self.add_tile(
1901+ Some(self.active_column_idx + 1),
19871902 tile,
19881903 true,
19891904 width,
@@ -2071,6 +1986,7 @@ impl<W: LayoutElement> Workspace<W> {
20711986 );
20721987
20731988 self.add_tile(
1989+ Some(self.active_column_idx + 1),
20741990 tile,
20751991 true,
20761992 width,
@@ -2424,27 +2340,14 @@ impl<W: LayoutElement> Workspace<W> {
24242340
24252341 if is_fullscreen && col.tiles.len() > 1 {
24262342 // This wasn't the only window in its column; extract it into a separate column.
2427- let target_window_was_focused =
2428- self.active_column_idx == col_idx && col.active_tile_idx == tile_idx;
2429- let window = col.tiles.remove(tile_idx).into_window();
2430- col.data.remove(tile_idx);
2431- col.active_tile_idx = min(col.active_tile_idx, col.tiles.len() - 1);
2432-
2433- // If one window is left, reset its weight to 1.
2434- if col.data.len() == 1 {
2435- if let WindowHeight::Auto { weight } = &mut col.data[0].height {
2436- *weight = 1.;
2437- }
2438- }
2439-
2440- col.update_tile_sizes(false);
2441- self.data[col_idx].update(col);
2343+ let activate = self.active_column_idx == col_idx && col.active_tile_idx == tile_idx;
24422344 let width = col.width;
24432345 let is_full_width = col.is_full_width;
24442346
2445- col_idx += 1;
2446- let column = Column::new(
2447- window,
2347+ let tile = self.remove_tile_by_idx(col_idx, tile_idx, Transaction::new(), None);
2348+ // Create a column manually to disable the resize animation.
2349+ let column = Column::new_with_tile(
2350+ tile,
24482351 self.view_size,
24492352 self.working_area,
24502353 self.scale.fractional_scale(),
@@ -2453,16 +2356,9 @@ impl<W: LayoutElement> Workspace<W> {
24532356 is_full_width,
24542357 false,
24552358 );
2456- self.data.insert(col_idx, ColumnData::new(&column));
2457- self.columns.insert(col_idx, column);
2458-
2459- if target_window_was_focused {
2460- self.activate_column(col_idx);
2461- self.view_offset_before_fullscreen = Some(self.static_view_offset());
2462- } else if self.active_column_idx >= col_idx {
2463- self.active_column_idx += 1;
2464- }
2359+ self.add_column(Some(col_idx + 1), column, activate, None);
24652360
2361+ col_idx += 1;
24662362 col = &mut self.columns[col_idx];
24672363 }
24682364
@@ -2989,30 +2885,6 @@ impl<W: LayoutElement> Workspace<W> {
29892885 }
29902886
29912887 impl<W: LayoutElement> Column<W> {
2992- #[allow(clippy::too_many_arguments)]
2993- fn new(
2994- window: W,
2995- view_size: Size<f64, Logical>,
2996- working_area: Rectangle<f64, Logical>,
2997- scale: f64,
2998- options: Rc<Options>,
2999- width: ColumnWidth,
3000- is_full_width: bool,
3001- animate_resize: bool,
3002- ) -> Self {
3003- let tile = Tile::new(window, scale, options.clone());
3004- Self::new_with_tile(
3005- tile,
3006- view_size,
3007- working_area,
3008- scale,
3009- options,
3010- width,
3011- is_full_width,
3012- animate_resize,
3013- )
3014- }
3015-
30162888 #[allow(clippy::too_many_arguments)]
30172889 fn new_with_tile(
30182890 tile: Tile<W>,