Implement popup unconstraining
Browse files at this commite259061cbcfaf3a52a9eb2016b949cdc42e45db3Parent: 206493b4 changed files +125 -6
- src/handlers/compositor.rs +3 -0
- src/handlers/layer_shell.rs +5 -0
- src/handlers/xdg_shell.rs +90 -6
- src/layout.rs +27 -0
src/handlers/compositor.rsmodified+3 -0
| @@ -135,6 +135,9 @@ impl CompositorHandler for State { | |||
| 135 | // The toplevel remains mapped. | 135 | // The toplevel remains mapped. |
| 136 | self.niri.layout.update_window(&window); | 136 | self.niri.layout.update_window(&window); |
| 137 | 137 | ||
| 138 | + // Popup placement depends on window size which might have changed. | ||
| 139 | + self.update_reactive_popups(&window, &output); | ||
| 140 | + | ||
| 138 | self.niri.queue_redraw(output); | 141 | self.niri.queue_redraw(output); |
| 139 | return; | 142 | return; |
| 140 | } | 143 | } |
src/handlers/layer_shell.rsmodified+5 -0
| @@ -8,6 +8,7 @@ use smithay::wayland::shell::wlr_layer::{ | |||
| 8 | Layer, LayerSurface as WlrLayerSurface, LayerSurfaceData, WlrLayerShellHandler, | 8 | Layer, LayerSurface as WlrLayerSurface, LayerSurfaceData, WlrLayerShellHandler, |
| 9 | WlrLayerShellState, | 9 | WlrLayerShellState, |
| 10 | }; | 10 | }; |
| 11 | +use smithay::wayland::shell::xdg::PopupSurface; | ||
| 11 | 12 | ||
| 12 | use crate::niri::State; | 13 | use crate::niri::State; |
| 13 | 14 | ||
| @@ -52,6 +53,10 @@ impl WlrLayerShellHandler for State { | |||
| 52 | self.niri.output_resized(output); | 53 | self.niri.output_resized(output); |
| 53 | } | 54 | } |
| 54 | } | 55 | } |
| 56 | + | ||
| 57 | + fn new_popup(&mut self, _parent: WlrLayerSurface, popup: PopupSurface) { | ||
| 58 | + self.unconstrain_popup(&popup); | ||
| 59 | + } | ||
| 55 | } | 60 | } |
| 56 | delegate_layer_shell!(State); | 61 | delegate_layer_shell!(State); |
| 57 | 62 | ||
src/handlers/xdg_shell.rsmodified+90 -6
| @@ -1,11 +1,14 @@ | |||
| 1 | -use smithay::desktop::{find_popup_root_surface, PopupKind, Window}; | 1 | +use smithay::desktop::{ |
| 2 | + find_popup_root_surface, get_popup_toplevel_coords, layer_map_for_output, LayerSurface, | ||
| 3 | + PopupKind, PopupManager, Window, WindowSurfaceType, | ||
| 4 | +}; | ||
| 2 | use smithay::output::Output; | 5 | use smithay::output::Output; |
| 3 | use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1; | 6 | use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1; |
| 4 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::{self, ResizeEdge}; | 7 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::{self, ResizeEdge}; |
| 5 | use smithay::reexports::wayland_server::protocol::wl_output; | 8 | use smithay::reexports::wayland_server::protocol::wl_output; |
| 6 | use smithay::reexports::wayland_server::protocol::wl_seat::WlSeat; | 9 | use smithay::reexports::wayland_server::protocol::wl_seat::WlSeat; |
| 7 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | 10 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 8 | -use smithay::utils::Serial; | 11 | +use smithay::utils::{Rectangle, Serial}; |
| 9 | use smithay::wayland::compositor::{send_surface_state, with_states}; | 12 | use smithay::wayland::compositor::{send_surface_state, with_states}; |
| 10 | use smithay::wayland::shell::kde::decoration::{KdeDecorationHandler, KdeDecorationState}; | 13 | use smithay::wayland::shell::kde::decoration::{KdeDecorationHandler, KdeDecorationState}; |
| 11 | use smithay::wayland::shell::xdg::decoration::XdgDecorationHandler; | 14 | use smithay::wayland::shell::xdg::decoration::XdgDecorationHandler; |
| @@ -49,8 +52,8 @@ impl XdgShellHandler for State { | |||
| 49 | } | 52 | } |
| 50 | 53 | ||
| 51 | fn new_popup(&mut self, surface: PopupSurface, _positioner: PositionerState) { | 54 | fn new_popup(&mut self, surface: PopupSurface, _positioner: PositionerState) { |
| 52 | - // FIXME: adjust the geometry so the popup doesn't overflow at least off the top and bottom | 55 | + self.unconstrain_popup(&surface); |
| 53 | - // screen edges, and ideally off the view size. | 56 | + |
| 54 | if let Err(err) = self.niri.popups.track_popup(PopupKind::Xdg(surface)) { | 57 | if let Err(err) = self.niri.popups.track_popup(PopupKind::Xdg(surface)) { |
| 55 | warn!("error tracking popup: {err:?}"); | 58 | warn!("error tracking popup: {err:?}"); |
| 56 | } | 59 | } |
| @@ -76,13 +79,12 @@ impl XdgShellHandler for State { | |||
| 76 | positioner: PositionerState, | 79 | positioner: PositionerState, |
| 77 | token: u32, | 80 | token: u32, |
| 78 | ) { | 81 | ) { |
| 79 | - // FIXME: adjust the geometry so the popup doesn't overflow at least off the top and bottom | ||
| 80 | - // screen edges, and ideally off the view size. | ||
| 81 | surface.with_pending_state(|state| { | 82 | surface.with_pending_state(|state| { |
| 82 | let geometry = positioner.get_geometry(); | 83 | let geometry = positioner.get_geometry(); |
| 83 | state.geometry = geometry; | 84 | state.geometry = geometry; |
| 84 | state.positioner = positioner; | 85 | state.positioner = positioner; |
| 85 | }); | 86 | }); |
| 87 | + self.unconstrain_popup(&surface); | ||
| 86 | surface.send_repositioned(token); | 88 | surface.send_repositioned(token); |
| 87 | } | 89 | } |
| 88 | 90 | ||
| @@ -289,4 +291,86 @@ impl State { | |||
| 289 | let root = find_popup_root_surface(popup).ok()?; | 291 | let root = find_popup_root_surface(popup).ok()?; |
| 290 | self.niri.output_for_root(&root) | 292 | self.niri.output_for_root(&root) |
| 291 | } | 293 | } |
| 294 | + | ||
| 295 | + pub fn unconstrain_popup(&self, popup: &PopupSurface) { | ||
| 296 | + let _span = tracy_client::span!("Niri::unconstrain_popup"); | ||
| 297 | + | ||
| 298 | + // Popups with a NULL parent will get repositioned in their respective protocol handlers | ||
| 299 | + // (i.e. layer-shell). | ||
| 300 | + let Ok(root) = find_popup_root_surface(&PopupKind::Xdg(popup.clone())) else { | ||
| 301 | + return; | ||
| 302 | + }; | ||
| 303 | + | ||
| 304 | + // Figure out if the root is a window or a layer surface. | ||
| 305 | + if let Some((window, output)) = self.niri.layout.find_window_and_output(&root) { | ||
| 306 | + self.unconstrain_window_popup(popup, &window, &output); | ||
| 307 | + } else if let Some((layer_surface, output)) = self.niri.layout.outputs().find_map(|o| { | ||
| 308 | + let map = layer_map_for_output(o); | ||
| 309 | + let layer_surface = map.layer_for_surface(&root, WindowSurfaceType::TOPLEVEL)?; | ||
| 310 | + Some((layer_surface.clone(), o)) | ||
| 311 | + }) { | ||
| 312 | + self.unconstrain_layer_shell_popup(popup, &layer_surface, output); | ||
| 313 | + } | ||
| 314 | + } | ||
| 315 | + | ||
| 316 | + fn unconstrain_window_popup(&self, popup: &PopupSurface, window: &Window, output: &Output) { | ||
| 317 | + let window_geo = window.geometry(); | ||
| 318 | + let output_geo = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 319 | + | ||
| 320 | + // The target geometry for the positioner should be relative to its parent's geometry, so | ||
| 321 | + // we will compute that here. | ||
| 322 | + // | ||
| 323 | + // We try to keep regular window popups within the window itself horizontally (since the | ||
| 324 | + // window can be scrolled to both edges of the screen), but within the whole monitor's | ||
| 325 | + // height. | ||
| 326 | + let mut target = | ||
| 327 | + Rectangle::from_loc_and_size((0, 0), (window_geo.size.w, output_geo.size.h)); | ||
| 328 | + target.loc.y -= self.niri.layout.window_y(window).unwrap(); | ||
| 329 | + target.loc -= get_popup_toplevel_coords(&PopupKind::Xdg(popup.clone())); | ||
| 330 | + | ||
| 331 | + popup.with_pending_state(|state| { | ||
| 332 | + state.geometry = state.positioner.get_unconstrained_geometry(target); | ||
| 333 | + }); | ||
| 334 | + } | ||
| 335 | + | ||
| 336 | + pub fn unconstrain_layer_shell_popup( | ||
| 337 | + &self, | ||
| 338 | + popup: &PopupSurface, | ||
| 339 | + layer_surface: &LayerSurface, | ||
| 340 | + output: &Output, | ||
| 341 | + ) { | ||
| 342 | + let output_geo = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 343 | + let map = layer_map_for_output(output); | ||
| 344 | + let Some(layer_geo) = map.layer_geometry(layer_surface) else { | ||
| 345 | + return; | ||
| 346 | + }; | ||
| 347 | + | ||
| 348 | + // The target geometry for the positioner should be relative to its parent's geometry, so | ||
| 349 | + // we will compute that here. | ||
| 350 | + let mut target = Rectangle::from_loc_and_size((0, 0), output_geo.size); | ||
| 351 | + target.loc -= layer_geo.loc; | ||
| 352 | + target.loc -= get_popup_toplevel_coords(&PopupKind::Xdg(popup.clone())); | ||
| 353 | + | ||
| 354 | + popup.with_pending_state(|state| { | ||
| 355 | + state.geometry = state.positioner.get_unconstrained_geometry(target); | ||
| 356 | + }); | ||
| 357 | + } | ||
| 358 | + | ||
| 359 | + pub fn update_reactive_popups(&self, window: &Window, output: &Output) { | ||
| 360 | + let _span = tracy_client::span!("Niri::update_reactive_popups"); | ||
| 361 | + | ||
| 362 | + for (popup, _) in PopupManager::popups_for_surface(window.toplevel().wl_surface()) { | ||
| 363 | + match popup { | ||
| 364 | + PopupKind::Xdg(ref popup) => { | ||
| 365 | + if popup.with_pending_state(|state| state.positioner.reactive) { | ||
| 366 | + self.unconstrain_window_popup(popup, window, output); | ||
| 367 | + if let Err(err) = popup.send_pending_configure() { | ||
| 368 | + warn!("error re-configuring reactive popup: {err:?}"); | ||
| 369 | + } | ||
| 370 | + } | ||
| 371 | + } | ||
| 372 | + PopupKind::InputMethod(_) => (), | ||
| 373 | + } | ||
| 374 | + } | ||
| 375 | + } | ||
| 292 | } | 376 | } |
src/layout.rsmodified+27 -0
| @@ -795,6 +795,33 @@ impl<W: LayoutElement> Layout<W> { | |||
| 795 | None | 795 | None |
| 796 | } | 796 | } |
| 797 | 797 | ||
| 798 | + pub fn window_y(&self, window: &W) -> Option<i32> { | ||
| 799 | + match &self.monitor_set { | ||
| 800 | + MonitorSet::Normal { monitors, .. } => { | ||
| 801 | + for mon in monitors { | ||
| 802 | + for ws in &mon.workspaces { | ||
| 803 | + for col in &ws.columns { | ||
| 804 | + if let Some(idx) = col.windows.iter().position(|w| w == window) { | ||
| 805 | + return Some(col.window_y(idx)); | ||
| 806 | + } | ||
| 807 | + } | ||
| 808 | + } | ||
| 809 | + } | ||
| 810 | + } | ||
| 811 | + MonitorSet::NoOutputs { workspaces, .. } => { | ||
| 812 | + for ws in workspaces { | ||
| 813 | + for col in &ws.columns { | ||
| 814 | + if let Some(idx) = col.windows.iter().position(|w| w == window) { | ||
| 815 | + return Some(col.window_y(idx)); | ||
| 816 | + } | ||
| 817 | + } | ||
| 818 | + } | ||
| 819 | + } | ||
| 820 | + } | ||
| 821 | + | ||
| 822 | + None | ||
| 823 | + } | ||
| 824 | + | ||
| 798 | pub fn update_output_size(&mut self, output: &Output) { | 825 | pub fn update_output_size(&mut self, output: &Output) { |
| 799 | let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set else { | 826 | let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set else { |
| 800 | panic!() | 827 | panic!() |