Implement explicit unmapped window state tracking
Browse files at this commit2317021a7c4a7296606533d38f1fdce96826f7dcParent: af6485c8 changed files +308 -153
- niri-visual-tests/src/cases/layout.rs +2 -3
- src/handlers/compositor.rs +36 -26
- src/handlers/xdg_shell.rs +168 -91
- src/layout/mod.rs +19 -21
- src/layout/workspace.rs +9 -11
- src/lib.rs +1 -0
- src/niri.rs +2 -1
- src/window.rs +71 -0
niri-visual-tests/src/cases/layout.rsmodified+2 -3
| @@ -145,8 +145,7 @@ impl Layout { | ||
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | fn add_window(&mut self, window: TestWindow, width: Option<ColumnWidth>) { |
| 148 | - self.layout | |
| 149 | - .add_window(window.clone(), width.map(Some), false); | |
| 148 | + self.layout.add_window(window.clone(), width, false); | |
| 150 | 149 | if window.communicate() { |
| 151 | 150 | self.layout.update_window(&window); |
| 152 | 151 | } |
| @@ -160,7 +159,7 @@ impl Layout { | ||
| 160 | 159 | width: Option<ColumnWidth>, |
| 161 | 160 | ) { |
| 162 | 161 | self.layout |
| 163 | - .add_window_right_of(right_of, window.clone(), width.map(Some), false); | |
| 162 | + .add_window_right_of(right_of, window.clone(), width, false); | |
| 164 | 163 | if window.communicate() { |
| 165 | 164 | self.layout.update_window(&window); |
| 166 | 165 | } |
src/handlers/compositor.rsmodified+36 -26
| @@ -16,9 +16,9 @@ use smithay::wayland::dmabuf::get_dmabuf; | ||
| 16 | 16 | use smithay::wayland::shm::{ShmHandler, ShmState}; |
| 17 | 17 | use smithay::{delegate_compositor, delegate_shm}; |
| 18 | 18 | |
| 19 | -use super::xdg_shell::{initial_configure_sent, resolve_window_rules}; | |
| 20 | 19 | use crate::niri::{ClientState, State}; |
| 21 | 20 | use crate::utils::clone2; |
| 21 | +use crate::window::{InitialConfigureState, Unmapped}; | |
| 22 | 22 | |
| 23 | 23 | impl CompositorHandler for State { |
| 24 | 24 | fn compositor_state(&mut self) -> &mut CompositorState { |
| @@ -105,29 +105,41 @@ impl CompositorHandler for State { | ||
| 105 | 105 | |
| 106 | 106 | if is_mapped { |
| 107 | 107 | // The toplevel got mapped. |
| 108 | - let window = entry.remove(); | |
| 108 | + let Unmapped { window, state } = entry.remove(); | |
| 109 | + | |
| 109 | 110 | window.on_commit(); |
| 110 | 111 | |
| 112 | + let (width, output) = | |
| 113 | + if let InitialConfigureState::Configured { width, output, .. } = state { | |
| 114 | + // Check that the output is still connected. | |
| 115 | + let output = | |
| 116 | + output.filter(|o| self.niri.layout.monitor_for_output(o).is_some()); | |
| 117 | + | |
| 118 | + (width, output) | |
| 119 | + } else { | |
| 120 | + error!("window map must happen after initial configure"); | |
| 121 | + (None, None) | |
| 122 | + }; | |
| 123 | + | |
| 111 | 124 | let parent = window |
| 112 | 125 | .toplevel() |
| 113 | 126 | .parent() |
| 114 | 127 | .and_then(|parent| self.niri.layout.find_window_and_output(&parent)) |
| 115 | - .map(|(win, _)| win.clone()); | |
| 116 | - | |
| 117 | - let (width, output) = { | |
| 118 | - let config = self.niri.config.borrow(); | |
| 119 | - let rules = resolve_window_rules(&config.window_rules, window.toplevel()); | |
| 120 | - let output = rules | |
| 121 | - .open_on_output | |
| 122 | - .and_then(|name| self.niri.output_by_name.get(name)) | |
| 123 | - .cloned(); | |
| 124 | - (rules.default_width, output) | |
| 125 | - }; | |
| 128 | + // Only consider the parent if we configured the window for the same | |
| 129 | + // output. | |
| 130 | + // | |
| 131 | + // Normally when we're following the parent, the configured output will be | |
| 132 | + // None. If the configured output is set, that means it was set explicitly | |
| 133 | + // by a window rule or a fullscreen request. | |
| 134 | + .filter(|(_, parent_output)| { | |
| 135 | + output.is_none() || output.as_ref() == Some(*parent_output) | |
| 136 | + }) | |
| 137 | + .map(|(window, _)| window.clone()); | |
| 126 | 138 | |
| 127 | 139 | let win = window.clone(); |
| 128 | 140 | |
| 129 | - // Open dialogs immediately to the right of their parent window. | |
| 130 | 141 | let output = if let Some(p) = parent { |
| 142 | + // Open dialogs immediately to the right of their parent window. | |
| 131 | 143 | self.niri.layout.add_window_right_of(&p, win, width, false) |
| 132 | 144 | } else if let Some(output) = &output { |
| 133 | 145 | self.niri |
| @@ -146,17 +158,10 @@ impl CompositorHandler for State { | ||
| 146 | 158 | } |
| 147 | 159 | |
| 148 | 160 | // The toplevel remains unmapped. |
| 149 | - let window = entry.get().clone(); | |
| 150 | - | |
| 151 | - // Send the initial configure in an idle, in case the client sent some more info | |
| 152 | - // after the initial commit. | |
| 153 | - if !initial_configure_sent(window.toplevel()) { | |
| 154 | - self.niri.event_loop.insert_idle(move |state| { | |
| 155 | - if !window.toplevel().alive() { | |
| 156 | - return; | |
| 157 | - } | |
| 158 | - state.send_initial_configure_if_needed(&window); | |
| 159 | - }); | |
| 161 | + let unmapped = entry.get(); | |
| 162 | + if unmapped.needs_initial_configure() { | |
| 163 | + let toplevel = unmapped.window.toplevel().clone(); | |
| 164 | + self.queue_initial_configure(toplevel); | |
| 160 | 165 | } |
| 161 | 166 | return; |
| 162 | 167 | } |
| @@ -178,7 +183,12 @@ impl CompositorHandler for State { | ||
| 178 | 183 | if !is_mapped { |
| 179 | 184 | // The toplevel got unmapped. |
| 180 | 185 | self.niri.layout.remove_window(&window); |
| 181 | - self.niri.unmapped_windows.insert(surface.clone(), window); | |
| 186 | + | |
| 187 | + // Newly-unmapped toplevels must perform the initial commit-configure sequence | |
| 188 | + // afresh. | |
| 189 | + let unmapped = Unmapped::new(window); | |
| 190 | + self.niri.unmapped_windows.insert(surface.clone(), unmapped); | |
| 191 | + | |
| 182 | 192 | self.niri.queue_redraw(output); |
| 183 | 193 | return; |
| 184 | 194 | } |
src/handlers/xdg_shell.rsmodified+168 -91
| @@ -27,19 +27,7 @@ use smithay::{delegate_kde_decoration, delegate_xdg_decoration, delegate_xdg_she | ||
| 27 | 27 | use crate::layout::workspace::ColumnWidth; |
| 28 | 28 | use crate::niri::{PopupGrabState, State}; |
| 29 | 29 | use crate::utils::clone2; |
| 30 | - | |
| 31 | -#[derive(Debug, Default)] | |
| 32 | -pub struct ResolvedWindowRule<'a> { | |
| 33 | - /// Default width for this window. | |
| 34 | - /// | |
| 35 | - /// - `None`: unset. | |
| 36 | - /// - `Some(None)`: set to empty. | |
| 37 | - /// - `Some(Some(width))`: set to a particular width. | |
| 38 | - pub default_width: Option<Option<ColumnWidth>>, | |
| 39 | - | |
| 40 | - /// Output to open this window on. | |
| 41 | - pub open_on_output: Option<&'a str>, | |
| 42 | -} | |
| 30 | +use crate::window::{InitialConfigureState, ResolvedWindowRules, Unmapped}; | |
| 43 | 31 | |
| 44 | 32 | fn window_matches(role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool { |
| 45 | 33 | if let Some(app_id_re) = &m.app_id { |
| @@ -63,13 +51,13 @@ fn window_matches(role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool { | ||
| 63 | 51 | true |
| 64 | 52 | } |
| 65 | 53 | |
| 66 | -pub fn resolve_window_rules<'a>( | |
| 67 | - rules: &'a [WindowRule], | |
| 54 | +pub fn resolve_window_rules( | |
| 55 | + rules: &[WindowRule], | |
| 68 | 56 | toplevel: &ToplevelSurface, |
| 69 | -) -> ResolvedWindowRule<'a> { | |
| 57 | +) -> ResolvedWindowRules { | |
| 70 | 58 | let _span = tracy_client::span!("resolve_window_rules"); |
| 71 | 59 | |
| 72 | - let mut resolved = ResolvedWindowRule::default(); | |
| 60 | + let mut resolved = ResolvedWindowRules::default(); | |
| 73 | 61 | |
| 74 | 62 | with_states(toplevel.wl_surface(), |states| { |
| 75 | 63 | let role = states |
| @@ -79,6 +67,8 @@ pub fn resolve_window_rules<'a>( | ||
| 79 | 67 | .lock() |
| 80 | 68 | .unwrap(); |
| 81 | 69 | |
| 70 | + let mut open_on_output = None; | |
| 71 | + | |
| 82 | 72 | for rule in rules { |
| 83 | 73 | if !(rule.matches.is_empty() || rule.matches.iter().any(|m| window_matches(&role, m))) { |
| 84 | 74 | continue; |
| @@ -97,9 +87,11 @@ pub fn resolve_window_rules<'a>( | ||
| 97 | 87 | } |
| 98 | 88 | |
| 99 | 89 | if let Some(x) = rule.open_on_output.as_deref() { |
| 100 | - resolved.open_on_output = Some(x); | |
| 90 | + open_on_output = Some(x); | |
| 101 | 91 | } |
| 102 | 92 | } |
| 93 | + | |
| 94 | + resolved.open_on_output = open_on_output.map(|x| x.to_owned()); | |
| 103 | 95 | }); |
| 104 | 96 | |
| 105 | 97 | resolved |
| @@ -112,10 +104,8 @@ impl XdgShellHandler for State { | ||
| 112 | 104 | |
| 113 | 105 | fn new_toplevel(&mut self, surface: ToplevelSurface) { |
| 114 | 106 | let wl_surface = surface.wl_surface().clone(); |
| 115 | - let window = Window::new(surface); | |
| 116 | - | |
| 117 | - // At the moment of creation, xdg toplevels must have no buffer. | |
| 118 | - let existing = self.niri.unmapped_windows.insert(wl_surface, window); | |
| 107 | + let unmapped = Unmapped::new(Window::new(surface)); | |
| 108 | + let existing = self.niri.unmapped_windows.insert(wl_surface, unmapped); | |
| 119 | 109 | assert!(existing.is_none()); |
| 120 | 110 | } |
| 121 | 111 | |
| @@ -273,53 +263,42 @@ impl XdgShellHandler for State { | ||
| 273 | 263 | surface: ToplevelSurface, |
| 274 | 264 | wl_output: Option<wl_output::WlOutput>, |
| 275 | 265 | ) { |
| 276 | - if surface | |
| 277 | - .current_state() | |
| 278 | - .capabilities | |
| 279 | - .contains(xdg_toplevel::WmCapabilities::Fullscreen) | |
| 266 | + if let Some((window, current_output)) = self | |
| 267 | + .niri | |
| 268 | + .layout | |
| 269 | + .find_window_and_output(surface.wl_surface()) | |
| 280 | 270 | { |
| 281 | - if let Some((window, current_output)) = self | |
| 282 | - .niri | |
| 283 | - .layout | |
| 284 | - .find_window_and_output(surface.wl_surface()) | |
| 285 | - { | |
| 286 | - let window = window.clone(); | |
| 271 | + let window = window.clone(); | |
| 287 | 272 | |
| 288 | - if let Some(requested_output) = wl_output.as_ref().and_then(Output::from_resource) { | |
| 289 | - if &requested_output != current_output { | |
| 290 | - self.niri | |
| 291 | - .layout | |
| 292 | - .move_window_to_output(window.clone(), &requested_output); | |
| 293 | - } | |
| 273 | + if let Some(requested_output) = wl_output.as_ref().and_then(Output::from_resource) { | |
| 274 | + if &requested_output != current_output { | |
| 275 | + self.niri | |
| 276 | + .layout | |
| 277 | + .move_window_to_output(window.clone(), &requested_output); | |
| 294 | 278 | } |
| 279 | + } | |
| 295 | 280 | |
| 296 | - self.niri.layout.set_fullscreen(&window, true); | |
| 297 | - } else if let Some(window) = self.niri.unmapped_windows.get(surface.wl_surface()) { | |
| 298 | - let config = self.niri.config.borrow(); | |
| 299 | - let rules = resolve_window_rules(&config.window_rules, window.toplevel()); | |
| 300 | - | |
| 301 | - // FIXME: take requested output into account (will need to thread this through to | |
| 302 | - // send_initial_configure_if_needed and commit handler). | |
| 303 | - let output = rules | |
| 304 | - .open_on_output | |
| 305 | - .and_then(|name| self.niri.output_by_name.get(name)); | |
| 306 | - let mon = output.map(|o| self.niri.layout.monitor_for_output(o).unwrap()); | |
| 307 | - let ws = mon | |
| 308 | - .map(|mon| mon.active_workspace_ref()) | |
| 309 | - .or_else(|| self.niri.layout.active_workspace()); | |
| 310 | - | |
| 311 | - if let Some(ws) = ws { | |
| 312 | - window.toplevel().with_pending_state(|state| { | |
| 313 | - state.size = Some(ws.view_size()); | |
| 314 | - state.states.set(xdg_toplevel::State::Fullscreen); | |
| 315 | - }); | |
| 281 | + self.niri.layout.set_fullscreen(&window, true); | |
| 282 | + | |
| 283 | + // A configure is required in response to this event regardless if there are pending | |
| 284 | + // changes. | |
| 285 | + surface.send_configure(); | |
| 286 | + } else if let Some(unmapped) = self.niri.unmapped_windows.get_mut(surface.wl_surface()) { | |
| 287 | + match &mut unmapped.state { | |
| 288 | + InitialConfigureState::NotConfigured { wants_fullscreen } => { | |
| 289 | + *wants_fullscreen = Some(wl_output.as_ref().and_then(Output::from_resource)); | |
| 290 | + | |
| 291 | + // The required configure will be the initial configure. | |
| 316 | 292 | } |
| 317 | - } | |
| 318 | - } | |
| 293 | + InitialConfigureState::Configured { .. } => { | |
| 294 | + // FIXME: implement this once I figure out a good way without code duplication. | |
| 319 | 295 | |
| 320 | - // A configure is required in response to this event. However, if an initial configure | |
| 321 | - // wasn't sent, then we will send this as part of the initial configure later. | |
| 322 | - if initial_configure_sent(&surface) { | |
| 296 | + // We already sent the initial configure, so we need to reconfigure. | |
| 297 | + surface.send_configure(); | |
| 298 | + } | |
| 299 | + } | |
| 300 | + } else { | |
| 301 | + error!("couldn't find the toplevel in fullscreen_request()"); | |
| 323 | 302 | surface.send_configure(); |
| 324 | 303 | } |
| 325 | 304 | } |
| @@ -332,24 +311,27 @@ impl XdgShellHandler for State { | ||
| 332 | 311 | { |
| 333 | 312 | let window = window.clone(); |
| 334 | 313 | self.niri.layout.set_fullscreen(&window, false); |
| 335 | - } else if let Some(window) = self.niri.unmapped_windows.get(surface.wl_surface()) { | |
| 336 | - let config = self.niri.config.borrow(); | |
| 337 | - let rules = resolve_window_rules(&config.window_rules, window.toplevel()); | |
| 338 | - | |
| 339 | - let output = rules | |
| 340 | - .open_on_output | |
| 341 | - .and_then(|name| self.niri.output_by_name.get(name)); | |
| 342 | - let mon = output.map(|o| self.niri.layout.monitor_for_output(o).unwrap()); | |
| 343 | - let ws = mon | |
| 344 | - .map(|mon| mon.active_workspace_ref()) | |
| 345 | - .or_else(|| self.niri.layout.active_workspace()); | |
| 346 | - | |
| 347 | - if let Some(ws) = ws { | |
| 348 | - window.toplevel().with_pending_state(|state| { | |
| 349 | - state.size = Some(ws.new_window_size(rules.default_width)); | |
| 350 | - state.states.unset(xdg_toplevel::State::Fullscreen); | |
| 351 | - }); | |
| 314 | + | |
| 315 | + // A configure is required in response to this event regardless if there are pending | |
| 316 | + // changes. | |
| 317 | + surface.send_configure(); | |
| 318 | + } else if let Some(unmapped) = self.niri.unmapped_windows.get_mut(surface.wl_surface()) { | |
| 319 | + match &mut unmapped.state { | |
| 320 | + InitialConfigureState::NotConfigured { wants_fullscreen } => { | |
| 321 | + *wants_fullscreen = None; | |
| 322 | + | |
| 323 | + // The required configure will be the initial configure. | |
| 324 | + } | |
| 325 | + InitialConfigureState::Configured { .. } => { | |
| 326 | + // FIXME: implement this once I figure out a good way without code duplication. | |
| 327 | + | |
| 328 | + // We already sent the initial configure, so we need to reconfigure. | |
| 329 | + surface.send_configure(); | |
| 330 | + } | |
| 352 | 331 | } |
| 332 | + } else { | |
| 333 | + error!("couldn't find the toplevel in unfullscreen_request()"); | |
| 334 | + surface.send_configure(); | |
| 353 | 335 | } |
| 354 | 336 | } |
| 355 | 337 | |
| @@ -385,6 +367,14 @@ impl XdgShellHandler for State { | ||
| 385 | 367 | self.niri.queue_redraw(output.clone()); |
| 386 | 368 | } |
| 387 | 369 | } |
| 370 | + | |
| 371 | + fn app_id_changed(&mut self, toplevel: ToplevelSurface) { | |
| 372 | + self.update_window_rules(&toplevel); | |
| 373 | + } | |
| 374 | + | |
| 375 | + fn title_changed(&mut self, toplevel: ToplevelSurface) { | |
| 376 | + self.update_window_rules(&toplevel); | |
| 377 | + } | |
| 388 | 378 | } |
| 389 | 379 | |
| 390 | 380 | delegate_xdg_shell!(State); |
| @@ -440,7 +430,7 @@ impl KdeDecorationHandler for State { | ||
| 440 | 430 | |
| 441 | 431 | delegate_kde_decoration!(State); |
| 442 | 432 | |
| 443 | -pub fn initial_configure_sent(toplevel: &ToplevelSurface) -> bool { | |
| 433 | +fn initial_configure_sent(toplevel: &ToplevelSurface) -> bool { | |
| 444 | 434 | with_states(toplevel.wl_surface(), |states| { |
| 445 | 435 | states |
| 446 | 436 | .data_map |
| @@ -453,28 +443,82 @@ pub fn initial_configure_sent(toplevel: &ToplevelSurface) -> bool { | ||
| 453 | 443 | } |
| 454 | 444 | |
| 455 | 445 | impl State { |
| 456 | - pub fn send_initial_configure_if_needed(&mut self, window: &Window) { | |
| 457 | - let toplevel = window.toplevel(); | |
| 458 | - if initial_configure_sent(toplevel) { | |
| 446 | + pub fn send_initial_configure(&mut self, toplevel: &ToplevelSurface) { | |
| 447 | + let _span = tracy_client::span!("State::send_initial_configure"); | |
| 448 | + | |
| 449 | + let Some(unmapped) = self.niri.unmapped_windows.get_mut(toplevel.wl_surface()) else { | |
| 450 | + error!("window must be present in unmapped_windows in send_initial_configure()"); | |
| 459 | 451 | return; |
| 460 | - } | |
| 452 | + }; | |
| 461 | 453 | |
| 462 | - let _span = tracy_client::span!("State::send_initial_configure_if_needed"); | |
| 454 | + let Unmapped { window, state } = unmapped; | |
| 455 | + | |
| 456 | + let InitialConfigureState::NotConfigured { wants_fullscreen } = state else { | |
| 457 | + error!("window must not be already configured in send_initial_configure()"); | |
| 458 | + return; | |
| 459 | + }; | |
| 463 | 460 | |
| 464 | 461 | let config = self.niri.config.borrow(); |
| 465 | 462 | let rules = resolve_window_rules(&config.window_rules, toplevel); |
| 466 | 463 | |
| 467 | - let output = rules | |
| 464 | + // Pick the target monitor. First, check if we had an output set in the window rules. | |
| 465 | + let mon = rules | |
| 468 | 466 | .open_on_output |
| 469 | - .and_then(|name| self.niri.output_by_name.get(name)); | |
| 470 | - let mon = output.map(|o| self.niri.layout.monitor_for_output(o).unwrap()); | |
| 467 | + .as_deref() | |
| 468 | + .and_then(|name| self.niri.output_by_name.get(name)) | |
| 469 | + .and_then(|o| self.niri.layout.monitor_for_output(o)); | |
| 470 | + | |
| 471 | + // If not, check if the window requested one for fullscreen. | |
| 472 | + let mon = mon.or_else(|| { | |
| 473 | + wants_fullscreen | |
| 474 | + .as_ref() | |
| 475 | + .and_then(|x| x.as_ref()) | |
| 476 | + // The monitor might not exist if the output was disconnected. | |
| 477 | + .and_then(|o| self.niri.layout.monitor_for_output(o)) | |
| 478 | + }); | |
| 479 | + | |
| 480 | + // If not, check if this is a dialog with a parent, to place it next to the parent. | |
| 481 | + let mon = mon.map(|mon| (mon, false)).or_else(|| { | |
| 482 | + toplevel | |
| 483 | + .parent() | |
| 484 | + .and_then(|parent| self.niri.layout.find_window_and_output(&parent)) | |
| 485 | + .map(|(_win, output)| output) | |
| 486 | + .and_then(|o| self.niri.layout.monitor_for_output(o)) | |
| 487 | + .map(|mon| (mon, true)) | |
| 488 | + }); | |
| 489 | + | |
| 490 | + // If not, use the active monitor. | |
| 491 | + let mon = mon.or_else(|| { | |
| 492 | + self.niri | |
| 493 | + .layout | |
| 494 | + .active_monitor_ref() | |
| 495 | + .map(|mon| (mon, false)) | |
| 496 | + }); | |
| 497 | + | |
| 498 | + // If we're following the parent, don't set the target output, so that when the window is | |
| 499 | + // mapped, it fetches the possibly changed parent's output again, and shows up there. | |
| 500 | + let output = mon | |
| 501 | + .filter(|(_, parent)| !parent) | |
| 502 | + .map(|(mon, _)| mon.output.clone()); | |
| 503 | + let mon = mon.map(|(mon, _)| mon); | |
| 504 | + | |
| 505 | + let mut width = None; | |
| 506 | + | |
| 507 | + // Tell the surface the preferred size and bounds for its likely output. | |
| 471 | 508 | let ws = mon |
| 472 | 509 | .map(|mon| mon.active_workspace_ref()) |
| 473 | 510 | .or_else(|| self.niri.layout.active_workspace()); |
| 474 | 511 | |
| 475 | - // Tell the surface the preferred size and bounds for its likely output. | |
| 476 | 512 | if let Some(ws) = ws { |
| 477 | - ws.configure_new_window(window, rules.default_width); | |
| 513 | + // Set a fullscreen state if requested. | |
| 514 | + if wants_fullscreen.is_some() { | |
| 515 | + toplevel.with_pending_state(|state| { | |
| 516 | + state.states.set(xdg_toplevel::State::Fullscreen); | |
| 517 | + }); | |
| 518 | + } | |
| 519 | + | |
| 520 | + width = ws.resolve_default_width(rules.default_width); | |
| 521 | + ws.configure_new_window(window, width); | |
| 478 | 522 | } |
| 479 | 523 | |
| 480 | 524 | // If the user prefers no CSD, it's a reasonable assumption that they would prefer to get |
| @@ -488,9 +532,32 @@ impl State { | ||
| 488 | 532 | }); |
| 489 | 533 | } |
| 490 | 534 | |
| 535 | + // Set the configured settings. | |
| 536 | + *state = InitialConfigureState::Configured { | |
| 537 | + rules, | |
| 538 | + width, | |
| 539 | + output, | |
| 540 | + }; | |
| 541 | + | |
| 491 | 542 | toplevel.send_configure(); |
| 492 | 543 | } |
| 493 | 544 | |
| 545 | + pub fn queue_initial_configure(&self, toplevel: ToplevelSurface) { | |
| 546 | + // Send the initial configure in an idle, in case the client sent some more info after the | |
| 547 | + // initial commit. | |
| 548 | + self.niri.event_loop.insert_idle(move |state| { | |
| 549 | + if !toplevel.alive() { | |
| 550 | + return; | |
| 551 | + } | |
| 552 | + | |
| 553 | + if let Some(unmapped) = state.niri.unmapped_windows.get(toplevel.wl_surface()) { | |
| 554 | + if unmapped.needs_initial_configure() { | |
| 555 | + state.send_initial_configure(&toplevel); | |
| 556 | + } | |
| 557 | + } | |
| 558 | + }); | |
| 559 | + } | |
| 560 | + | |
| 494 | 561 | /// Should be called on `WlSurface::commit` |
| 495 | 562 | pub fn popups_handle_commit(&mut self, surface: &WlSurface) { |
| 496 | 563 | self.niri.popups.commit(surface); |
| @@ -611,6 +678,16 @@ impl State { | ||
| 611 | 678 | } |
| 612 | 679 | } |
| 613 | 680 | } |
| 681 | + | |
| 682 | + pub fn update_window_rules(&mut self, toplevel: &ToplevelSurface) { | |
| 683 | + let resolve = || resolve_window_rules(&self.niri.config.borrow().window_rules, toplevel); | |
| 684 | + | |
| 685 | + if let Some(unmapped) = self.niri.unmapped_windows.get_mut(toplevel.wl_surface()) { | |
| 686 | + if let InitialConfigureState::Configured { rules, .. } = &mut unmapped.state { | |
| 687 | + *rules = resolve(); | |
| 688 | + } | |
| 689 | + } | |
| 690 | + } | |
| 614 | 691 | } |
| 615 | 692 | |
| 616 | 693 | fn unconstrain_with_padding( |
src/layout/mod.rsmodified+19 -21
| @@ -531,15 +531,10 @@ impl<W: LayoutElement> Layout<W> { | ||
| 531 | 531 | pub fn add_window( |
| 532 | 532 | &mut self, |
| 533 | 533 | window: W, |
| 534 | - width: Option<Option<ColumnWidth>>, | |
| 534 | + width: Option<ColumnWidth>, | |
| 535 | 535 | is_full_width: bool, |
| 536 | 536 | ) -> Option<&Output> { |
| 537 | - let width = match width { | |
| 538 | - Some(Some(width)) => Some(width), | |
| 539 | - Some(None) => None, | |
| 540 | - None => self.options.default_width, | |
| 541 | - } | |
| 542 | - .unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); | |
| 537 | + let width = width.unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); | |
| 543 | 538 | |
| 544 | 539 | match &mut self.monitor_set { |
| 545 | 540 | MonitorSet::Normal { |
| @@ -587,15 +582,10 @@ impl<W: LayoutElement> Layout<W> { | ||
| 587 | 582 | &mut self, |
| 588 | 583 | right_of: &W, |
| 589 | 584 | window: W, |
| 590 | - width: Option<Option<ColumnWidth>>, | |
| 585 | + width: Option<ColumnWidth>, | |
| 591 | 586 | is_full_width: bool, |
| 592 | 587 | ) -> Option<&Output> { |
| 593 | - let width = match width { | |
| 594 | - Some(Some(width)) => Some(width), | |
| 595 | - Some(None) => None, | |
| 596 | - None => self.options.default_width, | |
| 597 | - } | |
| 598 | - .unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); | |
| 588 | + let width = width.unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); | |
| 599 | 589 | |
| 600 | 590 | match &mut self.monitor_set { |
| 601 | 591 | MonitorSet::Normal { monitors, .. } => { |
| @@ -623,15 +613,10 @@ impl<W: LayoutElement> Layout<W> { | ||
| 623 | 613 | &mut self, |
| 624 | 614 | output: &Output, |
| 625 | 615 | window: W, |
| 626 | - width: Option<Option<ColumnWidth>>, | |
| 616 | + width: Option<ColumnWidth>, | |
| 627 | 617 | is_full_width: bool, |
| 628 | 618 | ) { |
| 629 | - let width = match width { | |
| 630 | - Some(Some(width)) => Some(width), | |
| 631 | - Some(None) => None, | |
| 632 | - None => self.options.default_width, | |
| 633 | - } | |
| 634 | - .unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); | |
| 619 | + let width = width.unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); | |
| 635 | 620 | |
| 636 | 621 | let MonitorSet::Normal { |
| 637 | 622 | monitors, |
| @@ -931,6 +916,19 @@ impl<W: LayoutElement> Layout<W> { | ||
| 931 | 916 | Some(&mut monitors[*active_monitor_idx]) |
| 932 | 917 | } |
| 933 | 918 | |
| 919 | + pub fn active_monitor_ref(&self) -> Option<&Monitor<W>> { | |
| 920 | + let MonitorSet::Normal { | |
| 921 | + monitors, | |
| 922 | + active_monitor_idx, | |
| 923 | + .. | |
| 924 | + } = &self.monitor_set | |
| 925 | + else { | |
| 926 | + return None; | |
| 927 | + }; | |
| 928 | + | |
| 929 | + Some(&monitors[*active_monitor_idx]) | |
| 930 | + } | |
| 931 | + | |
| 934 | 932 | pub fn monitor_for_output(&self, output: &Output) -> Option<&Monitor<W>> { |
| 935 | 933 | let MonitorSet::Normal { monitors, .. } = &self.monitor_set else { |
| 936 | 934 | return None; |
src/layout/workspace.rsmodified+9 -11
| @@ -322,17 +322,19 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 322 | 322 | )) |
| 323 | 323 | } |
| 324 | 324 | |
| 325 | - pub fn new_window_size( | |
| 325 | + pub fn resolve_default_width( | |
| 326 | 326 | &self, |
| 327 | 327 | default_width: Option<Option<ColumnWidth>>, |
| 328 | - ) -> Size<i32, Logical> { | |
| 329 | - let default_width = match default_width { | |
| 328 | + ) -> Option<ColumnWidth> { | |
| 329 | + match default_width { | |
| 330 | 330 | Some(Some(width)) => Some(width), |
| 331 | 331 | Some(None) => None, |
| 332 | 332 | None => self.options.default_width, |
| 333 | - }; | |
| 333 | + } | |
| 334 | + } | |
| 334 | 335 | |
| 335 | - let width = if let Some(width) = default_width { | |
| 336 | + pub fn new_window_size(&self, width: Option<ColumnWidth>) -> Size<i32, Logical> { | |
| 337 | + let width = if let Some(width) = width { | |
| 336 | 338 | let mut width = width.resolve(&self.options, self.working_area.size.w); |
| 337 | 339 | if !self.options.border.off { |
| 338 | 340 | width -= self.options.border.width as i32 * 2; |
| @@ -350,11 +352,7 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 350 | 352 | Size::from((width, max(height, 1))) |
| 351 | 353 | } |
| 352 | 354 | |
| 353 | - pub fn configure_new_window( | |
| 354 | - &self, | |
| 355 | - window: &Window, | |
| 356 | - default_width: Option<Option<ColumnWidth>>, | |
| 357 | - ) { | |
| 355 | + pub fn configure_new_window(&self, window: &Window, width: Option<ColumnWidth>) { | |
| 358 | 356 | if let Some(output) = self.output.as_ref() { |
| 359 | 357 | set_preferred_scale_transform(window, output); |
| 360 | 358 | } |
| @@ -363,7 +361,7 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 363 | 361 | if state.states.contains(xdg_toplevel::State::Fullscreen) { |
| 364 | 362 | state.size = Some(self.view_size); |
| 365 | 363 | } else { |
| 366 | - state.size = Some(self.new_window_size(default_width)); | |
| 364 | + state.size = Some(self.new_window_size(width)); | |
| 367 | 365 | } |
| 368 | 366 | |
| 369 | 367 | state.bounds = Some(self.toplevel_bounds()); |
src/lib.rsmodified+1 -0
| @@ -17,6 +17,7 @@ pub mod protocols; | ||
| 17 | 17 | pub mod render_helpers; |
| 18 | 18 | pub mod ui; |
| 19 | 19 | pub mod utils; |
| 20 | +pub mod window; | |
| 20 | 21 | |
| 21 | 22 | #[cfg(not(feature = "xdp-gnome-screencast"))] |
| 22 | 23 | pub mod dummy_pw_utils; |
src/niri.rsmodified+2 -1
| @@ -107,6 +107,7 @@ use crate::ui::screenshot_ui::{ScreenshotUi, ScreenshotUiRenderElement}; | ||
| 107 | 107 | use crate::utils::{ |
| 108 | 108 | center, get_monotonic_time, make_screenshot_path, output_size, write_png_rgba8, |
| 109 | 109 | }; |
| 110 | +use crate::window::Unmapped; | |
| 110 | 111 | use crate::{animation, niri_render_elements}; |
| 111 | 112 | |
| 112 | 113 | const CLEAR_COLOR: [f32; 4] = [0.2, 0.2, 0.2, 1.]; |
| @@ -132,7 +133,7 @@ pub struct Niri { | ||
| 132 | 133 | pub global_space: Space<Window>, |
| 133 | 134 | |
| 134 | 135 | // Windows which don't have a buffer attached yet. |
| 135 | - pub unmapped_windows: HashMap<WlSurface, Window>, | |
| 136 | + pub unmapped_windows: HashMap<WlSurface, Unmapped>, | |
| 136 | 137 | |
| 137 | 138 | pub output_state: HashMap<Output, OutputState>, |
| 138 | 139 | pub output_by_name: HashMap<String, Output>, |
src/window.rsadded+71 -0
| @@ -0,0 +1,71 @@ | ||
| 1 | +use smithay::desktop::Window; | |
| 2 | +use smithay::output::Output; | |
| 3 | + | |
| 4 | +use crate::layout::workspace::ColumnWidth; | |
| 5 | + | |
| 6 | +#[derive(Debug)] | |
| 7 | +pub struct Unmapped { | |
| 8 | + pub window: Window, | |
| 9 | + pub state: InitialConfigureState, | |
| 10 | +} | |
| 11 | + | |
| 12 | +#[derive(Debug)] | |
| 13 | +pub enum InitialConfigureState { | |
| 14 | + /// The window has not been initially configured yet. | |
| 15 | + NotConfigured { | |
| 16 | + /// Whether the window requested to be fullscreened, and the requested output, if any. | |
| 17 | + wants_fullscreen: Option<Option<Output>>, | |
| 18 | + }, | |
| 19 | + /// The window has been configured. | |
| 20 | + Configured { | |
| 21 | + /// Up-to-date rules. | |
| 22 | + /// | |
| 23 | + /// We start tracking window rules when sending the initial configure, since they don't | |
| 24 | + /// affect anything before that. | |
| 25 | + rules: ResolvedWindowRules, | |
| 26 | + | |
| 27 | + /// Resolved default width for this window. | |
| 28 | + /// | |
| 29 | + /// `None` means that the window will pick its own width. | |
| 30 | + width: Option<ColumnWidth>, | |
| 31 | + | |
| 32 | + /// Output to open this window on. | |
| 33 | + /// | |
| 34 | + /// This can be `None` in cases like: | |
| 35 | + /// | |
| 36 | + /// - There are no outputs connected. | |
| 37 | + /// - This is a dialog with a parent, and there was no explicit output set, so this dialog | |
| 38 | + /// should fetch the parent's current output again upon mapping. | |
| 39 | + output: Option<Output>, | |
| 40 | + }, | |
| 41 | +} | |
| 42 | + | |
| 43 | +/// Rules fully resolved for a window. | |
| 44 | +#[derive(Debug, Default)] | |
| 45 | +pub struct ResolvedWindowRules { | |
| 46 | + /// Default width for this window. | |
| 47 | + /// | |
| 48 | + /// - `None`: unset (global default should be used). | |
| 49 | + /// - `Some(None)`: set to empty (window picks its own width). | |
| 50 | + /// - `Some(Some(width))`: set to a particular width. | |
| 51 | + pub default_width: Option<Option<ColumnWidth>>, | |
| 52 | + | |
| 53 | + /// Output to open this window on. | |
| 54 | + pub open_on_output: Option<String>, | |
| 55 | +} | |
| 56 | + | |
| 57 | +impl Unmapped { | |
| 58 | + /// Wraps a newly created window that hasn't been initially configured yet. | |
| 59 | + pub fn new(window: Window) -> Self { | |
| 60 | + Self { | |
| 61 | + window, | |
| 62 | + state: InitialConfigureState::NotConfigured { | |
| 63 | + wants_fullscreen: None, | |
| 64 | + }, | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + pub fn needs_initial_configure(&self) -> bool { | |
| 69 | + matches!(self.state, InitialConfigureState::NotConfigured { .. }) | |
| 70 | + } | |
| 71 | +} |