GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat(domain): add `MoveContainerWithinTreeCommand` arg for whether container and its siblings should be resized
Lars Bergeraedf5d47fbd4
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.csindex 67012c78..7a00ead9 100644--- a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs+++ b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs@@ -1,5 +1,4 @@-using System;-using System.Linq;+using System.Linq;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.Utils;@@ -22,14 +21,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlersvar containerToMove = command.ContainerToMove;var targetParent = command.TargetParent;var targetIndex = command.TargetIndex;-- var containerAtTargetIndex = targetParent.Children.ElementAtOrDefault(targetIndex);-- if (targetParent == containerToMove.Parent && containerAtTargetIndex != null)- {- SwapSiblingContainers(containerToMove, containerAtTargetIndex);- return CommandResponse.Ok;- }+ var shouldAdjustSize = command.ShouldAdjustSize;// Get lowest common ancestor (LCA) between `containerToMove` and `targetParent`. This could// be the `targetParent` itself.@@ -42,7 +34,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlers// or moving a container to a direct ancestor).if (targetParent == lowestCommonAncestor){- MoveToLowestCommonAncestor(containerToMove, lowestCommonAncestor, targetIndex);+ MoveToLowestCommonAncestor(containerToMove, lowestCommonAncestor, targetIndex, shouldAdjustSize);return CommandResponse.Ok;}@@ -63,9 +55,16 @@ namespace GlazeWM.Domain.Containers.CommandHandlersvar originalFocusIndex = containerToMoveAncestor.FocusIndex;var isSubtreeFocused = originalFocusIndex < targetParentAncestor.FocusIndex;- _bus.Invoke(new DetachAndResizeContainerCommand(containerToMove));-- _bus.Invoke(new AttachAndResizeContainerCommand(containerToMove, targetParent, targetIndex));+ if (shouldAdjustSize)+ {+ _bus.Invoke(new DetachAndResizeContainerCommand(containerToMove));+ _bus.Invoke(new AttachAndResizeContainerCommand(containerToMove, targetParent, targetIndex));+ }+ else+ {+ _bus.Invoke(new DetachContainerCommand(containerToMove));+ _bus.Invoke(new AttachContainerCommand(containerToMove, targetParent, targetIndex));+ }// Set `containerToMove` as focused descendant within target subtree if its original subtree// had focus more recently (even if the container is not the last focused within that subtree).@@ -83,30 +82,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlersreturn CommandResponse.Ok;}- /// <summary>- /// Swap the positions of two containers within `targetParent`. If containers were instead- /// swapped via `MoveToLowestCommonAncestor`, then the containers would not retain their- /// `SizePercentage`.- /// </summary>- private void SwapSiblingContainers(Container firstContainer, Container secondContainer)- {- if (firstContainer.Parent != secondContainer.Parent)- throw new Exception("Cannot swap containers with different parents. This is a bug.");-- // Keep references to the original indices.- var firstContainerIndex = firstContainer.Index;- var secondContainerIndex = secondContainer.Index;-- // Swap positions of the containers. Note that using the parent of the first container is- // arbitrary since both containers have the same parent.- var targetParent = firstContainer.Parent;- targetParent.Children[firstContainerIndex] = secondContainer;- targetParent.Children[secondContainerIndex] = firstContainer;-- _containerService.ContainersToRedraw.Add(targetParent);- }-- private void MoveToLowestCommonAncestor(Container containerToMove, Container lowestCommonAncestor, int targetIndex)+ private void MoveToLowestCommonAncestor(Container containerToMove, Container lowestCommonAncestor, int targetIndex, bool shouldAdjustSize){// Keep reference to focus index of container's ancestor in LCA's `ChildFocusOrder`.var originalFocusIndex = containerToMove.SelfAndAncestors@@ -117,7 +93,10 @@ namespace GlazeWM.Domain.Containers.CommandHandlersvar originalIndex = containerToMove.Index;var originalLcaChildCount = lowestCommonAncestor.Children.Count;- _bus.Invoke(new DetachAndResizeContainerCommand(containerToMove));+ if (shouldAdjustSize)+ _bus.Invoke(new DetachAndResizeContainerCommand(containerToMove));+ else+ _bus.Invoke(new DetachContainerCommand(containerToMove));var newLcaChildCount = lowestCommonAncestor.Children.Count;var shouldAdjustTargetIndex = originalLcaChildCount > newLcaChildCount@@ -127,7 +106,10 @@ namespace GlazeWM.Domain.Containers.CommandHandlers// top-level container to the right in a workspace.var adjustedTargetIndex = shouldAdjustTargetIndex ? targetIndex - 1 : targetIndex;- _bus.Invoke(new AttachAndResizeContainerCommand(containerToMove, lowestCommonAncestor, adjustedTargetIndex));+ if (shouldAdjustSize)+ _bus.Invoke(new AttachAndResizeContainerCommand(containerToMove, lowestCommonAncestor, adjustedTargetIndex));+ else+ _bus.Invoke(new AttachContainerCommand(containerToMove, lowestCommonAncestor, adjustedTargetIndex));lowestCommonAncestor.ChildFocusOrder.ShiftToIndex(originalFocusIndex, containerToMove);}diff --git a/GlazeWM.Domain/Containers/Commands/MoveContainerWithinTreeCommand.cs b/GlazeWM.Domain/Containers/Commands/MoveContainerWithinTreeCommand.csindex a4de6460..d48ba463 100644--- a/GlazeWM.Domain/Containers/Commands/MoveContainerWithinTreeCommand.cs+++ b/GlazeWM.Domain/Containers/Commands/MoveContainerWithinTreeCommand.cs@@ -7,20 +7,23 @@ namespace GlazeWM.Domain.Containers.Commandspublic Container ContainerToMove { get; }public Container TargetParent { get; }public int TargetIndex { get; }+ public bool ShouldAdjustSize { get; }// Insert child as end element if `targetIndex` is not provided.- public MoveContainerWithinTreeCommand(Container containerToMove, Container targetParent)+ public MoveContainerWithinTreeCommand(Container containerToMove, Container targetParent, bool shouldAdjustSize){ContainerToMove = containerToMove;TargetParent = targetParent;TargetIndex = targetParent.Children.Count;+ ShouldAdjustSize = shouldAdjustSize;}- public MoveContainerWithinTreeCommand(Container containerToMove, Container targetParent, int targetIndex)+ public MoveContainerWithinTreeCommand(Container containerToMove, Container targetParent, int targetIndex, bool shouldAdjustSize){ContainerToMove = containerToMove;TargetParent = targetParent;TargetIndex = targetIndex;+ ShouldAdjustSize = shouldAdjustSize;}}}diff --git a/GlazeWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.csindex ea5582ad..6c80217a 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.cs@@ -88,7 +88,14 @@ namespace GlazeWM.Domain.Windows.CommandHandlers// Swap the focused window with sibling in given direction.if (siblingInDirection is Window){- _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, focusedWindow.Parent, siblingInDirection.Index));+ _bus.Invoke(+ new MoveContainerWithinTreeCommand(+ focusedWindow,+ focusedWindow.Parent,+ siblingInDirection.Index,+ false+ )+ );_bus.Invoke(new RedrawContainersCommand());return;}@@ -101,7 +108,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlersvar insertionIndex = targetParent.Layout != layoutForDirection || direction == Direction.UP ||direction == Direction.LEFT ? targetDescendant.Index + 1 : targetDescendant.Index;- _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, targetParent, insertionIndex));+ _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, targetParent, insertionIndex, true));_bus.Invoke(new RedrawContainersCommand());}@@ -116,9 +123,9 @@ namespace GlazeWM.Domain.Windows.CommandHandlers// TODO: Descend into container if possible.if (direction == Direction.UP || direction == Direction.LEFT)- _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, workspaceInDirection));+ _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, workspaceInDirection, true));else- _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, workspaceInDirection, 0));+ _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, workspaceInDirection, 0, true));// Since window has crossed monitors, adjustments might need to be made because of DPI.focusedWindow.HasPendingDpiAdjustment = true;@@ -164,7 +171,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlersvar insertionIndex = targetParent.Layout != layoutForDirection || direction == Direction.UP ||direction == Direction.LEFT ? targetDescendant.Index + 1 : targetDescendant.Index;- _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, targetParent, insertionIndex));+ _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, targetParent, insertionIndex, true));}else{@@ -172,7 +179,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlersvar insertionIndex = (direction == Direction.UP || direction == Direction.LEFT) ?insertionReference.Index : insertionReference.Index + 1;- _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, ancestorWithLayout, insertionIndex));+ _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, ancestorWithLayout, insertionIndex, true));}_bus.Invoke(new RedrawContainersCommand());diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.csindex 252308fe..c1b1cccf 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.cs@@ -49,7 +49,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlers);_bus.Invoke(new ReplaceContainerCommand(floatingWindow, window.Parent, window.Index));- _bus.Invoke(new MoveContainerWithinTreeCommand(floatingWindow, workspace));+ _bus.Invoke(new MoveContainerWithinTreeCommand(floatingWindow, workspace, true));_bus.Invoke(new RedrawContainersCommand());}@@ -71,13 +71,14 @@ namespace GlazeWM.Domain.Windows.CommandHandlers// Insert the created tiling window after the last focused descendant of the workspace.if (insertionTarget == null)- _bus.Invoke(new MoveContainerWithinTreeCommand(tilingWindow, workspace, 0));+ _bus.Invoke(new MoveContainerWithinTreeCommand(tilingWindow, workspace, 0, true));else_bus.Invoke(new MoveContainerWithinTreeCommand(tilingWindow,insertionTarget.Parent,- insertionTarget.Index + 1+ insertionTarget.Index + 1,+ true));diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizeEndedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizeEndedHandler.csindex 9b747a10..9c20ce77 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizeEndedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizeEndedHandler.cs@@ -40,13 +40,14 @@ namespace GlazeWM.Domain.Windows.EventHandlers// Insert the created tiling window after the last focused descendant of the workspace.if (insertionTarget == null)- _bus.Invoke(new MoveContainerWithinTreeCommand(tilingWindow, workspace, 0));+ _bus.Invoke(new MoveContainerWithinTreeCommand(tilingWindow, workspace, 0, true));else_bus.Invoke(new MoveContainerWithinTreeCommand(tilingWindow,insertionTarget.Parent,- insertionTarget.Index + 1+ insertionTarget.Index + 1,+ true));diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.csindex d1cb04c1..fa015fcc 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs@@ -42,7 +42,7 @@ namespace GlazeWM.Domain.Windows.EventHandlersvar workspace = _workspaceService.GetWorkspaceFromChildContainer(window);_bus.Invoke(new ReplaceContainerCommand(minimizedWindow, window.Parent, window.Index));- _bus.Invoke(new MoveContainerWithinTreeCommand(minimizedWindow, workspace));+ _bus.Invoke(new MoveContainerWithinTreeCommand(minimizedWindow, workspace, true));var containerToFocus = workspace.LastFocusedDescendant ?? workspace;diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.csindex 367c8b4e..518236fd 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.cs@@ -59,7 +59,7 @@ namespace GlazeWM.Domain.Windows.EventHandlersreturn;// Change the window's parent workspace.- _bus.Invoke(new MoveContainerWithinTreeCommand(window, targetWorkspace));+ _bus.Invoke(new MoveContainerWithinTreeCommand(window, targetWorkspace, true));}}}diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.csindex c8524e65..b63fb7c5 100644--- a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.cs+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.cs@@ -41,9 +41,9 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers// Insert the focused window into the target workspace.if (insertionTarget == null)- _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, targetWorkspace));+ _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, targetWorkspace, true));else- _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, insertionTarget.Parent, insertionTarget.Index + 1));+ _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, insertionTarget.Parent, insertionTarget.Index + 1, true));// Reassign focus to descendant within the current workspace._bus.Invoke(new FocusWorkspaceCommand(currentWorkspace.Name));