Skip to content

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

feat(domain): avoid resizing siblings of added/removed containers that aren't tiling

Lars Berger8479a3919aa7
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/AttachContainerHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/AttachContainerHandler.cs
index 564bb175..ad0b4a8b 100644
--- a/GlazeWM.Domain/Containers/CommandHandlers/AttachContainerHandler.cs
+++ b/GlazeWM.Domain/Containers/CommandHandlers/AttachContainerHandler.cs
@@ -1,4 +1,6 @@
-using GlazeWM.Domain.Containers.Commands;
+using System.Linq;
+using GlazeWM.Domain.Containers.Commands;
+using GlazeWM.Domain.Windows;
using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.Containers.CommandHandlers
@@ -28,10 +30,8 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
parent.Children.Insert(command.InsertPosition, childToAdd);
childToAdd.Parent = parent;
- // Adjust SizePercentage of self and siblings.
- double defaultPercent = 1.0 / parent.Children.Count;
- foreach (var child in parent.Children)
- child.SizePercentage = defaultPercent;
+ if (childToAdd is TilingWindow || childToAdd is SplitContainer)
+ AdjustSiblingSizes(childToAdd);
_containerService.SplitContainersToRedraw.Add(parent);
@@ -41,5 +41,15 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
return CommandResponse.Ok;
}
+
+ private void AdjustSiblingSizes(Container childToAdd)
+ {
+ var resizableSiblings = childToAdd.SelfAndSiblings.Where(container => container is IResizable);
+ double defaultPercent = 1.0 / resizableSiblings.Count();
+
+ // Adjust `SizePercentage` of the added container and its siblings.
+ foreach (var sibling in resizableSiblings)
+ (sibling as IResizable).SizePercentage = defaultPercent;
+ }
}
}
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/DetachContainerHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/DetachContainerHandler.cs
index c80315dd..33bdc1a3 100644
--- a/GlazeWM.Domain/Containers/CommandHandlers/DetachContainerHandler.cs
+++ b/GlazeWM.Domain/Containers/CommandHandlers/DetachContainerHandler.cs
@@ -1,5 +1,6 @@
using System.Linq;
using GlazeWM.Domain.Containers.Commands;
+using GlazeWM.Domain.Windows;
using GlazeWM.Domain.Workspaces;
using GlazeWM.Infrastructure.Bussing;
@@ -24,7 +25,8 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
parent.ChildFocusOrder.Remove(childToRemove);
parent.RemoveChild(childToRemove);
- AdjustSiblingSizes(parent);
+ if (childToRemove is TilingWindow || childToRemove is SplitContainer)
+ AdjustSiblingSizes(parent);
return CommandResponse.Ok;
}
@@ -34,30 +36,25 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
// Siblings of the removed child.
var siblings = parent.Children;
- var isEmptySplitContainer = !parent.HasChildren() && !(parent is Workspace);
+ var isEmptySplitContainer = parent is SplitContainer && !parent.HasChildren()
+ && !(parent is Workspace);
// If the parent of the removed child is an empty split container, remove
// the split container as well.
if (isEmptySplitContainer)
{
- var grandparent = parent.Parent;
- grandparent.RemoveChild(parent);
- grandparent.ChildFocusOrder.Remove(parent);
-
- // TODO: Perhaps create a private method that takes the container with children
- // to adjust that has the SizePercentage and default percent logic. Alternatively
- // create a variable containerToAdjust that is then operated on.
- foreach (var child in grandparent.Children)
- child.SizePercentage = 1.0 / grandparent.Children.Count;
-
- _containerService.SplitContainersToRedraw.Add(grandparent as SplitContainer);
+ _bus.Invoke(new DetachContainerCommand(parent.Parent as SplitContainer, parent));
return;
}
// TODO: Adjust SizePercentage of children based on their previous SizePercentage.
- foreach (var child in siblings)
- child.SizePercentage = 1.0 / siblings.Count;
+ var resizableSiblings = siblings.Where(container => container is IResizable);
+ double defaultPercent = 1.0 / resizableSiblings.Count();
+
+ // Adjust `SizePercentage` of the siblings of the removed container.
+ foreach (var sibling in resizableSiblings)
+ (sibling as IResizable).SizePercentage = defaultPercent;
_containerService.SplitContainersToRedraw.Add(parent);
}
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/ReplaceContainerHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/ReplaceContainerHandler.cs
index 6743dda2..5370b009 100644
--- a/GlazeWM.Domain/Containers/CommandHandlers/ReplaceContainerHandler.cs
+++ b/GlazeWM.Domain/Containers/CommandHandlers/ReplaceContainerHandler.cs
@@ -26,8 +26,8 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
foreach (var replacementContainer in replacementContainers)
{
replacementContainer.Parent = parentContainer;
- replacementContainer.SizePercentage =
- containerToReplace.SizePercentage * replacementContainer.SizePercentage;
+ (replacementContainer as IResizable).SizePercentage =
+ (containerToReplace as IResizable).SizePercentage * (replacementContainer as IResizable).SizePercentage;
}
// Replace the container at the given index.
diff --git a/GlazeWM.Domain/Containers/ContainerService.cs b/GlazeWM.Domain/Containers/ContainerService.cs
index 3057d9b9..6103e48b 100644
--- a/GlazeWM.Domain/Containers/ContainerService.cs
+++ b/GlazeWM.Domain/Containers/ContainerService.cs
@@ -50,7 +50,7 @@ namespace GlazeWM.Domain.Containers
var innerGap = _userConfigService.UserConfig.Gaps.InnerGap;
- return (int)(container.SizePercentage * (parent.Width - (innerGap * (parent.Children.Count - 1))));
+ return (int)((container as IResizable).SizePercentage * (parent.Width - (innerGap * (parent.Children.Count - 1))));
}
/// <summary>
@@ -66,7 +66,7 @@ namespace GlazeWM.Domain.Containers
var innerGap = _userConfigService.UserConfig.Gaps.InnerGap;
- return (int)(container.SizePercentage * (parent.Height - (innerGap * (parent.Children.Count - 1))));
+ return (int)((container as IResizable).SizePercentage * (parent.Height - (innerGap * (parent.Children.Count - 1))));
}
/// <summary>
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ResizeFocusedWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ResizeFocusedWindowHandler.cs
index 280fff5e..302ee78d 100644
--- a/GlazeWM.Domain/Windows/CommandHandlers/ResizeFocusedWindowHandler.cs
+++ b/GlazeWM.Domain/Windows/CommandHandlers/ResizeFocusedWindowHandler.cs
@@ -1,4 +1,6 @@
-using System.Linq;
+using System.Collections.Generic;
+using System.Linq;
+using System.Windows.Documents;
using GlazeWM.Domain.Common.Enums;
using GlazeWM.Domain.Containers;
using GlazeWM.Domain.Containers.Commands;
@@ -12,14 +14,12 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
class ResizeFocusedWindowHandler : ICommandHandler<ResizeFocusedWindowCommand>
{
private Bus _bus;
- private WindowService _windowService;
private UserConfigService _userConfigService;
private ContainerService _containerService;
- public ResizeFocusedWindowHandler(Bus bus, WindowService windowService, UserConfigService userConfigService, ContainerService containerService)
+ public ResizeFocusedWindowHandler(Bus bus, UserConfigService userConfigService, ContainerService containerService)
{
_bus = bus;
- _windowService = windowService;
_userConfigService = userConfigService;
_containerService = containerService;
}
@@ -44,20 +44,23 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
var containerToResize = shouldResizeParent ? focusedWindow.Parent : focusedWindow;
+ // Get siblings that can be resized.
+ var resizableSiblings = containerToResize.Siblings.Where(container => container is IResizable);
+
// Ignore cases where the container to resize is a workspace or is only child.
- if (!containerToResize.HasSiblings() || containerToResize is Workspace)
+ if (resizableSiblings.Count() == 0 || containerToResize is Workspace)
return CommandResponse.Ok;
switch (resizeDirection)
{
case ResizeDirection.GROW_WIDTH:
case ResizeDirection.GROW_HEIGHT:
- ShrinkSizeOfSiblings(containerToResize);
+ ShrinkSizeOfSiblings(containerToResize, resizableSiblings);
break;
case ResizeDirection.SHRINK_WIDTH:
case ResizeDirection.SHRINK_HEIGHT:
- GrowSizeOfSiblings(containerToResize);
+ GrowSizeOfSiblings(containerToResize, resizableSiblings);
break;
}
@@ -67,22 +70,22 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
return CommandResponse.Ok;
}
- private void GrowSizeOfSiblings(Container containerToShrink)
+ private void GrowSizeOfSiblings(Container containerToShrink, IEnumerable<Container> resizableSiblings)
{
var resizeProportion = _userConfigService.UserConfig.ResizeProportion;
- containerToShrink.SizePercentage -= resizeProportion;
+ (containerToShrink as IResizable).SizePercentage -= resizeProportion;
- foreach (var sibling in containerToShrink.Siblings)
- sibling.SizePercentage += resizeProportion / containerToShrink.Siblings.Count();
+ foreach (var sibling in resizableSiblings)
+ (sibling as IResizable).SizePercentage += resizeProportion / resizableSiblings.Count();
}
- private void ShrinkSizeOfSiblings(Container containerToGrow)
+ private void ShrinkSizeOfSiblings(Container containerToGrow, IEnumerable<Container> resizableSiblings)
{
var resizeProportion = _userConfigService.UserConfig.ResizeProportion;
- containerToGrow.SizePercentage += resizeProportion;
+ (containerToGrow as IResizable).SizePercentage += resizeProportion;
- foreach (var sibling in containerToGrow.Siblings)
- sibling.SizePercentage -= resizeProportion / containerToGrow.Siblings.Count();
+ foreach (var sibling in resizableSiblings)
+ (sibling as IResizable).SizePercentage -= resizeProportion / resizableSiblings.Count();
}
}
}