Skip to content

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

feat(domain): handle case where container is inserted as child of LCA in `MoveContainerWithinTreeHandler`

Lars Bergere46c74986c9b
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
index 01a96b07..1548203d 100644
--- a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
+++ b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
@@ -1,6 +1,4 @@
-using System;
-using System.Linq;
-using GlazeWM.Domain.Common.Enums;
+using System.Linq;
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Infrastructure.Utils;
@@ -24,13 +22,20 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
var targetParent = command.TargetParent;
var targetIndex = command.TargetIndex;
- // TODO: Handle case where target parent doesn't have children.
- // TODO: Throw error if `target` is null.
+ // TODO: Handle case where target parent doesn't have children. Not sure if this is an issue anymore.
- // Get lowest common ancestor (LCA) between `container` and `target`. This could be the
+ // Get lowest common ancestor (LCA) between `container` and `targetParent`. This could be the
// `targetParent` itself.
var lowestCommonAncestor = _containerService.GetLowestCommonAncestor(container, targetParent);
+ if (targetParent == lowestCommonAncestor)
+ {
+ MoveToLowestCommonAncestor(container, lowestCommonAncestor, targetIndex);
+ return CommandResponse.Ok;
+ }
+
+ // Get ancestors of `container` and `targetParent` that are direct children of the LCA. This
+ // could be the `container` or `targetParent` itself.
var containerAncestor = container.SelfAndAncestors
.First(ancestor => ancestor.Parent == lowestCommonAncestor);
@@ -38,73 +43,49 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
var isFocusedDescendant = container == containerAncestor
? true : containerAncestor.LastFocusedDescendant == container;
- var targetParentAncestor = targetParent == lowestCommonAncestor
- ? null : targetParent.SelfAndAncestors.First(ancestor => ancestor.Parent == lowestCommonAncestor);
-
+ var targetParentAncestor = targetParent.SelfAndAncestors
+ .First(ancestor => ancestor.Parent == lowestCommonAncestor);
- // Get whether the ancestor of `container` appears before `target`'s ancestor in the
+ // Get whether the ancestor of `container` appears before `targetParent`'s ancestor in the
// `ChildFocusOrder` of LCA.
var originalFocusIndex = containerAncestor.FocusIndex;
- var isSubtreeFocusedBefore = originalFocusIndex < targetAncestor.FocusIndex;
+ var isSubtreeFocused = originalFocusIndex < targetParentAncestor.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));
- }
+ // Set `container` 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).
+ if (isSubtreeFocused)
+ _bus.Invoke(new SetFocusedDescendantCommand(container, targetParentAncestor));
// 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 && isSubtreeFocused)
+ lowestCommonAncestor.ChildFocusOrder.ShiftToIndex(
+ originalFocusIndex,
+ targetParentAncestor
+ );
+ return CommandResponse.Ok;
+ }
-
-
-
- // 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.
+ private void MoveToLowestCommonAncestor(Container container, Container lowestCommonAncestor, int targetIndex)
+ {
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));
+ // TODO: Handle case where target index changes on detach of container. For example, when shifting
+ // a top-level container to the right in a workspace.
- _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)
- _bus.Invoke(new SetFocusedDescendantCommand(container, targetAncestor));
+ _bus.Invoke(new DetachContainerCommand(container));
- // 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 && isSubtreeFocusedBefore)
- lowestCommonAncestor.ChildFocusOrder.ShiftToIndex(
- originalFocusIndex,
- targetAncestor
- );
+ _bus.Invoke(new AttachContainerCommand(lowestCommonAncestor as SplitContainer, container, targetIndex));
- return CommandResponse.Ok;
+ lowestCommonAncestor.ChildFocusOrder.ShiftToIndex(originalFocusIndex, container);
}
}
}
diff --git a/GlazeWM.Domain/Containers/Commands/MoveContainerWithinTreeCommand.cs b/GlazeWM.Domain/Containers/Commands/MoveContainerWithinTreeCommand.cs
index c3af53bf..72dc1bd5 100644
--- a/GlazeWM.Domain/Containers/Commands/MoveContainerWithinTreeCommand.cs
+++ b/GlazeWM.Domain/Containers/Commands/MoveContainerWithinTreeCommand.cs
@@ -1,5 +1,4 @@
-using GlazeWM.Domain.Common.Enums;
-using GlazeWM.Infrastructure.Bussing;
+using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.Containers.Commands
{
@@ -8,14 +7,12 @@ namespace GlazeWM.Domain.Containers.Commands
public Container Container { get; }
public Container TargetParent { get; }
public int TargetIndex { get; }
- public InsertionPosition InsertionPosition { get; }
- public MoveContainerWithinTreeCommand(Container container, Container targetParent, int targetIndex, InsertionPosition insertionPosition)
+ public MoveContainerWithinTreeCommand(Container container, Container targetParent, int targetIndex)
{
Container = container;
TargetParent = targetParent;
TargetIndex = targetIndex;
- InsertionPosition = insertionPosition;
}
}
}
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.cs
index 6538c728..97834478 100644
--- a/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.cs
+++ b/GlazeWM.Domain/Windows/CommandHandlers/ToggleFloatingHandler.cs
@@ -1,4 +1,3 @@
-using GlazeWM.Domain.Common.Enums;
using GlazeWM.Domain.Containers;
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.Windows.Commands;
@@ -83,8 +82,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
new MoveContainerWithinTreeCommand(
tilingWindow,
insertionTarget.Parent,
- insertionTarget.Index,
- InsertionPosition.AFTER
+ insertionTarget.Index + 1
)
);