layout/workspace: Reduce code duplication in adding windows
Browse files at this commit7d4e99b76047155c5aaa8453ff590f8e1e6befd3Parent: ab7d81a3 changed files +46 -174
- src/layout/mod.rs +2 -2
- src/layout/monitor.rs +2 -2
- src/layout/workspace.rs +42 -170
src/layout/mod.rsmodified+2 -2
| @@ -594,7 +594,7 @@ impl<W: LayoutElement> Layout<W> { | |||
| 594 | .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name)) | 594 | .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name)) |
| 595 | }) | 595 | }) |
| 596 | .unwrap(); | 596 | .unwrap(); |
| 597 | - ws.add_window(window, true, width, is_full_width); | 597 | + ws.add_window(None, window, true, width, is_full_width); |
| 598 | None | 598 | None |
| 599 | } | 599 | } |
| 600 | } | 600 | } |
| @@ -672,7 +672,7 @@ impl<W: LayoutElement> Layout<W> { | |||
| 672 | workspaces.push(Workspace::new_no_outputs(self.options.clone())); | 672 | workspaces.push(Workspace::new_no_outputs(self.options.clone())); |
| 673 | &mut workspaces[0] | 673 | &mut workspaces[0] |
| 674 | }; | 674 | }; |
| 675 | - ws.add_window(window, true, width, is_full_width); | 675 | + ws.add_window(None, window, true, width, is_full_width); |
| 676 | None | 676 | None |
| 677 | } | 677 | } |
| 678 | } | 678 | } |
src/layout/monitor.rsmodified+2 -2
| @@ -183,7 +183,7 @@ impl<W: LayoutElement> Monitor<W> { | |||
| 183 | ) { | 183 | ) { |
| 184 | let workspace = &mut self.workspaces[workspace_idx]; | 184 | let workspace = &mut self.workspaces[workspace_idx]; |
| 185 | 185 | ||
| 186 | - workspace.add_window(window, activate, width, is_full_width); | 186 | + workspace.add_window(None, window, activate, width, is_full_width); |
| 187 | 187 | ||
| 188 | // After adding a new window, workspace becomes this output's own. | 188 | // After adding a new window, workspace becomes this output's own. |
| 189 | workspace.original_output = OutputId::new(&self.output); | 189 | workspace.original_output = OutputId::new(&self.output); |
| @@ -222,7 +222,7 @@ impl<W: LayoutElement> Monitor<W> { | |||
| 222 | pub fn add_column(&mut self, workspace_idx: usize, column: Column<W>, activate: bool) { | 222 | pub fn add_column(&mut self, workspace_idx: usize, column: Column<W>, activate: bool) { |
| 223 | let workspace = &mut self.workspaces[workspace_idx]; | 223 | let workspace = &mut self.workspaces[workspace_idx]; |
| 224 | 224 | ||
| 225 | - workspace.add_column(column, activate); | 225 | + workspace.add_column(None, column, activate, None); |
| 226 | 226 | ||
| 227 | // After adding a new window, workspace becomes this output's own. | 227 | // After adding a new window, workspace becomes this output's own. |
| 228 | workspace.original_output = OutputId::new(&self.output); | 228 | workspace.original_output = OutputId::new(&self.output); |
src/layout/workspace.rsmodified+42 -170
| @@ -958,31 +958,27 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 958 | self.windows_mut().find(|win| win.is_wl_surface(wl_surface)) | 958 | self.windows_mut().find(|win| win.is_wl_surface(wl_surface)) |
| 959 | } | 959 | } |
| 960 | 960 | ||
| 961 | - pub fn add_window_at( | 961 | + pub fn add_window( |
| 962 | &mut self, | 962 | &mut self, |
| 963 | - col_idx: usize, | 963 | + col_idx: Option<usize>, |
| 964 | window: W, | 964 | window: W, |
| 965 | activate: bool, | 965 | activate: bool, |
| 966 | width: ColumnWidth, | 966 | width: ColumnWidth, |
| 967 | is_full_width: bool, | 967 | is_full_width: bool, |
| 968 | ) { | 968 | ) { |
| 969 | let tile = Tile::new(window, self.scale.fractional_scale(), self.options.clone()); | 969 | 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); |
| 971 | } | 971 | } |
| 972 | 972 | ||
| 973 | - fn add_tile_at( | 973 | + fn add_tile( |
| 974 | &mut self, | 974 | &mut self, |
| 975 | - col_idx: usize, | 975 | + col_idx: Option<usize>, |
| 976 | tile: Tile<W>, | 976 | tile: Tile<W>, |
| 977 | activate: bool, | 977 | activate: bool, |
| 978 | width: ColumnWidth, | 978 | width: ColumnWidth, |
| 979 | is_full_width: bool, | 979 | is_full_width: bool, |
| 980 | anim_config: Option<niri_config::Animation>, | 980 | anim_config: Option<niri_config::Animation>, |
| 981 | ) { | 981 | ) { |
| 982 | - self.enter_output_for_window(tile.window()); | ||
| 983 | - | ||
| 984 | - let was_empty = self.columns.is_empty(); | ||
| 985 | - | ||
| 986 | let column = Column::new_with_tile( | 982 | let column = Column::new_with_tile( |
| 987 | tile, | 983 | tile, |
| 988 | self.view_size, | 984 | self.view_size, |
| @@ -993,72 +989,8 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 993 | is_full_width, | 989 | is_full_width, |
| 994 | true, | 990 | true, |
| 995 | ); | 991 | ); |
| 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 | - }; | ||
| 1043 | 992 | ||
| 1044 | - self.add_window_at(col_idx, window, activate, width, is_full_width); | 993 | + self.add_column(col_idx, column, activate, anim_config); |
| 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); | ||
| 1062 | } | 994 | } |
| 1063 | 995 | ||
| 1064 | pub fn add_window_right_of( | 996 | pub fn add_window_right_of( |
| @@ -1068,62 +1000,39 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1068 | width: ColumnWidth, | 1000 | width: ColumnWidth, |
| 1069 | is_full_width: bool, | 1001 | is_full_width: bool, |
| 1070 | ) { | 1002 | ) { |
| 1071 | - self.enter_output_for_window(&window); | ||
| 1072 | - | ||
| 1073 | let right_of_idx = self | 1003 | let right_of_idx = self |
| 1074 | .columns | 1004 | .columns |
| 1075 | .iter() | 1005 | .iter() |
| 1076 | .position(|col| col.contains(right_of)) | 1006 | .position(|col| col.contains(right_of)) |
| 1077 | .unwrap(); | 1007 | .unwrap(); |
| 1078 | - let idx = right_of_idx + 1; | 1008 | + let col_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); | ||
| 1092 | 1009 | ||
| 1093 | // Activate the new window if right_of was active. | 1010 | // Activate the new window if right_of was active. |
| 1094 | - if self.active_column_idx == right_of_idx { | 1011 | + let activate = 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 | - } | ||
| 1101 | 1012 | ||
| 1102 | - // Animate movement of other columns. | 1013 | + self.add_window(Some(col_idx), window, activate, width, is_full_width); |
| 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 | - } | ||
| 1113 | } | 1014 | } |
| 1114 | 1015 | ||
| 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 | + ) { | ||
| 1116 | for tile in &column.tiles { | 1023 | for tile in &column.tiles { |
| 1117 | self.enter_output_for_window(tile.window()); | 1024 | self.enter_output_for_window(tile.window()); |
| 1118 | } | 1025 | } |
| 1119 | 1026 | ||
| 1120 | let was_empty = self.columns.is_empty(); | 1027 | let was_empty = self.columns.is_empty(); |
| 1121 | 1028 | ||
| 1122 | - let idx = if self.columns.is_empty() { | 1029 | + let idx = idx.unwrap_or_else(|| { |
| 1123 | - 0 | 1030 | + if was_empty { |
| 1124 | - } else { | 1031 | + 0 |
| 1125 | - self.active_column_idx + 1 | 1032 | + } else { |
| 1126 | - }; | 1033 | + self.active_column_idx + 1 |
| 1034 | + } | ||
| 1035 | + }); | ||
| 1127 | 1036 | ||
| 1128 | column.update_config(self.scale.fractional_scale(), self.options.clone()); | 1037 | column.update_config(self.scale.fractional_scale(), self.options.clone()); |
| 1129 | column.set_view_size(self.view_size, self.working_area); | 1038 | column.set_view_size(self.view_size, self.working_area); |
| @@ -1142,19 +1051,24 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1142 | 1051 | ||
| 1143 | let prev_offset = (!was_empty).then(|| self.static_view_offset()); | 1052 | let prev_offset = (!was_empty).then(|| self.static_view_offset()); |
| 1144 | 1053 | ||
| 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); | ||
| 1146 | self.activate_prev_column_on_removal = prev_offset; | 1057 | self.activate_prev_column_on_removal = prev_offset; |
| 1058 | + } else if !was_empty && idx <= self.active_column_idx { | ||
| 1059 | + self.active_column_idx += 1; | ||
| 1147 | } | 1060 | } |
| 1148 | 1061 | ||
| 1149 | // Animate movement of other columns. | 1062 | // Animate movement of other columns. |
| 1150 | let offset = self.column_x(idx + 1) - self.column_x(idx); | 1063 | let offset = self.column_x(idx + 1) - self.column_x(idx); |
| 1064 | + let config = anim_config.unwrap_or(self.options.animations.window_movement.0); | ||
| 1151 | if self.active_column_idx <= idx { | 1065 | if self.active_column_idx <= idx { |
| 1152 | for col in &mut self.columns[idx + 1..] { | 1066 | for col in &mut self.columns[idx + 1..] { |
| 1153 | - col.animate_move_from(-offset); | 1067 | + col.animate_move_from_with_config(-offset, config); |
| 1154 | } | 1068 | } |
| 1155 | } else { | 1069 | } else { |
| 1156 | for col in &mut self.columns[..idx] { | 1070 | for col in &mut self.columns[..idx] { |
| 1157 | - col.animate_move_from(offset); | 1071 | + col.animate_move_from_with_config(offset, config); |
| 1158 | } | 1072 | } |
| 1159 | } | 1073 | } |
| 1160 | } | 1074 | } |
| @@ -1902,8 +1816,8 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1902 | None, | 1816 | None, |
| 1903 | ); | 1817 | ); |
| 1904 | 1818 | ||
| 1905 | - self.add_tile_at( | 1819 | + self.add_tile( |
| 1906 | - self.active_column_idx, | 1820 | + Some(self.active_column_idx), |
| 1907 | tile, | 1821 | tile, |
| 1908 | true, | 1822 | true, |
| 1909 | width, | 1823 | width, |
| @@ -1984,6 +1898,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1984 | ); | 1898 | ); |
| 1985 | 1899 | ||
| 1986 | self.add_tile( | 1900 | self.add_tile( |
| 1901 | + Some(self.active_column_idx + 1), | ||
| 1987 | tile, | 1902 | tile, |
| 1988 | true, | 1903 | true, |
| 1989 | width, | 1904 | width, |
| @@ -2071,6 +1986,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 2071 | ); | 1986 | ); |
| 2072 | 1987 | ||
| 2073 | self.add_tile( | 1988 | self.add_tile( |
| 1989 | + Some(self.active_column_idx + 1), | ||
| 2074 | tile, | 1990 | tile, |
| 2075 | true, | 1991 | true, |
| 2076 | width, | 1992 | width, |
| @@ -2424,27 +2340,14 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 2424 | 2340 | ||
| 2425 | if is_fullscreen && col.tiles.len() > 1 { | 2341 | if is_fullscreen && col.tiles.len() > 1 { |
| 2426 | // This wasn't the only window in its column; extract it into a separate column. | 2342 | // This wasn't the only window in its column; extract it into a separate column. |
| 2427 | - let target_window_was_focused = | 2343 | + let activate = self.active_column_idx == col_idx && col.active_tile_idx == tile_idx; |
| 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); | ||
| 2442 | let width = col.width; | 2344 | let width = col.width; |
| 2443 | let is_full_width = col.is_full_width; | 2345 | let is_full_width = col.is_full_width; |
| 2444 | 2346 | ||
| 2445 | - col_idx += 1; | 2347 | + let tile = self.remove_tile_by_idx(col_idx, tile_idx, Transaction::new(), None); |
| 2446 | - let column = Column::new( | 2348 | + // Create a column manually to disable the resize animation. |
| 2447 | - window, | 2349 | + let column = Column::new_with_tile( |
| 2350 | + tile, | ||
| 2448 | self.view_size, | 2351 | self.view_size, |
| 2449 | self.working_area, | 2352 | self.working_area, |
| 2450 | self.scale.fractional_scale(), | 2353 | self.scale.fractional_scale(), |
| @@ -2453,16 +2356,9 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 2453 | is_full_width, | 2356 | is_full_width, |
| 2454 | false, | 2357 | false, |
| 2455 | ); | 2358 | ); |
| 2456 | - self.data.insert(col_idx, ColumnData::new(&column)); | 2359 | + self.add_column(Some(col_idx + 1), column, activate, None); |
| 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 | - } | ||
| 2465 | 2360 | ||
| 2361 | + col_idx += 1; | ||
| 2466 | col = &mut self.columns[col_idx]; | 2362 | col = &mut self.columns[col_idx]; |
| 2467 | } | 2363 | } |
| 2468 | 2364 | ||
| @@ -2989,30 +2885,6 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 2989 | } | 2885 | } |
| 2990 | 2886 | ||
| 2991 | impl<W: LayoutElement> Column<W> { | 2887 | 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 | - | ||
| 3016 | #[allow(clippy::too_many_arguments)] | 2888 | #[allow(clippy::too_many_arguments)] |
| 3017 | fn new_with_tile( | 2889 | fn new_with_tile( |
| 3018 | tile: Tile<W>, | 2890 | tile: Tile<W>, |