forge
Explore
Log in

Don't you dare go Hollow

Implement floating child stacking above parents

Browse files at this commit

aac54d0ea1a5c95aba698aed583ee3fa9670f18bIvan MolodetskikhParent: 4fe7185
niri-visual-tests/src/test_window.rsmodified+4 -0
@@ -240,6 +240,10 @@ impl LayoutElement for TestWindow {
240240 self.inner.borrow().requested_size
241241 }
242242
243+ fn is_child_of(&self, _parent: &Self) -> bool {
244+ false
245+ }
246+
243247 fn refresh(&self) {}
244248
245249 fn rules(&self) -> &ResolvedWindowRules {
src/handlers/xdg_shell.rsmodified+16 -0
@@ -647,6 +647,22 @@ impl XdgShellHandler for State {
647647 fn title_changed(&mut self, toplevel: ToplevelSurface) {
648648 self.update_window_rules(&toplevel);
649649 }
650+
651+ fn parent_changed(&mut self, toplevel: ToplevelSurface) {
652+ let Some(parent) = toplevel.parent() else {
653+ return;
654+ };
655+
656+ if let Some((mapped, output)) = self.niri.layout.find_window_and_output_mut(&parent) {
657+ let output = output.cloned();
658+ let window = mapped.window.clone();
659+ if self.niri.layout.descendants_added(&window) {
660+ if let Some(output) = output {
661+ self.niri.queue_redraw(&output);
662+ }
663+ }
664+ }
665+ }
650666 }
651667
652668 delegate_xdg_shell!(State);
src/layout/floating.rsmodified+66 -6
@@ -325,7 +325,7 @@ impl<W: LayoutElement> FloatingSpace<W> {
325325
326326 fn add_tile_at(
327327 &mut self,
328- idx: usize,
328+ mut idx: usize,
329329 mut tile: Tile<W>,
330330 pos: Option<Point<f64, Logical>>,
331331 activate: bool,
@@ -353,6 +353,14 @@ impl<W: LayoutElement> FloatingSpace<W> {
353353 self.active_window_id = Some(win.id().clone());
354354 }
355355
356+ // Make sure the tile isn't inserted below its parent.
357+ for (i, tile_above) in self.tiles.iter().enumerate().take(idx) {
358+ if win.is_child_of(tile_above.window()) {
359+ idx = i;
360+ break;
361+ }
362+ }
363+
356364 let mut pos = pos.unwrap_or_else(|| {
357365 let area_size = self.working_area.size.to_point();
358366 let tile_size = tile.tile_size().to_point();
@@ -367,6 +375,8 @@ impl<W: LayoutElement> FloatingSpace<W> {
367375 let data = Data::new(self.working_area, &tile, pos);
368376 self.data.insert(idx, data);
369377 self.tiles.insert(idx, tile);
378+
379+ self.bring_up_descendants_of(idx);
370380 }
371381
372382 pub fn add_tile_above(&mut self, above: &W::Id, tile: Tile<W>) {
@@ -382,6 +392,33 @@ impl<W: LayoutElement> FloatingSpace<W> {
382392 self.add_tile_at(idx, tile, Some(pos), activate);
383393 }
384394
395+ fn bring_up_descendants_of(&mut self, idx: usize) {
396+ let tile = &self.tiles[idx];
397+ let win = tile.window();
398+
399+ // We always maintain the correct stacking order, so walking descendants back to front
400+ // should give us all of them.
401+ let mut descendants: Vec<usize> = Vec::new();
402+ for (i, tile_below) in self.tiles.iter().enumerate().skip(idx + 1).rev() {
403+ let win_below = tile_below.window();
404+ if win_below.is_child_of(win)
405+ || descendants
406+ .iter()
407+ .any(|idx| win_below.is_child_of(self.tiles[*idx].window()))
408+ {
409+ descendants.push(i);
410+ }
411+ }
412+
413+ // Now, descendants is in back-to-front order, and repositioning them in the front-to-back
414+ // order will preserve the subsequent indices and work out right.
415+ let mut idx = idx;
416+ for descendant_idx in descendants.into_iter().rev() {
417+ self.raise_window(descendant_idx, idx);
418+ idx += 1;
419+ }
420+ }
421+
385422 pub fn remove_active_tile(&mut self) -> Option<RemovedTile<W>> {
386423 let id = self.active_window_id.clone()?;
387424 Some(self.remove_tile(&id))
@@ -434,15 +471,22 @@ impl<W: LayoutElement> FloatingSpace<W> {
434471 return false;
435472 };
436473
437- let tile = self.tiles.remove(idx);
438- let data = self.data.remove(idx);
439- self.tiles.insert(0, tile);
440- self.data.insert(0, data);
474+ self.raise_window(idx, 0);
441475 self.active_window_id = Some(id.clone());
476+ self.bring_up_descendants_of(0);
442477
443478 true
444479 }
445480
481+ fn raise_window(&mut self, from_idx: usize, to_idx: usize) {
482+ assert!(to_idx <= from_idx);
483+
484+ let tile = self.tiles.remove(from_idx);
485+ let data = self.data.remove(from_idx);
486+ self.tiles.insert(to_idx, tile);
487+ self.data.insert(to_idx, data);
488+ }
489+
446490 pub fn start_close_animation_for_window(
447491 &mut self,
448492 renderer: &mut GlesRenderer,
@@ -561,6 +605,15 @@ impl<W: LayoutElement> FloatingSpace<W> {
561605 win.request_size(win_size, animate, None);
562606 }
563607
608+ pub fn descendants_added(&mut self, id: &W::Id) -> bool {
609+ let Some(idx) = self.idx_of(id) else {
610+ return false;
611+ };
612+
613+ self.bring_up_descendants_of(idx);
614+ true
615+ }
616+
564617 pub fn update_window(&mut self, id: &W::Id, serial: Option<Serial>) -> bool {
565618 let Some(tile_idx) = self.idx_of(id) else {
566619 return false;
@@ -769,7 +822,7 @@ impl<W: LayoutElement> FloatingSpace<W> {
769822 assert!(self.scale.is_finite());
770823 assert_eq!(self.tiles.len(), self.data.len());
771824
772- for (tile, data) in zip(&self.tiles, &self.data) {
825+ for (i, (tile, data)) in zip(&self.tiles, &self.data).enumerate() {
773826 assert!(Rc::ptr_eq(&self.options, &tile.options));
774827 assert_eq!(self.clock, tile.clock);
775828 assert_eq!(self.scale, tile.scale());
@@ -786,6 +839,13 @@ impl<W: LayoutElement> FloatingSpace<W> {
786839 data2.update(tile);
787840 data2.update_config(self.working_area);
788841 assert_eq!(data, &data2, "tile data must be up to date");
842+
843+ for tile_below in &self.tiles[i + 1..] {
844+ assert!(
845+ !tile_below.window().is_child_of(tile.window()),
846+ "children must be stacked above parents"
847+ );
848+ }
789849 }
790850
791851 if let Some(id) = &self.active_window_id {
src/layout/mod.rsmodified+256 -3
@@ -186,6 +186,8 @@ pub trait LayoutElement {
186186 /// Size previously requested through [`LayoutElement::request_size()`].
187187 fn requested_size(&self) -> Option<Size<i32, Logical>>;
188188
189+ fn is_child_of(&self, parent: &Self) -> bool;
190+
189191 fn rules(&self) -> &ResolvedWindowRules;
190192
191193 /// Runs periodic clean-up tasks.
@@ -1113,6 +1115,16 @@ impl<W: LayoutElement> Layout<W> {
11131115 None
11141116 }
11151117
1118+ pub fn descendants_added(&mut self, id: &W::Id) -> bool {
1119+ for ws in self.workspaces_mut() {
1120+ if ws.descendants_added(id) {
1121+ return true;
1122+ }
1123+ }
1124+
1125+ false
1126+ }
1127+
11161128 pub fn update_window(&mut self, window: &W::Id, serial: Option<Serial>) {
11171129 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
11181130 if move_.tile.window().id() == window {
@@ -3868,6 +3880,7 @@ mod tests {
38683880 #[derive(Debug)]
38693881 struct TestWindowInner {
38703882 id: usize,
3883+ parent_id: Cell<Option<usize>>,
38713884 bbox: Cell<Rectangle<i32, Logical>>,
38723885 initial_bbox: Rectangle<i32, Logical>,
38733886 requested_size: Cell<Option<Size<i32, Logical>>>,
@@ -3884,6 +3897,8 @@ mod tests {
38843897 struct TestWindowParams {
38853898 #[proptest(strategy = "1..=5usize")]
38863899 id: usize,
3900+ #[proptest(strategy = "arbitrary_parent_id()")]
3901+ parent_id: Option<usize>,
38873902 is_floating: bool,
38883903 #[proptest(strategy = "arbitrary_bbox()")]
38893904 bbox: Rectangle<i32, Logical>,
@@ -3895,6 +3910,7 @@ mod tests {
38953910 pub fn new(id: usize) -> Self {
38963911 Self {
38973912 id,
3913+ parent_id: None,
38983914 is_floating: false,
38993915 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
39003916 min_max_size: Default::default(),
@@ -3906,6 +3922,7 @@ mod tests {
39063922 fn new(params: TestWindowParams) -> Self {
39073923 Self(Rc::new(TestWindowInner {
39083924 id: params.id,
3925+ parent_id: Cell::new(params.parent_id),
39093926 bbox: Cell::new(params.bbox),
39103927 initial_bbox: params.bbox,
39113928 requested_size: Cell::new(None),
@@ -4033,6 +4050,10 @@ mod tests {
40334050 self.0.requested_size.get()
40344051 }
40354052
4053+ fn is_child_of(&self, parent: &Self) -> bool {
4054+ self.0.parent_id.get() == Some(parent.0.id)
4055+ }
4056+
40364057 fn refresh(&self) {}
40374058
40384059 fn rules(&self) -> &ResolvedWindowRules {
@@ -4136,6 +4157,13 @@ mod tests {
41364157 ]
41374158 }
41384159
4160+ fn arbitrary_parent_id() -> impl Strategy<Value = Option<usize>> {
4161+ prop_oneof![
4162+ 5 => Just(None),
4163+ 1 => prop::option::of(1..=5usize),
4164+ ]
4165+ }
4166+
41394167 #[derive(Debug, Clone, Copy, Arbitrary)]
41404168 enum Op {
41414169 AddOutput(#[proptest(strategy = "1..=5usize")] usize),
@@ -4195,6 +4223,7 @@ mod tests {
41954223 FocusWindowUpOrColumnRight,
41964224 FocusWindowOrWorkspaceDown,
41974225 FocusWindowOrWorkspaceUp,
4226+ FocusWindow(#[proptest(strategy = "1..=5usize")] usize),
41984227 MoveColumnLeft,
41994228 MoveColumnRight,
42004229 MoveColumnToFirst,
@@ -4265,6 +4294,12 @@ mod tests {
42654294 id: Option<usize>,
42664295 },
42674296 SwitchFocusFloatingTiling,
4297+ SetParent {
4298+ #[proptest(strategy = "1..=5usize")]
4299+ id: usize,
4300+ #[proptest(strategy = "prop::option::of(1..=5usize)")]
4301+ new_parent_id: Option<usize>,
4302+ },
42684303 Communicate(#[proptest(strategy = "1..=5usize")] usize),
42694304 Refresh {
42704305 is_active: bool,
@@ -4446,10 +4481,15 @@ mod tests {
44464481 Op::UnnameWorkspace { ws_name } => {
44474482 layout.unname_workspace(&format!("ws{ws_name}"));
44484483 }
4449- Op::AddWindow { params } => {
4484+ Op::AddWindow { mut params } => {
44504485 if layout.has_window(&params.id) {
44514486 return;
44524487 }
4488+ if let Some(parent_id) = params.parent_id {
4489+ if parent_id_causes_loop(layout, params.id, parent_id) {
4490+ params.parent_id = None;
4491+ }
4492+ }
44534493
44544494 let win = TestWindow::new(params);
44554495 layout.add_window(
@@ -4461,7 +4501,7 @@ mod tests {
44614501 );
44624502 }
44634503 Op::AddWindowRightOf {
4464- params,
4504+ mut params,
44654505 right_of_id,
44664506 } => {
44674507 let mut found_right_of = false;
@@ -4511,10 +4551,19 @@ mod tests {
45114551 return;
45124552 }
45134553
4554+ if let Some(parent_id) = params.parent_id {
4555+ if parent_id_causes_loop(layout, params.id, parent_id) {
4556+ params.parent_id = None;
4557+ }
4558+ }
4559+
45144560 let win = TestWindow::new(params);
45154561 layout.add_window_right_of(&right_of_id, win, None, false, params.is_floating);
45164562 }
4517- Op::AddWindowToNamedWorkspace { params, ws_name } => {
4563+ Op::AddWindowToNamedWorkspace {
4564+ mut params,
4565+ ws_name,
4566+ } => {
45184567 let ws_name = format!("ws{ws_name}");
45194568 let mut found_workspace = false;
45204569
@@ -4567,6 +4616,12 @@ mod tests {
45674616 return;
45684617 }
45694618
4619+ if let Some(parent_id) = params.parent_id {
4620+ if parent_id_causes_loop(layout, params.id, parent_id) {
4621+ params.parent_id = None;
4622+ }
4623+ }
4624+
45704625 let win = TestWindow::new(params);
45714626 layout.add_window_to_named_workspace(
45724627 &ws_name,
@@ -4635,6 +4690,7 @@ mod tests {
46354690 Op::FocusWindowUpOrColumnRight => layout.focus_up_or_right(),
46364691 Op::FocusWindowOrWorkspaceDown => layout.focus_window_or_workspace_down(),
46374692 Op::FocusWindowOrWorkspaceUp => layout.focus_window_or_workspace_up(),
4693+ Op::FocusWindow(id) => layout.activate_window(&id),
46384694 Op::MoveColumnLeft => layout.move_left(),
46394695 Op::MoveColumnRight => layout.move_right(),
46404696 Op::MoveColumnToFirst => layout.move_column_to_first(),
@@ -4740,6 +4796,62 @@ mod tests {
47404796 Op::SwitchFocusFloatingTiling => {
47414797 layout.switch_focus_floating_tiling();
47424798 }
4799+ Op::SetParent {
4800+ id,
4801+ mut new_parent_id,
4802+ } => {
4803+ if !layout.has_window(&id) {
4804+ return;
4805+ }
4806+
4807+ if let Some(parent_id) = new_parent_id {
4808+ if parent_id_causes_loop(layout, id, parent_id) {
4809+ new_parent_id = None;
4810+ }
4811+ }
4812+
4813+ let mut update = false;
4814+
4815+ if let Some(InteractiveMoveState::Moving(move_)) = &layout.interactive_move {
4816+ if move_.tile.window().0.id == id {
4817+ move_.tile.window().0.parent_id.set(new_parent_id);
4818+ update = true;
4819+ }
4820+ }
4821+
4822+ match &mut layout.monitor_set {
4823+ MonitorSet::Normal { monitors, .. } => {
4824+ 'outer: for mon in monitors {
4825+ for ws in &mut mon.workspaces {
4826+ for win in ws.windows() {
4827+ if win.0.id == id {
4828+ win.0.parent_id.set(new_parent_id);
4829+ update = true;
4830+ break 'outer;
4831+ }
4832+ }
4833+ }
4834+ }
4835+ }
4836+ MonitorSet::NoOutputs { workspaces, .. } => {
4837+ 'outer: for ws in workspaces {
4838+ for win in ws.windows() {
4839+ if win.0.id == id {
4840+ win.0.parent_id.set(new_parent_id);
4841+ update = true;
4842+ break 'outer;
4843+ }
4844+ }
4845+ }
4846+ }
4847+ }
4848+
4849+ if update {
4850+ if let Some(new_parent_id) = new_parent_id {
4851+ layout.descendants_added(&new_parent_id);
4852+ }
4853+ }
4854+ }
47434855 Op::Communicate(id) => {
47444856 let mut update = false;
47454857
@@ -6270,6 +6382,147 @@ mod tests {
62706382 assert!(win.0.pending_activated.get());
62716383 }
62726384
6385+ #[test]
6386+ fn stacking_add_parent_brings_up_child() {
6387+ let ops = [
6388+ Op::AddOutput(0),
6389+ Op::AddWindow {
6390+ params: TestWindowParams {
6391+ is_floating: true,
6392+ parent_id: Some(1),
6393+ ..TestWindowParams::new(0)
6394+ },
6395+ },
6396+ Op::AddWindow {
6397+ params: TestWindowParams {
6398+ is_floating: true,
6399+ ..TestWindowParams::new(1)
6400+ },
6401+ },
6402+ ];
6403+
6404+ check_ops(&ops);
6405+ }
6406+
6407+ #[test]
6408+ fn stacking_add_parent_brings_up_descendants() {
6409+ let ops = [
6410+ Op::AddOutput(0),
6411+ Op::AddWindow {
6412+ params: TestWindowParams {
6413+ is_floating: true,
6414+ parent_id: Some(2),
6415+ ..TestWindowParams::new(0)
6416+ },
6417+ },
6418+ Op::AddWindow {
6419+ params: TestWindowParams {
6420+ is_floating: true,
6421+ parent_id: Some(0),
6422+ ..TestWindowParams::new(1)
6423+ },
6424+ },
6425+ Op::AddWindow {
6426+ params: TestWindowParams {
6427+ is_floating: true,
6428+ ..TestWindowParams::new(2)
6429+ },
6430+ },
6431+ ];
6432+
6433+ check_ops(&ops);
6434+ }
6435+
6436+ #[test]
6437+ fn stacking_activate_brings_up_descendants() {
6438+ let ops = [
6439+ Op::AddOutput(0),
6440+ Op::AddWindow {
6441+ params: TestWindowParams {
6442+ is_floating: true,
6443+ ..TestWindowParams::new(0)
6444+ },
6445+ },
6446+ Op::AddWindow {
6447+ params: TestWindowParams {
6448+ is_floating: true,
6449+ parent_id: Some(0),
6450+ ..TestWindowParams::new(1)
6451+ },
6452+ },
6453+ Op::AddWindow {
6454+ params: TestWindowParams {
6455+ is_floating: true,
6456+ parent_id: Some(1),
6457+ ..TestWindowParams::new(2)
6458+ },
6459+ },
6460+ Op::AddWindow {
6461+ params: TestWindowParams {
6462+ is_floating: true,
6463+ ..TestWindowParams::new(3)
6464+ },
6465+ },
6466+ Op::FocusWindow(0),
6467+ ];
6468+
6469+ check_ops(&ops);
6470+ }
6471+
6472+ #[test]
6473+ fn stacking_set_parent_brings_up_child() {
6474+ let ops = [
6475+ Op::AddOutput(0),
6476+ Op::AddWindow {
6477+ params: TestWindowParams {
6478+ is_floating: true,
6479+ ..TestWindowParams::new(0)
6480+ },
6481+ },
6482+ Op::AddWindow {
6483+ params: TestWindowParams {
6484+ is_floating: true,
6485+ ..TestWindowParams::new(1)
6486+ },
6487+ },
6488+ Op::SetParent {
6489+ id: 0,
6490+ new_parent_id: Some(1),
6491+ },
6492+ ];
6493+
6494+ check_ops(&ops);
6495+ }
6496+
6497+ fn parent_id_causes_loop(layout: &Layout<TestWindow>, id: usize, mut parent_id: usize) -> bool {
6498+ if parent_id == id {
6499+ return true;
6500+ }
6501+
6502+ 'outer: loop {
6503+ for (_, win) in layout.windows() {
6504+ if win.0.id == parent_id {
6505+ match win.0.parent_id.get() {
6506+ Some(new_parent_id) => {
6507+ if new_parent_id == id {
6508+ // Found a loop.
6509+ return true;
6510+ }
6511+
6512+ parent_id = new_parent_id;
6513+ continue 'outer;
6514+ }
6515+ // Reached window with no parent.
6516+ None => return false,
6517+ }
6518+ }
6519+ }
6520+
6521+ // Parent is not in the layout.
6522+ return false;
6523+ }
6524+ }
6525+
62736526 fn arbitrary_spacing() -> impl Strategy<Value = f64> {
62746527 // Give equal weight to:
62756528 // - 0: the element is disabled
src/layout/workspace.rsmodified+4 -0
@@ -1127,6 +1127,10 @@ impl<W: LayoutElement> Workspace<W> {
11271127 })
11281128 }
11291129
1130+ pub fn descendants_added(&mut self, id: &W::Id) -> bool {
1131+ self.floating.descendants_added(id)
1132+ }
1133+
11301134 pub fn update_window(&mut self, window: &W::Id, serial: Option<Serial>) {
11311135 if !self.floating.update_window(window, serial) {
11321136 self.scrolling.update_window(window, serial);
src/window/mapped.rsmodified+4 -0
@@ -720,6 +720,10 @@ impl LayoutElement for Mapped {
720720 self.toplevel().with_pending_state(|state| state.size)
721721 }
722722
723+ fn is_child_of(&self, parent: &Self) -> bool {
724+ self.toplevel().parent().as_ref() == Some(parent.toplevel().wl_surface())
725+ }
726+
723727 fn refresh(&self) {
724728 self.window.refresh();
725729 }