Skip to content

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

fix: prevent containers from exceeding min/max size percentage (#98)

lars-bergere6c5a73fe9c4
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs
index 52530753..3ddc5fa1 100644
--- a/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs
+++ b/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
@@ -16,6 +17,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
{
private readonly Bus _bus;
private readonly ContainerService _containerService;
+ private const double MIN_SIZE_PERCENTAGE = 0.01;
public ResizeWindowHandler(Bus bus, ContainerService containerService)
{
@@ -37,26 +39,71 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
var containerToResize = GetContainerToResize(windowToResize, dimensionToResize);
var resizableSiblings = containerToResize.SiblingsOfType(typeof(IResizable));
- // Ignore cases where the container to resize is a workspace or is the only child.
+ // Ignore cases where the container to resize is a workspace or the only child.
if (!resizableSiblings.Any() || containerToResize is Workspace)
return CommandResponse.Ok;
// Convert `resizeAmount` to a percentage to increase/decrease the window size by.
- var pixelScaleFactor = GetPixelScaleFactor(containerToResize, dimensionToResize);
- var resizePercentage = ConvertToResizePercentage(resizeAmount, pixelScaleFactor);
+ var resizePercentage = ConvertToResizePercentage(
+ containerToResize,
+ dimensionToResize,
+ resizeAmount
+ );
- // TODO: Avoid unnecessary resize call if either delta is 0.
- (containerToResize as IResizable).SizePercentage += resizePercentage;
+ // Get available size percentage amongst siblings.
+ var availableSizePercentage = GetAvailableSizePercentage(
+ resizableSiblings
+ );
- foreach (var sibling in resizableSiblings)
- (sibling as IResizable).SizePercentage -= resizePercentage / resizableSiblings.Count();
+ // Prevent window from being smaller than the minimum and larger than space available from
+ // sibling containers.
+ var minResizeDelta = MIN_SIZE_PERCENTAGE - (containerToResize as IResizable).SizePercentage;
+ var clampedResizePercentage = Math.Clamp(
+ resizePercentage,
+ minResizeDelta,
+ availableSizePercentage
+ );
+ // Resize the container and distribute the size percentage amongst its siblings.
+ (containerToResize as IResizable).SizePercentage += clampedResizePercentage;
+ DistributeSizePercentage(resizableSiblings, clampedResizePercentage, availableSizePercentage);
+
+ // TODO: Return early if `clampedResizePercentage` is 0 to avoid unnecessary redraws.
_containerService.ContainersToRedraw.Add(containerToResize.Parent);
_bus.Invoke(new RedrawContainersCommand());
return CommandResponse.Ok;
}
+ private static double GetAvailableSizePercentage(IEnumerable<Container> containers)
+ {
+ return containers.Aggregate(
+ 0.0,
+ (sum, container) => sum + (container as IResizable).SizePercentage - MIN_SIZE_PERCENTAGE
+ );
+ }
+
+ private static void DistributeSizePercentage(
+ IEnumerable<Container> containers,
+ double sizePercentage,
+ double availableSizePercentage
+ )
+ {
+ foreach (var container in containers)
+ {
+ var conAvailableSizePercentage =
+ (container as IResizable).SizePercentage - MIN_SIZE_PERCENTAGE;
+
+ // Get percentage of resize that affects this container. `availableSizePercentage`
+ // can be 0 here when the main container to resize is shrunk from max size percentage.
+ var resizeFactor = availableSizePercentage == 0.0
+ ? 1.0 / containers.Count()
+ : conAvailableSizePercentage / availableSizePercentage;
+
+ (container as IResizable).SizePercentage -= resizeFactor * sizePercentage;
+ }
+ }
+
private static Container GetContainerToResize(
Window windowToResize,
ResizeDimension dimensionToResize
@@ -71,32 +118,19 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
(layout == Layout.HORIZONTAL && dimensionToResize == ResizeDimension.HEIGHT) ||
(layout == Layout.VERTICAL && dimensionToResize == ResizeDimension.WIDTH);
- if (!isInverseResize && !windowToResize.Siblings.Any() && grandparent is IResizable)
+ var hasResizableSiblings = windowToResize.SiblingsOfType(typeof(IResizable)).Any();
+
+ if (!isInverseResize && !hasResizableSiblings && grandparent is IResizable)
return grandparent;
return isInverseResize ? parent : windowToResize;
}
- private static double GetPixelScaleFactor(
+ private static double ConvertToResizePercentage(
Container containerToResize,
- ResizeDimension dimensionToResize
- )
- {
- // Get available width/height that can be resized (ie. exclude inner gaps).
- var resizableArea = containerToResize.SelfAndSiblings.Aggregate(
- 1.0,
- (width, container) =>
- {
- return dimensionToResize == ResizeDimension.WIDTH
- ? width + container.Width
- : width + container.Height;
- }
- );
-
- return 1.0 / resizableArea;
- }
-
- private static double ConvertToResizePercentage(string resizeAmount, double pixelScaleFactor)
+ ResizeDimension dimensionToResize,
+ string resizeAmount
+ )
{
try
{
@@ -109,7 +143,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
{
"%" => floatAmount / 100,
"ppt" => floatAmount / 100,
- "px" => floatAmount * pixelScaleFactor,
+ "px" => floatAmount * GetPixelScaleFactor(containerToResize, dimensionToResize),
_ => throw new ArgumentException(null, nameof(resizeAmount)),
};
}
@@ -118,5 +152,22 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
throw new FatalUserException($"Invalid resize amount {resizeAmount}.");
}
}
+
+ private static double GetPixelScaleFactor(
+ Container containerToResize,
+ ResizeDimension dimensionToResize
+ )
+ {
+ // Get available width/height that can be resized (ie. exclude inner gaps).
+ var resizableLength = containerToResize.SelfAndSiblingsOfType(typeof(IResizable)).Aggregate(
+ 1.0,
+ (sum, container) =>
+ dimensionToResize == ResizeDimension.WIDTH
+ ? sum + container.Width
+ : sum + container.Height
+ );
+
+ return 1.0 / resizableLength;
+ }
}
}