GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
fix: partial fix for flattening sibling split containers on detach
Lars Berger6fb540b7e2bd
diff --git a/packages/wm/src/containers/commands/detach_container.rs b/packages/wm/src/containers/commands/detach_container.rsindex afa92926..a0a6ae51 100644--- a/packages/wm/src/containers/commands/detach_container.rs+++ b/packages/wm/src/containers/commands/detach_container.rs@@ -11,22 +11,30 @@ use super::flatten_split_container;/// Removes a container from the tree.pub fn detach_container(child_to_remove: Container) -> anyhow::Result<()> {- 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)?;- }+ 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)?;}- parent+ adjusted_parent.borrow_children_mut().retain(|c| c.id() != child_to_remove.id());- parent+ adjusted_parent.borrow_child_focus_order_mut().retain(|id| *id != child_to_remove.id());@@ -35,40 +43,16 @@ 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() {- resize_sibling_containers(parent, child_to_remove.tiling_size())?;- }-- Ok(())-}--fn resize_sibling_containers(- parent: Container,- tiling_size: f32,-) -> anyhow::Result<()> {- let tiling_siblings = parent.tiling_children().collect::<Vec<_>>();- let tiling_size_increment = tiling_size / tiling_siblings.len() as f32;-- // Adjust size of the siblings based on the freed up space.- for sibling in &tiling_siblings {- sibling.set_tiling_size(sibling.tiling_size() + tiling_size_increment);- }-- // If there is exactly *one* sibling to the detached container, then- // flatten that sibling if it's a split container. This is to handle- // layouts like H[1 V[2 H[3]]], where container 2 gets detached.- if tiling_siblings.len() == 1 {- if let Some(split_sibling) = tiling_siblings[0].as_split().cloned() {- let split_sibling_parent =- split_sibling.parent().context("No parent.")?;+ let tiling_siblings =+ adjusted_parent.tiling_children().collect::<Vec<_>>();- flatten_split_container(split_sibling)?;+ let tiling_size_increment =+ child_to_remove.tiling_size() / tiling_siblings.len() as f32;- // Additionally flatten parent to handle deeply nested layouts.- if let Some(split_sibling_parent) =- split_sibling_parent.as_split().cloned()- {- flatten_split_container(split_sibling_parent)?;- }+ // Adjust size of the siblings based on the freed up space.+ for sibling in &tiling_siblings {+ sibling+ .set_tiling_size(sibling.tiling_size() + tiling_size_increment);}}diff --git a/packages/wm/src/containers/traits/common_getters.rs b/packages/wm/src/containers/traits/common_getters.rsindex f75a2617..aa587750 100644--- a/packages/wm/src/containers/traits/common_getters.rs+++ b/packages/wm/src/containers/traits/common_getters.rs@@ -3,6 +3,7 @@ use std::{collections::VecDeque,};+use anyhow::Context;use enum_dispatch::enum_dispatch;use uuid::Uuid;@@ -277,6 +278,45 @@ pub trait CommonGetters {fn has_focus(&self) -> bool {self.self_and_ancestors().all(|c| c.focus_index() == 0)}++ fn containers_to_flatten_on_detach(+ &self,+ ) -> anyhow::Result<Vec<SplitContainer>> {+ let parent = self.parent().context("No parent.")?;+ let mut containers_to_flatten = Vec::new();++ // Flatten the parent split container if it'll be empty after removing+ // the child.+ if let Some(split_parent) = parent.as_split() {+ if split_parent.child_count() == 1 {+ containers_to_flatten.push(split_parent.clone());+ }+ }++ let tiling_siblings = self.tiling_siblings().collect::<Vec<_>>();++ // If there is exactly *one* sibling to the detached container, then+ // flatten that sibling if it's a split container. This is for layouts+ // like V[1 H[2]] where container 1 gets detached, resulting in V[2].+ if tiling_siblings.len() == 1 {+ if let Some(split_sibling) = tiling_siblings[0].as_split() {+ let split_sibling_parent =+ split_sibling.parent().context("No parent.")?;++ containers_to_flatten.push(split_sibling.clone());++ // Additionally flatten parent in deeply nested layouts. This for+ // layouts like H[1 V[2 H[3]]] where container 2 gets detached,+ // resulting in H[1 3].+ if let Some(split_sibling_parent) = split_sibling_parent.as_split()+ {+ containers_to_flatten.push(split_sibling_parent.clone());+ }+ }+ }++ Ok(containers_to_flatten)+ }}/// An iterator over ancestors of a given container.diff --git a/packages/wm/src/windows/commands/unmanage_window.rs b/packages/wm/src/windows/commands/unmanage_window.rsindex a206d574..0df991c0 100644--- a/packages/wm/src/windows/commands/unmanage_window.rs+++ b/packages/wm/src/windows/commands/unmanage_window.rs@@ -15,12 +15,9 @@ pub fn unmanage_window(window: WindowContainer,state: &mut WmState,) -> anyhow::Result<()> {- let parent = window.parent().context("No parent.")?;- let grandparent = parent.parent().context("No grandparent.")?;-- // Get whether the window's parent will be an empty split container.- let has_empty_split_container =- parent.is_split() && parent.child_count() == 1;+ // Create iterator of window's 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.let focus_target =@@ -43,10 +40,14 @@ pub fn unmanage_window(// Sibling containers need to be redrawn if the window was tiling.if window.state() == WindowState::Tiling {- state.add_container_to_redraw(match has_empty_split_container {- true => grandparent.into(),- false => parent.into(),- });+ let ancestor_to_redraw = ancestors+ .into_iter()+ .find(|ancestor| !ancestor.is_detached())+ .context("No ancestor to redraw.")?;++ state+ .containers_to_redraw+ .extend(ancestor_to_redraw.tiling_children().map(Into::into));}Ok(())