Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat(domain): progress on changing `MoveContainerWithinTreeHandler` to only use target parent + index

Lars Berger2d02b9f6fd75
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
index d3acb302..01a96b07 100644
--- a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
+++ b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
@@ -23,14 +23,50 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
var container = command.Container;
var targetParent = command.TargetParent;
var targetIndex = command.TargetIndex;
- var insertionPosition = command.InsertionPosition;
// TODO: Handle case where target parent doesn't have children.
// TODO: Throw error if `target` is null.
- // Get lowest common ancestor (LCA) between `container` and `target`.
- var target = targetParent.Children[targetIndex];
- var lowestCommonAncestor = _containerService.GetLowestCommonAncestor(container, target);
+ // Get lowest common ancestor (LCA) between `container` and `target`. This could be the
+ // `targetParent` itself.
+ var lowestCommonAncestor = _containerService.GetLowestCommonAncestor(container, targetParent);
+
+ var containerAncestor = container.SelfAndAncestors
+ .First(ancestor => ancestor.Parent == lowestCommonAncestor);
+
+ // Get whether the container is the focused descendant in its original subtree.
+ var isFocusedDescendant = container == containerAncestor
+ ? true : containerAncestor.LastFocusedDescendant == container;
+
+ var targetParentAncestor = targetParent == lowestCommonAncestor
+ ? null : targetParent.SelfAndAncestors.First(ancestor => ancestor.Parent == lowestCommonAncestor);
+
+
+ // Get whether the ancestor of `container` appears before `target`'s ancestor in the
+ // `ChildFocusOrder` of LCA.
+ var originalFocusIndex = containerAncestor.FocusIndex;
+ var isSubtreeFocusedBefore = originalFocusIndex < targetAncestor.FocusIndex;
+
+ _bus.Invoke(new DetachContainerCommand(container));
+
+ _bus.Invoke(new AttachContainerCommand(targetParent as SplitContainer, container, targetIndex));
+
+ // 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 (isSubtreeFocusedBefore)
+ {
+ var newContainerAncestor = container.SelfAndAncestors
+ .First(ancestor => ancestor.Parent == lowestCommonAncestor);
+
+ _bus.Invoke(new SetFocusedDescendantCommand(container, newContainerAncestor));
+ }
+
+ // 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`.
+
+
+
+
// 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.
@@ -53,17 +89,16 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
_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));
+ _bus.Invoke(new AttachContainerCommand(targetParent as SplitContainer, container, targetIndex));
// 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)
+ if (isSubtreeFocusedBefore)
_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)
+ if (isFocusedDescendant && isSubtreeFocusedBefore)
lowestCommonAncestor.ChildFocusOrder.ShiftToIndex(
originalFocusIndex,
targetAncestor