GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
refactor: move out `wrap_in_split_container` into its own command
Lars Berger77276c480404
diff --git a/packages/wm/src/containers/commands/mod.rs b/packages/wm/src/containers/commands/mod.rsindex f32a149d..70569418 100644--- a/packages/wm/src/containers/commands/mod.rs+++ b/packages/wm/src/containers/commands/mod.rs@@ -9,6 +9,7 @@ mod replace_container;mod resize_tiling_container;mod set_focused_descendant;mod toggle_tiling_direction;+mod wrap_in_split_container;pub use attach_container::*;pub use center_cursor_on_container::*;@@ -21,3 +22,4 @@ pub use replace_container::*;pub use resize_tiling_container::*;pub use set_focused_descendant::*;pub use toggle_tiling_direction::*;+pub use wrap_in_split_container::*;diff --git a/packages/wm/src/containers/commands/toggle_tiling_direction.rs b/packages/wm/src/containers/commands/toggle_tiling_direction.rsindex c736ca22..7f32dbdd 100644--- a/packages/wm/src/containers/commands/toggle_tiling_direction.rs+++ b/packages/wm/src/containers/commands/toggle_tiling_direction.rs@@ -14,9 +14,7 @@ use crate::{workspaces::Workspace,};-use super::{- attach_container, flatten_split_container, replace_container,-};+use super::{flatten_split_container, wrap_in_split_container};pub fn toggle_tiling_direction(container: Container,@@ -75,19 +73,10 @@ fn toggle_window_direction(config.value.gaps.inner_gap.clone(),);- // Replace the window with the wrapping split container. The window- // has to be attached to the split container after the replacement.- replace_container(- split_container.clone().into(),- parent.clone().into(),- tiling_window.index(),- )?;-- // Add the window as a child to the new split container.- attach_container(- &tiling_window.clone().into(),- &split_container.clone().into(),- None,+ wrap_in_split_container(+ split_container.clone(),+ parent.into(),+ vec![tiling_window.clone().into()],)?;Ok((split_container.id(), split_container.tiling_direction()))diff --git a/packages/wm/src/containers/commands/wrap_in_split_container.rs b/packages/wm/src/containers/commands/wrap_in_split_container.rsnew file mode 100644index 00000000..b2056036--- /dev/null+++ b/packages/wm/src/containers/commands/wrap_in_split_container.rs@@ -0,0 +1,83 @@+use std::collections::VecDeque;++use anyhow::Context;++use crate::containers::{+ traits::{CommonGetters, TilingSizeGetters},+ Container, SplitContainer, TilingContainer,+};++pub fn wrap_in_split_container(+ split_container: SplitContainer,+ target_parent: Container,+ target_children: Vec<TilingContainer>,+) -> anyhow::Result<()> {+ let starting_index = target_children+ .iter()+ .map(|child| child.index())+ .min()+ .context("Failed to get starting index.")?;++ target_parent+ .borrow_children_mut()+ .insert(starting_index, split_container.clone().into());++ let starting_focus_index = target_children+ .iter()+ .map(|child| child.focus_index())+ .min()+ .context("Failed to get starting focus index.")?;++ target_parent+ .borrow_child_focus_order_mut()+ .insert(starting_focus_index, split_container.id());++ // Get the total tiling size amongst all children.+ let total_tiling_size = target_children+ .iter()+ .map(|child| child.tiling_size())+ .sum::<f32>();++ let target_children_ids = target_children+ .iter()+ .map(|child| child.id())+ .collect::<Vec<_>>();++ let sorted_focus_ids = target_parent+ .borrow_child_focus_order()+ .iter()+ .filter(|id| target_children_ids.contains(id))+ .cloned()+ .collect::<VecDeque<_>>();++ // Set the split container's parent and tiling size.+ *split_container.borrow_parent_mut() = Some(target_parent.clone());+ split_container.set_tiling_size(total_tiling_size);++ // Move the children from their original parent to the split container.+ for target_child in target_children.iter() {+ *target_child.borrow_parent_mut() =+ Some(split_container.clone().into());++ split_container+ .borrow_children_mut()+ .push_back(target_child.clone().into());++ target_parent+ .borrow_children_mut()+ .retain(|child| child != &target_child.clone().into());++ target_parent+ .borrow_child_focus_order_mut()+ .retain(|id| id != &target_child.id());++ // Scale the tiling size to the new split container.+ target_child+ .set_tiling_size(target_child.tiling_size() / total_tiling_size);+ }++ // Add original focus order to split container.+ *split_container.borrow_child_focus_order_mut() = sorted_focus_ids;++ Ok(())+}diff --git a/packages/wm/src/windows/commands/move_window_in_direction.rs b/packages/wm/src/windows/commands/move_window_in_direction.rsindex a3ebb979..17b72f91 100644--- a/packages/wm/src/windows/commands/move_window_in_direction.rs+++ b/packages/wm/src/windows/commands/move_window_in_direction.rs@@ -1,5 +1,3 @@-use std::collections::VecDeque;-use anyhow::Context;use crate::{@@ -8,13 +6,10 @@ use crate::{commands::{flatten_child_split_containers, flatten_split_container,move_container_within_tree, resize_tiling_container,+ wrap_in_split_container,},- traits::{- CommonGetters, PositionGetters, TilingDirectionGetters,- TilingSizeGetters,- },- Container, DirectionContainer, SplitContainer, TilingContainer,- WindowContainer,+ traits::{CommonGetters, PositionGetters, TilingDirectionGetters},+ DirectionContainer, SplitContainer, TilingContainer, WindowContainer,},monitors::Monitor,user_config::UserConfig,@@ -319,81 +314,6 @@ fn invert_workspace_tiling_direction(Ok(())}-fn wrap_in_split_container(- split_container: SplitContainer,- target_parent: Container,- target_children: Vec<TilingContainer>,-) -> anyhow::Result<()> {- let starting_index = target_children- .iter()- .map(|child| child.index())- .min()- .context("Failed to get starting index.")?;-- target_parent- .borrow_children_mut()- .insert(starting_index, split_container.clone().into());-- let starting_focus_index = target_children- .iter()- .map(|child| child.focus_index())- .min()- .context("Failed to get starting focus index.")?;-- target_parent- .borrow_child_focus_order_mut()- .insert(starting_focus_index, split_container.id());-- // Get the total tiling size amongst all children.- let total_tiling_size = target_children- .iter()- .map(|child| child.tiling_size())- .sum::<f32>();-- let target_children_ids = target_children- .iter()- .map(|child| child.id())- .collect::<Vec<_>>();-- let sorted_focus_ids = target_parent- .borrow_child_focus_order()- .iter()- .filter(|id| target_children_ids.contains(id))- .cloned()- .collect::<VecDeque<_>>();-- // Set the split container's parent and tiling size.- *split_container.borrow_parent_mut() = Some(target_parent.clone());- split_container.set_tiling_size(total_tiling_size);-- // Move the children from their original parent to the split container.- for target_child in target_children.iter() {- *target_child.borrow_parent_mut() =- Some(split_container.clone().into());-- split_container- .borrow_children_mut()- .push_back(target_child.clone().into());-- target_parent- .borrow_children_mut()- .retain(|child| child != &target_child.clone().into());-- target_parent- .borrow_child_focus_order_mut()- .retain(|id| id != &target_child.id());-- // Scale the tiling size to the new split container.- target_child- .set_tiling_size(target_child.tiling_size() / total_tiling_size);- }-- // Add original focus order to split container.- *split_container.borrow_child_focus_order_mut() = sorted_focus_ids;-- Ok(())-}-fn insert_into_ancestor(window_to_move: TilingWindow,target_ancestor: DirectionContainer,