GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: emit `FocusedContainerMoved` in `move_container_within_tree`
Lars Berger4c31f60b657f
diff --git a/packages/wm/src/common/events/handle_window_moved_or_resized.rs b/packages/wm/src/common/events/handle_window_moved_or_resized.rsindex edccf5af..3cc66b7f 100644--- a/packages/wm/src/common/events/handle_window_moved_or_resized.rs+++ b/packages/wm/src/common/events/handle_window_moved_or_resized.rs@@ -12,7 +12,6 @@ use crate::{commands::resize_window, traits::WindowGetters, NonTilingWindow,TilingWindow, WindowState,},- wm_event::WmEvent,wm_state::WmState,};@@ -35,7 +34,6 @@ pub fn handle_window_moved_or_resized(update_floating_window(non_tiling_window, state)?;}}- _ => (),}}@@ -46,15 +44,16 @@ fn update_tiling_window(window: TilingWindow,state: &mut WmState,) -> anyhow::Result<()> {- // Snap window to its original position if it's not being resized.+ // Snap window to its original position if it's the only window in the+ // workspace.if window.parent().context("No parent.")?.is_workspace()&& window.tiling_siblings().count() == 0{- state.containers_to_redraw.push(window.clone().into());+ state.containers_to_redraw.push(window.into());return Ok(());}- let monitor = window.monitor().context("Window has no monitor.")?;+ let monitor = window.monitor().context("No monitor.")?;// Remove invisible borders from current placement to be able to compare// window width/height.@@ -81,13 +80,11 @@ fn update_tiling_window()?;resize_window(- window.clone().into(),+ window.into(),ResizeDimension::Height,LengthValue::new_px(delta_height as f32),state,- )?;-- Ok(())+ )}fn update_floating_window(@@ -99,8 +96,7 @@ fn update_floating_window(window.set_floating_placement(new_placement);// Change floating window's parent workspace if moved out of its bounds.- update_parent_workspace(window.clone().into(), state)?;- Ok(())+ update_parent_workspace(window.clone().into(), state)}fn update_parent_workspace(@@ -110,9 +106,8 @@ fn update_parent_workspace(// Get workspace that encompasses most of the window.let target_workspace = state.nearest_monitor(&window.native())- .context("Couldn't find nearest monitor.")?- .displayed_workspace()- .context("Monitor has no displayed workspace.")?;+ .and_then(|monitor| monitor.displayed_workspace())+ .context("Failed to get workspace of nearest monitor.")?;// Ignore if window is still within the bounds of its current workspace.if target_workspace.id()@@ -126,13 +121,6 @@ fn update_parent_workspace(window.clone().into(),target_workspace.clone().into(),target_workspace.child_count(),- )?;-- if window.has_focus() {- state.emit_event(WmEvent::FocusedContainerMoved {- focused_container: window.into(),- });- }-- Ok(())+ state,+ )}diff --git a/packages/wm/src/containers/commands/move_container_within_tree.rs b/packages/wm/src/containers/commands/move_container_within_tree.rsindex cc18f9b1..34d7ee1c 100644--- a/packages/wm/src/containers/commands/move_container_within_tree.rs+++ b/packages/wm/src/containers/commands/move_container_within_tree.rs@@ -3,14 +3,25 @@ use anyhow::Context;use crate::{common::VecDequeExt,containers::{traits::CommonGetters, Container},+ wm_event::WmEvent,+ wm_state::WmState,};use super::{attach_container, detach_container, 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+/// the specified index.+///+/// If this container is a tiling container, its siblings are resized on+/// 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.pub fn move_container_within_tree(container_to_move: Container,target_parent: Container,target_index: usize,+ state: &WmState,) -> anyhow::Result<()> {// Get lowest common ancestor (LCA) between `container_to_move` and// `target_parent`. This could be the `target_parent` itself.@@ -21,7 +32,13 @@ pub fn move_container_within_tree(if container_to_move.parent().context("No parent.")? == target_parent {target_parent.borrow_children_mut()- .shift_to_index(target_index, container_to_move);+ .shift_to_index(target_index, container_to_move.clone());++ if container_to_move.has_focus() {+ state.emit_event(WmEvent::FocusedContainerMoved {+ focused_container: container_to_move.into(),+ });+ }return Ok(());}@@ -33,6 +50,7 @@ pub fn move_container_within_tree(container_to_move,lowest_common_ancestor,target_index,+ state,);}@@ -61,9 +79,14 @@ pub fn move_container_within_tree(|| container_to_move_ancestor.descendant_focus_order().next()- .context("TODO.")?- .self_and_ancestors()- .any(|ancestor| ancestor == container_to_move);+ .map(|last_focused| {+ last_focused+ .self_and_ancestors()+ .any(|ancestor| ancestor == container_to_move)+ })+ .context(+ "Failed to get whether container is the focused descendant.",+ )?;// Get whether the ancestor of `container_to_move` appears before// `target_parent`'s ancestor in the child focus order of the LCA.@@ -71,6 +94,7 @@ 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(),@@ -83,7 +107,7 @@ pub fn move_container_within_tree(// not the last focused within that subtree).if is_subtree_focused {set_focused_descendant(- container_to_move,+ container_to_move.clone(),Some(target_parent_ancestor.clone()),);}@@ -97,6 +121,12 @@ pub fn move_container_within_tree(.shift_to_index(original_focus_index, target_parent_ancestor.id());}+ if container_to_move.has_focus() {+ state.emit_event(WmEvent::FocusedContainerMoved {+ focused_container: container_to_move.into(),+ });+ }+Ok(())}@@ -104,6 +134,7 @@ fn move_to_lowest_common_ancestor(container_to_move: Container,lowest_common_ancestor: Container,target_index: usize,+ state: &WmState,) -> anyhow::Result<()> {// Keep reference to focus index of container's ancestor in LCA's child// focus order.@@ -112,8 +143,8 @@ fn move_to_lowest_common_ancestor(.find(|ancestor| {ancestor.parent() == Some(lowest_common_ancestor.clone())})- .context("TODO.")?- .focus_index();+ .map(|ancestor| ancestor.focus_index())+ .context("Unable to get focus index of container's ancestor.")?;detach_container(container_to_move.clone())?;@@ -133,6 +164,12 @@ fn move_to_lowest_common_ancestor(.borrow_child_focus_order_mut().shift_to_index(original_focus_index, container_to_move.id());+ if container_to_move.has_focus() {+ state.emit_event(WmEvent::FocusedContainerMoved {+ focused_container: container_to_move.into(),+ });+ }+Ok(())}diff --git a/packages/wm/src/containers/commands/toggle_tiling_direction.rs b/packages/wm/src/containers/commands/toggle_tiling_direction.rsindex 5ba07b63..c736ca22 100644--- a/packages/wm/src/containers/commands/toggle_tiling_direction.rs+++ b/packages/wm/src/containers/commands/toggle_tiling_direction.rs@@ -25,10 +25,10 @@ pub fn toggle_tiling_direction() -> anyhow::Result<()> {let (modified_id, new_tiling_direction) = match container {Container::TilingWindow(tiling_window) => {- toggle_window_direction(tiling_window, state, config)+ toggle_window_direction(tiling_window, config)}Container::Workspace(workspace) => {- toggle_workspace_direction(workspace, state)+ toggle_workspace_direction(workspace)}// Can only toggle tiling direction from a tiling window or workspace._ => return Ok(()),@@ -44,7 +44,6 @@ pub fn toggle_tiling_direction(fn toggle_window_direction(tiling_window: TilingWindow,- state: &mut WmState,config: &UserConfig,) -> anyhow::Result<(Uuid, TilingDirection)> {let parent = tiling_window@@ -54,10 +53,10 @@ fn toggle_window_direction(// If the window is an only child, then either change the tiling// direction of its parent workspace or flatten its parent split// container.- if !tiling_window.has_siblings() {+ if tiling_window.tiling_siblings().count() == 0 {match parent {DirectionContainer::Workspace(workspace) => {- return toggle_workspace_direction(workspace, state);+ return toggle_workspace_direction(workspace);}DirectionContainer::Split(split_container) => {flatten_split_container(split_container.clone())?;@@ -96,31 +95,8 @@ fn toggle_window_direction(fn toggle_workspace_direction(workspace: Workspace,- state: &mut WmState,) -> anyhow::Result<(Uuid, TilingDirection)> {workspace.set_tiling_direction(workspace.tiling_direction().inverse());- let inverse_split_containers = workspace- .children()- .iter()- .filter_map(|child| match child.as_split() {- Some(split)- if split.tiling_direction() == workspace.tiling_direction() =>- {- Some(split)- }- _ => None,- })- .cloned()- .collect::<Vec<_>>();-- // Flatten any top-level split containers with the same tiling- // direction as the workspace.- for split_container in inverse_split_containers {- flatten_split_container(split_container)?;- }-- state.add_container_to_redraw(workspace.clone().into());-Ok((workspace.id(), workspace.tiling_direction()))}diff --git a/packages/wm/src/monitors/commands/remove_monitor.rs b/packages/wm/src/monitors/commands/remove_monitor.rsindex b67119d6..a8ed473d 100644--- a/packages/wm/src/monitors/commands/remove_monitor.rs+++ b/packages/wm/src/monitors/commands/remove_monitor.rs@@ -45,6 +45,7 @@ pub fn remove_monitor(workspace.clone().into(),target_monitor.clone().into(),target_monitor.child_count(),+ state,)?;state.emit_event(WmEvent::WorkspaceMoved {diff --git a/packages/wm/src/windows/commands/move_window_in_direction.rs b/packages/wm/src/windows/commands/move_window_in_direction.rsindex 5a319b2f..6adba44f 100644--- a/packages/wm/src/windows/commands/move_window_in_direction.rs+++ b/packages/wm/src/windows/commands/move_window_in_direction.rs@@ -3,10 +3,9 @@ use anyhow::Context;use crate::{common::{Direction, TilingDirection},containers::{- commands::{move_container_within_tree, toggle_tiling_direction},+ commands::move_container_within_tree,traits::{CommonGetters, PositionGetters, TilingDirectionGetters},- Container, DirectionContainer, SplitContainer, TilingContainer,- WindowContainer,+ DirectionContainer, TilingContainer, WindowContainer,},user_config::UserConfig,windows::{@@ -107,6 +106,8 @@ fn move_tiling_window(}}+/// Gets the next sibling `TilingWindow` or `SplitContainer` in the given+/// direction.fn tiling_sibling_in_direction(window: TilingWindow,direction: &Direction,@@ -114,10 +115,10 @@ fn tiling_sibling_in_direction(match direction {Direction::Up | Direction::Left => window.prev_siblings()- .find_map(|c| c.as_tiling_container().ok()),+ .find_map(|sibling| sibling.as_tiling_container().ok()),_ => window.next_siblings()- .find_map(|c| c.as_tiling_container().ok()),+ .find_map(|sibling| sibling.as_tiling_container().ok()),}}@@ -136,6 +137,7 @@ fn move_to_sibling_container(window_to_move.clone().into(),parent,sibling_window.index(),+ state,)?;state@@ -169,8 +171,10 @@ fn move_to_sibling_container(window_to_move.into(),target_parent.clone().into(),target_index,+ state,)?;+ // TODO: Only redraw tiling containers.state.containers_to_redraw.extend([target_parent.into(), parent.into()]);@@ -209,15 +213,17 @@ fn move_to_workspace_in_direction(let target_index = match direction {Direction::Down | Direction::Right => 0,- _ => workspace.child_count() - 1,+ _ => workspace.child_count(),};move_container_within_tree(window_to_move.into(),workspace.clone().into(),target_index,+ state,)?;+ // TODO: Only redraw tiling containers.state.containers_to_redraw.extend([workspace.into(), parent.into()]);@@ -232,11 +238,21 @@ fn change_workspace_tiling_direction(state: &mut WmState,config: &UserConfig,) -> anyhow::Result<()> {+ // 1. Change the tiling direction of the workspace.+ // 2. All other containers are wrapped in a split container.+ // 3. Split container is resized to be 50% (and window is also 50%).+ // 4. Depending on direction, place window before/after split container.+let workspace = window_to_move.workspace().context("No workspace.")?;- toggle_tiling_direction(workspace.clone().into(), state, config)?;- state.containers_to_redraw.push(workspace.into());+ // Invert the tiling direction of the workspace.+ workspace.set_tiling_direction(workspace.tiling_direction().inverse());+ state+ .containers_to_redraw+ .extend(workspace.tiling_children().map(Into::into));++ // Re-attempt to swap siblings after changing workspace layout.if let Some(sibling) =tiling_sibling_in_direction(window_to_move.clone(), direction){@@ -264,19 +280,22 @@ fn insert_into_ancestor(}).context("Window ancestor not found.")?;- // Move the window into the container above.let target_index = match direction {Direction::Up | Direction::Left => window_ancestor.index(),_ => window_ancestor.index() + 1,};+ // Move the window into the container above.move_container_within_tree(window_to_move.clone().into(),target_ancestor.clone().into(),target_index,+ state,)?;- state.containers_to_redraw.push(target_ancestor.into());+ state+ .containers_to_redraw+ .extend(target_ancestor.tiling_children().map(Into::into));Ok(())}diff --git a/packages/wm/src/windows/commands/move_window_to_workspace.rs b/packages/wm/src/windows/commands/move_window_to_workspace.rsindex 0814a3b2..5d28d7c0 100644--- a/packages/wm/src/windows/commands/move_window_to_workspace.rs+++ b/packages/wm/src/windows/commands/move_window_to_workspace.rs@@ -50,6 +50,7 @@ fn move_tiling_window_to_workspace(window_container_to_move.clone().into(),target_container,workspace.index(),+ state,)?;Ok(())diff --git a/packages/wm/src/windows/commands/update_window_state.rs b/packages/wm/src/windows/commands/update_window_state.rsindex 953f3f10..93c387df 100644--- a/packages/wm/src/windows/commands/update_window_state.rs+++ b/packages/wm/src/windows/commands/update_window_state.rs@@ -70,6 +70,7 @@ fn set_tiling(tiling_window.clone().into(),target_parent.clone(),target_index,+ state,)?;state.add_container_to_redraw(target_parent);@@ -99,6 +100,7 @@ fn set_non_tiling(window.clone().into(),workspace.clone().into(),workspace.child_count(),+ state,)?;}