Skip to content

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

feat(domain): create `SwapContainersCommand` and its handler

Lars Bergere648c0a90bce
diff --git a/LarsWM.Domain/Containers/CommandHandlers/SwapContainersHandler.cs b/LarsWM.Domain/Containers/CommandHandlers/SwapContainersHandler.cs
new file mode 100644
index 00000000..e9da9b8d
--- /dev/null
+++ b/LarsWM.Domain/Containers/CommandHandlers/SwapContainersHandler.cs
@@ -0,0 +1,49 @@
+using LarsWM.Domain.Containers.Commands;
+using LarsWM.Infrastructure.Bussing;
+
+namespace LarsWM.Domain.Containers.CommandHandlers
+{
+ class SwapContainersHandler : ICommandHandler<SwapContainersCommand>
+ {
+ private ContainerService _containerService;
+
+ public SwapContainersHandler(ContainerService containerService)
+ {
+ _containerService = containerService;
+ }
+
+ public dynamic Handle(SwapContainersCommand command)
+ {
+ var firstContainer = command.FirstContainer;
+ var secondContainer = command.SecondContainer;
+
+ // Keep references to the original parents.
+ var firstParent = firstContainer.Parent;
+ var secondParent = secondContainer.Parent;
+
+ var firstContainerIndex = firstContainer.SelfAndSiblings.IndexOf(firstContainer);
+ var secondContainerIndex = secondContainer.SelfAndSiblings.IndexOf(secondContainer);
+
+ // 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;
+
+ // 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);
+
+ return CommandResponse.Ok;
+ }
+ }
+}
diff --git a/LarsWM.Domain/Containers/Commands/SwapContainersCommand.cs b/LarsWM.Domain/Containers/Commands/SwapContainersCommand.cs
new file mode 100644
index 00000000..176fbc91
--- /dev/null
+++ b/LarsWM.Domain/Containers/Commands/SwapContainersCommand.cs
@@ -0,0 +1,16 @@
+using LarsWM.Infrastructure.Bussing;
+
+namespace LarsWM.Domain.Containers.Commands
+{
+ public class SwapContainersCommand : Command
+ {
+ public Container FirstContainer { get; }
+ public Container SecondContainer { get; }
+
+ public SwapContainersCommand(Container firstContainer, Container secondContainer)
+ {
+ FirstContainer = firstContainer;
+ SecondContainer = secondContainer;
+ }
+ }
+}
diff --git a/LarsWM.Domain/Containers/Container.cs b/LarsWM.Domain/Containers/Container.cs
index 6c4032e1..bcafc7e9 100644
--- a/LarsWM.Domain/Containers/Container.cs
+++ b/LarsWM.Domain/Containers/Container.cs
@@ -15,7 +15,7 @@ namespace LarsWM.Domain.Containers
public List<Container> Children { get; set; } = new List<Container>();
public Container LastFocusedContainer { get; set; } = null;
- public IEnumerable<Container> SelfAndSiblings => Parent.Children;
+ public List<Container> SelfAndSiblings => Parent.Children;
public IEnumerable<Container> Siblings
{