Skip to content

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

refactor(domain): only allow swapping sibling containers in `SwapContainersHandler`

Lars Berger61787d8af5fa
diff --git a/LarsWM.Domain/Containers/CommandHandlers/SwapContainersHandler.cs b/LarsWM.Domain/Containers/CommandHandlers/SwapContainersHandler.cs
index e9da9b8d..d3d87d9a 100644
--- a/LarsWM.Domain/Containers/CommandHandlers/SwapContainersHandler.cs
+++ b/LarsWM.Domain/Containers/CommandHandlers/SwapContainersHandler.cs
@@ -1,4 +1,5 @@
-using LarsWM.Domain.Containers.Commands;
+using System;
+using LarsWM.Domain.Containers.Commands;
using LarsWM.Infrastructure.Bussing;
namespace LarsWM.Domain.Containers.CommandHandlers
@@ -17,31 +18,20 @@ namespace LarsWM.Domain.Containers.CommandHandlers
var firstContainer = command.FirstContainer;
var secondContainer = command.SecondContainer;
- // Keep references to the original parents.
- var firstParent = firstContainer.Parent;
- var secondParent = secondContainer.Parent;
+ if (firstContainer.Parent != secondContainer.Parent)
+ throw new Exception("Attempting to swap containers with different parents. This is a bug.");
- var firstContainerIndex = firstContainer.SelfAndSiblings.IndexOf(firstContainer);
- var secondContainerIndex = secondContainer.SelfAndSiblings.IndexOf(secondContainer);
+ // Keep references to the original indices.
+ var firstContainerIndex = firstContainer.Index;
+ var secondContainerIndex = secondContainer.Index;
- // Swap the containers. Note that `SizePercentage` is not swapped between the containers.
- firstContainer.Parent.Children[firstContainerIndex] = secondContainer;
- firstContainer.Parent = secondParent;
- secondContainer.Parent.Children[secondContainerIndex] = firstContainer;
- secondContainer.Parent = firstParent;
+ // Swap the containers. Note that using the parent of the first container is arbitrary since
+ // both containers have the same parent.
+ var parent = firstContainer.Parent;
+ parent.Children[firstContainerIndex] = secondContainer;
+ parent.Children[secondContainerIndex] = firstContainer;
- // Correct focus stack references to avoid parent containers from referencing containers that
- // aren't children. No need to correct references if the containers have the same parent.
- var isSameParent = firstParent == secondParent;
-
- if (!isSameParent && firstParent.LastFocusedContainer == firstContainer)
- firstParent.LastFocusedContainer = secondContainer;
-
- if (!isSameParent && secondParent.LastFocusedContainer == secondContainer)
- secondParent.LastFocusedContainer = firstContainer;
-
- _containerService.SplitContainersToRedraw.Add(firstContainer.Parent as SplitContainer);
- _containerService.SplitContainersToRedraw.Add(secondContainer.Parent as SplitContainer);
+ _containerService.SplitContainersToRedraw.Add(parent as SplitContainer);
return CommandResponse.Ok;
}