GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
fix: flatten redundant split containers on move and detach
Lars Berger4b02bf41b2cf
diff --git a/packages/wm/src/containers/commands/detach_container.rs b/packages/wm/src/containers/commands/detach_container.rsindex a0a6ae51..acd34b1e 100644--- a/packages/wm/src/containers/commands/detach_container.rs+++ b/packages/wm/src/containers/commands/detach_container.rs@@ -11,30 +11,22 @@ use super::flatten_split_container;/// Removes a container from the tree.pub fn detach_container(child_to_remove: Container) -> anyhow::Result<()> {- let containers_to_flatten =- child_to_remove.containers_to_flatten_on_detach()?;-- // Get first ancestor that will remain after flattening.- // TODO: Need to flatten parent *before* resizing siblings, but sibling +- // sibling parent need to be flattened *after* resizing siblings.- let adjusted_parent = child_to_remove- .ancestors()- .find(|ancestor| {- containers_to_flatten- .iter()- .any(|to_flatten| to_flatten.id() != ancestor.id())- })- .context("No parent.")?;-- for split_container in containers_to_flatten {- flatten_split_container(split_container)?;+ let mut parent = child_to_remove.parent().context("No parent.")?;++ // Flatten the parent split container if it'll be empty after removing+ // the child.+ if let Some(split_parent) = parent.as_split().cloned() {+ if split_parent.child_count() == 1 {+ parent = parent.parent().context("No parent.")?;+ flatten_split_container(split_parent)?;+ }}- adjusted_parent+ parent.borrow_children_mut().retain(|c| c.id() != child_to_remove.id());- adjusted_parent+ parent.borrow_child_focus_order_mut().retain(|id| *id != child_to_remove.id());@@ -43,8 +35,7 @@ pub fn detach_container(child_to_remove: Container) -> anyhow::Result<()> {// Resize the siblings if it is a tiling container.if let Ok(child_to_remove) = child_to_remove.as_tiling_container() {- let tiling_siblings =- adjusted_parent.tiling_children().collect::<Vec<_>>();+ let tiling_siblings = parent.tiling_children().collect::<Vec<_>>();let tiling_size_increment =child_to_remove.tiling_size() / tiling_siblings.len() as f32;diff --git a/packages/wm/src/containers/commands/flatten_child_split_containers.rs b/packages/wm/src/containers/commands/flatten_child_split_containers.rsnew file mode 100644index 00000000..709f0bcd--- /dev/null+++ b/packages/wm/src/containers/commands/flatten_child_split_containers.rs@@ -0,0 +1,44 @@+use crate::containers::{traits::CommonGetters, Container};++use super::flatten_split_container;++/// Flattens any redundant split containers at the top-level of the given+/// parent container.+///+/// For example:+/// ```+/// H[1 H[V[2, 3]]] -> H[1, 2, 3]+/// H[V[1]] -> H[1]+/// ```+pub fn flatten_child_split_containers(+ parent: Container,+) -> anyhow::Result<()> {+ match parent.child_count() {+ 1 => {+ // Handle case where the parent is a split container and has a single+ // split container child.+ if parent.as_direction_container().is_ok() {+ if let Some(split_child) = parent.children()[0].as_split() {+ flatten_split_container(split_child.clone())?;+ }+ }+ }+ _ => {+ for split_child in parent+ .children()+ .iter()+ .filter_map(|child| child.as_split())+ .filter(|split_child| split_child.child_count() == 1)+ {+ if let Some(split_grandchild) =+ split_child.children()[0].as_split()+ {+ flatten_split_container(split_grandchild.clone())?;+ flatten_split_container(split_child.clone())?;+ }+ }+ }+ }++ Ok(())+}diff --git a/packages/wm/src/containers/commands/mod.rs b/packages/wm/src/containers/commands/mod.rsindex 3f5f2bdf..3f546587 100644--- a/packages/wm/src/containers/commands/mod.rs+++ b/packages/wm/src/containers/commands/mod.rs@@ -1,6 +1,7 @@mod attach_container;mod center_cursor_on_container;mod detach_container;+mod flatten_child_split_containers;mod flatten_split_container;mod focus_in_direction;mod move_container_within_tree;@@ -14,6 +15,7 @@ mod toggle_tiling_direction;pub use attach_container::*;pub use center_cursor_on_container::*;pub use detach_container::*;+pub use flatten_child_split_containers::*;pub use flatten_split_container::*;pub use focus_in_direction::*;pub use move_container_within_tree::*;diff --git a/packages/wm/src/containers/commands/move_container_within_tree.rs b/packages/wm/src/containers/commands/move_container_within_tree.rsindex 34d7ee1c..ffc8bbe3 100644--- a/packages/wm/src/containers/commands/move_container_within_tree.rs+++ b/packages/wm/src/containers/commands/move_container_within_tree.rs@@ -7,7 +7,10 @@ use crate::{wm_state::WmState,};-use super::{attach_container, detach_container, set_focused_descendant};+use super::{+ attach_container, detach_container, flatten_child_split_containers,+ set_focused_descendant,+};/// Move a container to a new location in the tree. This detaches the/// container from its current parent and attaches it to the new parent at@@ -17,12 +20,21 @@ use super::{attach_container, detach_container, set_focused_descendant};/// detach, and the container is sized to the default tiling size with its/// new siblings. No changes to the container's tiling size are made if/// its parent stays the same.+///+/// This will flatten any redundant split containers after moving the+/// container, which can cause the target parent to become detached. For+/// example, in the layout V[1 H[2]] where container 1 is moved down, the+/// parent gets removed resulting in V[1 2].pub fn move_container_within_tree(container_to_move: Container,target_parent: Container,target_index: usize,state: &WmState,) -> anyhow::Result<()> {+ // Create iterator of parent, grandparent, and great-grandparent.+ let ancestors =+ container_to_move.ancestors().take(3).collect::<Vec<_>>();+// Get lowest common ancestor (LCA) between `container_to_move` and// `target_parent`. This could be the `target_parent` itself.let lowest_common_ancestor =@@ -73,7 +85,7 @@ pub fn move_container_within_tree(.context("TODO.")?;// Get whether the container is the focused descendant in its original- // subtree.+ // subtree from the LCA.let is_focused_descendant = container_to_move== container_to_move_ancestor|| container_to_move_ancestor@@ -94,7 +106,6 @@ pub fn move_container_within_tree(let is_subtree_focused =original_focus_index < target_parent_ancestor.focus_index();- // TODO: This will cause a crash if the detach flattens the target parent.detach_container(container_to_move.clone())?;attach_container(&container_to_move.clone(),@@ -121,6 +132,13 @@ pub fn move_container_within_tree(.shift_to_index(original_focus_index, target_parent_ancestor.id());}+ // After moving the container, flatten any redundant split containers.+ // For example, in the layout V[1 H[2]] where container 1 is moved down+ // to become V[H[1 2]], this will then need to be flattened to V[1 2].+ for ancestor in ancestors.iter().rev() {+ flatten_child_split_containers(ancestor.clone())?;+ }+if container_to_move.has_focus() {state.emit_event(WmEvent::FocusedContainerMoved {focused_container: container_to_move.into(),diff --git a/packages/wm/src/windows/commands/unmanage_window.rs b/packages/wm/src/windows/commands/unmanage_window.rsindex 0df991c0..a1e68567 100644--- a/packages/wm/src/windows/commands/unmanage_window.rs+++ b/packages/wm/src/windows/commands/unmanage_window.rs@@ -2,7 +2,10 @@ use anyhow::Context;use crate::{containers::{- commands::{detach_container, set_focused_descendant},+ commands::{+ detach_container, flatten_child_split_containers,+ set_focused_descendant,+ },traits::CommonGetters,WindowContainer,},@@ -15,8 +18,7 @@ pub fn unmanage_window(window: WindowContainer,state: &mut WmState,) -> anyhow::Result<()> {- // Create iterator of window's parent, grandparent, and great-- // grandparent.+ // Create iterator of parent, grandparent, and great-grandparent.let ancestors = window.ancestors().take(3).collect::<Vec<_>>();// Get container to switch focus to after the window has been removed.@@ -25,6 +27,13 @@ pub fn unmanage_window(detach_container(window.clone().into())?;+ // After detaching the container, flatten any redundant split containers.+ // For example, in the layout V[1 H[2]] where container 1 is detached to+ // become V[H[2]], this will then need to be flattened to V[2].+ for ancestor in ancestors.iter().rev() {+ flatten_child_split_containers(ancestor.clone())?;+ }+state.emit_event(WmEvent::WindowUnmanaged {unmanaged_id: window.id(),unmanaged_handle: window.native().handle,