layout: Replace add_window_to_output with add_window
Browse files at this commit513763eaec76f425cfa865e48696fd0be55e08f2Parent: e4b5f652 changed files +43 -21
- src/handlers/compositor.rs +3 -5
- src/layout.rs +40 -16
src/handlers/compositor.rsmodified+3 -5
| @@ -86,11 +86,9 @@ impl CompositorHandler for State { | |||
| 86 | let window = entry.remove(); | 86 | let window = entry.remove(); |
| 87 | window.on_commit(); | 87 | window.on_commit(); |
| 88 | 88 | ||
| 89 | - let output = self.niri.monitor_set.active_output().unwrap().clone(); | 89 | + if let Some(output) = self.niri.monitor_set.add_window(window, true).cloned() { |
| 90 | - self.niri | 90 | + self.niri.queue_redraw(output); |
| 91 | - .monitor_set | 91 | + } |
| 92 | - .add_window_to_output(&output, window, true); | ||
| 93 | - self.niri.queue_redraw(output); | ||
| 94 | return; | 92 | return; |
| 95 | } | 93 | } |
| 96 | 94 | ||
src/layout.rsmodified+40 -16
| @@ -376,7 +376,7 @@ impl<W: LayoutElement> MonitorSet<W> { | |||
| 376 | } | 376 | } |
| 377 | } | 377 | } |
| 378 | 378 | ||
| 379 | - pub fn add_window( | 379 | + pub fn add_window_by_idx( |
| 380 | &mut self, | 380 | &mut self, |
| 381 | monitor_idx: usize, | 381 | monitor_idx: usize, |
| 382 | workspace_idx: usize, | 382 | workspace_idx: usize, |
| @@ -399,19 +399,31 @@ impl<W: LayoutElement> MonitorSet<W> { | |||
| 399 | } | 399 | } |
| 400 | } | 400 | } |
| 401 | 401 | ||
| 402 | - pub fn add_window_to_output(&mut self, output: &Output, window: W, activate: bool) { | 402 | + /// Adds a new window to the layout. |
| 403 | - let MonitorSet::Normal { monitors, .. } = self else { | 403 | + /// |
| 404 | - panic!() | 404 | + /// Returns an output that the window was added to, if there were any outputs. |
| 405 | - }; | 405 | + pub fn add_window(&mut self, window: W, activate: bool) -> Option<&Output> { |
| 406 | - | 406 | + match self { |
| 407 | - let (monitor_idx, monitor) = monitors | 407 | + MonitorSet::Normal { |
| 408 | - .iter() | 408 | + monitors, |
| 409 | - .enumerate() | 409 | + active_monitor_idx, |
| 410 | - .find(|(_, mon)| &mon.output == output) | 410 | + .. |
| 411 | - .unwrap(); | 411 | + } => { |
| 412 | - let workspace_idx = monitor.active_workspace_idx; | 412 | + let mon = &mut monitors[*active_monitor_idx]; |
| 413 | - | 413 | + mon.add_window(mon.active_workspace_idx, window, activate); |
| 414 | - self.add_window(monitor_idx, workspace_idx, window, activate); | 414 | + Some(&mon.output) |
| 415 | + } | ||
| 416 | + MonitorSet::NoOutputs(workspaces) => { | ||
| 417 | + let ws = if let Some(ws) = workspaces.get_mut(0) { | ||
| 418 | + ws | ||
| 419 | + } else { | ||
| 420 | + workspaces.push(Workspace::new_no_outputs()); | ||
| 421 | + &mut workspaces[0] | ||
| 422 | + }; | ||
| 423 | + ws.add_window(window, activate); | ||
| 424 | + None | ||
| 425 | + } | ||
| 426 | + } | ||
| 415 | } | 427 | } |
| 416 | 428 | ||
| 417 | pub fn remove_window(&mut self, window: &W) { | 429 | pub fn remove_window(&mut self, window: &W) { |
| @@ -880,7 +892,7 @@ impl<W: LayoutElement> MonitorSet<W> { | |||
| 880 | ws.remove_window(&window); | 892 | ws.remove_window(&window); |
| 881 | 893 | ||
| 882 | let workspace_idx = monitors[new_idx].active_workspace_idx; | 894 | let workspace_idx = monitors[new_idx].active_workspace_idx; |
| 883 | - self.add_window(new_idx, workspace_idx, window, true); | 895 | + self.add_window_by_idx(new_idx, workspace_idx, window, true); |
| 884 | } | 896 | } |
| 885 | } | 897 | } |
| 886 | 898 | ||
| @@ -895,7 +907,7 @@ impl<W: LayoutElement> MonitorSet<W> { | |||
| 895 | 907 | ||
| 896 | let workspace_idx = monitors[new_idx].active_workspace_idx; | 908 | let workspace_idx = monitors[new_idx].active_workspace_idx; |
| 897 | // FIXME: activate only if it was already active and focused. | 909 | // FIXME: activate only if it was already active and focused. |
| 898 | - self.add_window(new_idx, workspace_idx, window, true); | 910 | + self.add_window_by_idx(new_idx, workspace_idx, window, true); |
| 899 | } | 911 | } |
| 900 | } | 912 | } |
| 901 | 913 | ||
| @@ -1246,6 +1258,18 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1246 | } | 1258 | } |
| 1247 | } | 1259 | } |
| 1248 | 1260 | ||
| 1261 | + fn new_no_outputs() -> Self { | ||
| 1262 | + Self { | ||
| 1263 | + output: None, | ||
| 1264 | + original_output: OutputId(String::new()), | ||
| 1265 | + view_size: Size::from((1280, 720)), | ||
| 1266 | + columns: vec![], | ||
| 1267 | + active_column_idx: 0, | ||
| 1268 | + view_offset: 0, | ||
| 1269 | + view_offset_anim: None, | ||
| 1270 | + } | ||
| 1271 | + } | ||
| 1272 | + | ||
| 1249 | pub fn advance_animations(&mut self, current_time: Duration) { | 1273 | pub fn advance_animations(&mut self, current_time: Duration) { |
| 1250 | match &mut self.view_offset_anim { | 1274 | match &mut self.view_offset_anim { |
| 1251 | Some(anim) => { | 1275 | Some(anim) => { |