Skip to content

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

refactor(domain): replace unnecessary methods with added `Direction` getters

Lars Berger45752055842c
diff --git a/LarsWM.Domain/Containers/ContainerService.cs b/LarsWM.Domain/Containers/ContainerService.cs
index f24a876a..ab7d668a 100644
--- a/LarsWM.Domain/Containers/ContainerService.cs
+++ b/LarsWM.Domain/Containers/ContainerService.cs
@@ -97,8 +97,8 @@ namespace LarsWM.Domain.Containers
}
/// <summary>
- /// Traverse down a split container in search of a `Window` in the inverse direction
- /// of the given `Direction`. For example, get the rightmost Window for `Direction.LEFT`.
+ /// Traverse down a split container in search of a descendant in the given direction. For example,
+ /// get the rightmost container for `Direction.RIGHT`.
/// </summary>
public Container GetDescendantInDirection(Container originContainer, Direction direction)
{
@@ -107,19 +107,16 @@ namespace LarsWM.Domain.Containers
var layout = (originContainer as SplitContainer).Layout;
- var doesNotMatchDirection =
- (layout == Layout.Vertical && (direction == Direction.LEFT || direction == Direction.RIGHT)) ||
- (layout == Layout.Horizontal && (direction == Direction.UP || direction == Direction.DOWN));
-
// TODO: Need to correct focus stack after moving out a container from a vertical split container. With
// the current implementation, the split container still references the moved out container.
- if (doesNotMatchDirection)
+
+ if (layout != direction.GetCorrespondingLayout())
return GetDescendantInDirection(originContainer.LastFocusedContainer, direction);
if (direction == Direction.UP || direction == Direction.LEFT)
- return GetDescendantInDirection(originContainer.Children.First(), direction);
- else
return GetDescendantInDirection(originContainer.Children.Last(), direction);
+ else
+ return GetDescendantInDirection(originContainer.Children.First(), direction);
}
}
}
diff --git a/LarsWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.cs b/LarsWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.cs
index a8221117..109eee3e 100644
--- a/LarsWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.cs
+++ b/LarsWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.cs
@@ -34,7 +34,7 @@ namespace LarsWM.Domain.Windows.CommandHandlers
return CommandResponse.Ok;
var direction = command.Direction;
- var layoutForDirection = GetLayoutForDirection(direction);
+ var layoutForDirection = direction.GetCorrespondingLayout();
var parentMatchesLayout = (focusedWindow.Parent as SplitContainer).Layout == layoutForDirection;
if (parentMatchesLayout && HasSiblingInDirection(focusedWindow, direction))
@@ -48,7 +48,7 @@ namespace LarsWM.Domain.Windows.CommandHandlers
else
{
// Move the focused window into the sibling split container.
- var targetDescendant = _containerService.GetDescendantInDirection(siblingInDirection, direction);
+ var targetDescendant = _containerService.GetDescendantInDirection(siblingInDirection, direction.Inverse());
var targetParent = targetDescendant.Parent as SplitContainer;
var insertionIndex = targetParent.Layout != layoutForDirection || direction == Direction.UP ||
@@ -94,7 +94,7 @@ namespace LarsWM.Domain.Windows.CommandHandlers
if (ancestorWithLayout == null)
{
var workspace = _workspaceService.GetWorkspaceFromChildContainer(focusedWindow);
- workspace.Layout = GetLayoutForDirection(direction);
+ workspace.Layout = direction.GetCorrespondingLayout();
// TODO: Flatten any top-level split containers with the changed layout of the workspace.
// TODO: Index of focused window might have to be changed after layout is inverted.
@@ -118,7 +118,7 @@ namespace LarsWM.Domain.Windows.CommandHandlers
if (insertionReferenceSibling is SplitContainer)
{
// Move the focused window into the adjacent split container.
- var targetDescendant = _containerService.GetDescendantInDirection(insertionReferenceSibling, direction);
+ var targetDescendant = _containerService.GetDescendantInDirection(insertionReferenceSibling, direction.Inverse());
var targetParent = targetDescendant.Parent as SplitContainer;
var insertionIndex = targetParent.Layout != layoutForDirection || direction == Direction.UP ||
@@ -140,15 +140,6 @@ namespace LarsWM.Domain.Windows.CommandHandlers
return CommandResponse.Ok;
}
- /// <summary>
- /// Get the layout that is needed for a move in given direction (eg. a horizontal layout when moving horizontally).
- /// </summary>
- private Layout GetLayoutForDirection(Direction direction)
- {
- return (direction == Direction.LEFT || direction == Direction.RIGHT)
- ? Layout.Horizontal : Layout.Vertical;
- }
-
/// <summary>
/// Whether the focused window has a sibling in the given direction.
/// </summary>
diff --git a/LarsWM.Domain/Windows/Events/FocusedWindowMovedEvent.cs b/LarsWM.Domain/Windows/Events/FocusedWindowMovedEvent.cs
new file mode 100644
index 00000000..89027039
--- /dev/null
+++ b/LarsWM.Domain/Windows/Events/FocusedWindowMovedEvent.cs
@@ -0,0 +1,17 @@
+using LarsWM.Domain.Containers;
+using LarsWM.Infrastructure.Bussing;
+
+namespace LarsWM.Domain.Windows.Events
+{
+ public class FocusedWindowMovedEvent : Event
+ {
+ public Window FocusedWindow { get; }
+ public SplitContainer OriginalParent { get; }
+
+ public FocusedWindowMovedEvent(Window focusedWindow, SplitContainer originalParent)
+ {
+ FocusedWindow = focusedWindow;
+ OriginalParent = originalParent;
+ }
+ }
+}