GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
refactor(domain): split out logic for moving within tree to added `MoveContainerWithinTreeCommand`
Lars Bergerde4df272939d
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.csnew file mode 100644index 00000000..536d7cec--- /dev/null+++ b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs@@ -0,0 +1,70 @@+using System;+using System.Linq;+using GlazeWM.Domain.Common.Enums;+using GlazeWM.Domain.Containers.Commands;+using GlazeWM.Infrastructure.Bussing;+using GlazeWM.Infrastructure.Utils;++namespace GlazeWM.Domain.Containers.CommandHandlers+{+ class MoveContainerWithinTreeHandler : ICommandHandler<MoveContainerWithinTreeCommand>+ {+ private Bus _bus;+ private ContainerService _containerService;++ public MoveContainerWithinTreeHandler(Bus bus, ContainerService containerService)+ {+ _bus = bus;+ _containerService = containerService;+ }++ public CommandResponse Handle(MoveContainerWithinTreeCommand command)+ {+ var container = command.Container;+ var target = command.Target;+ var insertionPosition = command.InsertionPosition;++ // Get lowest common ancestor (LCA) between `container` and `target`.+ var lowestCommonAncestor = _containerService.GetLowestCommonAncestor(container, target);++ // Get ancestors of `container` and `target` that are direct children of the LCA. This could+ // be the `container` or `target` itself if they are direct children of the LCA.+ var containerAncestor = container.SelfAndAncestors+ .First(ancestor => ancestor.Parent == lowestCommonAncestor);+ var targetAncestor = target.SelfAndAncestors+ .First(ancestor => ancestor.Parent == lowestCommonAncestor);++ if (containerAncestor == targetAncestor)+ throw new Exception("Container ancestor is the same as target ancestor. This is a bug.");++ // Get whether the container is the focused descendant in its original subtree.+ var isFocusedDescendant = container == containerAncestor+ ? true : containerAncestor.LastFocusedDescendant == container;++ // Get whether the ancestor of `container` appears before `target`'s ancestor in the+ // `ChildFocusOrder` of LCA.+ var originalFocusIndex = containerAncestor.FocusIndex;+ var shouldFocusBefore = originalFocusIndex < targetAncestor.FocusIndex;++ _bus.Invoke(new DetachContainerCommand(container));++ var index = insertionPosition == InsertionPosition.BEFORE ? target.Index : target.Index + 1;+ _bus.Invoke(new AttachContainerCommand(target.Parent as SplitContainer, container, index));++ // Set `container` as focus descendant within target subtree if its original subtree had focus+ // more recently (even if the container is not the last focused within that subtree).+ if (shouldFocusBefore)+ _bus.Invoke(new SetFocusedDescendantCommand(container, targetAncestor));++ // If the focused descendant is moved to the targets subtree, then the target's ancestor+ // should be placed before the original ancestor in LCA's `ChildFocusOrder`.+ if (isFocusedDescendant && shouldFocusBefore)+ lowestCommonAncestor.ChildFocusOrder.ShiftToIndex(+ originalFocusIndex,+ targetAncestor+ );++ return CommandResponse.Ok;+ }+ }+}diff --git a/GlazeWM.Domain/Containers/Commands/MoveContainerWithinTreeCommand.cs b/GlazeWM.Domain/Containers/Commands/MoveContainerWithinTreeCommand.csnew file mode 100644index 00000000..f3056b95--- /dev/null+++ b/GlazeWM.Domain/Containers/Commands/MoveContainerWithinTreeCommand.cs@@ -0,0 +1,19 @@+using GlazeWM.Domain.Common.Enums;+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Containers.Commands+{+ public class MoveContainerWithinTreeCommand : Command+ {+ public Container Container { get; }+ public Container Target { get; }+ public InsertionPosition InsertionPosition { get; }++ public MoveContainerWithinTreeCommand(Container container, Container target, InsertionPosition insertionPosition)+ {+ Container = container;+ Target = target;+ InsertionPosition = insertionPosition;+ }+ }+}diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.csindex a1a36acd..5fae10eb 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.cs@@ -1,11 +1,9 @@-using System;-using System.Linq;+using GlazeWM.Domain.Common.Enums;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces;using GlazeWM.Infrastructure.Bussing;-using GlazeWM.Infrastructure.Utils;namespace GlazeWM.Domain.Windows.CommandHandlers{@@ -81,52 +79,9 @@ namespace GlazeWM.Domain.Windows.CommandHandlersif (insertionTarget == null)_bus.Invoke(new AttachContainerCommand(workspace, tilingWindow));else- {- MoveWithinTree(tilingWindow, insertionTarget, insertionTarget.Index + 1);- }+ _bus.Invoke(new MoveContainerWithinTreeCommand(tilingWindow, insertionTarget, InsertionPosition.AFTER));_bus.Invoke(new RedrawContainersCommand());}-- private void MoveWithinTree(Container container, Container target, int index)- {- // Get lowest common ancestor (LCA) between `container` and `target`.- var lowestCommonAncestor = _containerService.GetLowestCommonAncestor(container, target);-- // Get ancestors of `container` and `target` that are direct children of the LCA. This could- // be the `container` or `target` itself if they are direct children of the LCA.- var containerAncestor = container.SelfAndAncestors- .First(ancestor => ancestor.Parent == lowestCommonAncestor);- var targetAncestor = target.SelfAndAncestors- .First(ancestor => ancestor.Parent == lowestCommonAncestor);-- if (containerAncestor == targetAncestor)- throw new Exception("Container ancestor is the same as target ancestor. This is a bug.");-- // Get whether the container is the focused descendant in its original subtree.- var isFocusedDescendant = container == containerAncestor- ? true : containerAncestor.LastFocusedDescendant == container;-- // Get whether the ancestor of `container` appears before `target`'s ancestor in the- // `ChildFocusOrder` of LCA.- var shouldFocusBefore = containerAncestor.FocusIndex < targetAncestor.FocusIndex;- var originalFocusIndex = containerAncestor.FocusIndex;-- _bus.Invoke(new DetachContainerCommand(container));- _bus.Invoke(new AttachContainerCommand(target.Parent as SplitContainer, container, index));-- // Set `container` as focus descendant within target subtree if its original subtree had focus- // more recently (even if the container is not the last focused within that subtree).- if (shouldFocusBefore)- _bus.Invoke(new SetFocusedDescendantCommand(container, targetAncestor));-- // If the focused descendant is moved to the targets subtree, then the target's ancestor- // should be placed before the original ancestor in LCA's `ChildFocusOrder`.- if (isFocusedDescendant && shouldFocusBefore)- lowestCommonAncestor.ChildFocusOrder.ShiftToIndex(- originalFocusIndex,- targetAncestor- );- }}}