GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: resize windows with sizing border (#95)
lars-bergerab4350dc1584
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/AttachAndResizeContainerHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/AttachAndResizeContainerHandler.csindex e290288b..4b66b296 100644--- a/GlazeWM.Domain/Containers/CommandHandlers/AttachAndResizeContainerHandler.cs+++ b/GlazeWM.Domain/Containers/CommandHandlers/AttachAndResizeContainerHandler.cs@@ -25,7 +25,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlers_bus.Invoke(new AttachContainerCommand(childToAdd, targetParent, targetIndex));- var resizableSiblings = childToAdd.Siblings.Where(container => container is IResizable);+ var resizableSiblings = childToAdd.SiblingsOfType(typeof(IResizable));var defaultPercent = 1.0 / (resizableSiblings.Count() + 1);(childToAdd as IResizable).SizePercentage = defaultPercent;diff --git a/GlazeWM.Domain/Containers/Container.cs b/GlazeWM.Domain/Containers/Container.csindex a7ec23ee..d4106ac4 100644--- a/GlazeWM.Domain/Containers/Container.cs+++ b/GlazeWM.Domain/Containers/Container.cs@@ -136,6 +136,11 @@ namespace GlazeWM.Domain.Containersreturn Children.Where(container => type.IsAssignableFrom(container.GetType()));}+ public IEnumerable<Container> SiblingsOfType(Type type)+ {+ return Siblings.Where(container => type.IsAssignableFrom(container.GetType()));+ }+public IEnumerable<Container> SelfAndSiblingsOfType(Type type){return SelfAndSiblings.Where(container => type.IsAssignableFrom(container.GetType()));diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.csindex c592bcec..90c03822 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs@@ -40,18 +40,17 @@ namespace GlazeWM.Domain.Windows.CommandHandlers(layout == Layout.HORIZONTAL && dimensionToResize == ResizeDimension.HEIGHT)|| (layout == Layout.VERTICAL && dimensionToResize == ResizeDimension.WIDTH);+ // Get container and its siblings to resize.var containerToResize = shouldResizeParent ? windowToResize.Parent : windowToResize;+ var resizableSiblings = containerToResize.SiblingsOfType(typeof(IResizable));- // 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.+ // Ignore cases where the container to resize is a workspace or is 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 resizePercentage = ConvertToResizePercentage(resizeAmount);- var resizeProportion = resizePercentage / 100;+ var pixelScaleFactor = GetPixelScaleFactor(containerToResize, dimensionToResize);+ var resizeProportion = ConvertToResizePercentage(resizeAmount, pixelScaleFactor);(containerToResize as IResizable).SizePercentage += resizeProportion;@@ -64,19 +63,36 @@ namespace GlazeWM.Domain.Windows.CommandHandlersreturn CommandResponse.Ok;}- private static double ConvertToResizePercentage(string resizeAmount)+ private static double GetPixelScaleFactor(+ Container containerToResize,+ ResizeDimension dimensionToResize+ )+ {+ // Get area that can be resized (ie. exclude inner gaps).+ var resizableArea = containerToResize.SelfAndSiblings.Aggregate(1.0, (acc, con) =>+ {+ return dimensionToResize == ResizeDimension.WIDTH+ ? acc + con.Width+ : acc + con.Height;+ });++ return 1.0 / resizableArea;+ }++ private static double ConvertToResizePercentage(string resizeAmount, double pixelScaleFactor){try{- var matchedResizeAmount = new Regex("(.*)(%|ppt)").Match(resizeAmount);+ var matchedResizeAmount = new Regex("(.*)(%|ppt|px)").Match(resizeAmount);var amount = matchedResizeAmount.Groups[1].Value;var unit = matchedResizeAmount.Groups[2].Value;+ var floatAmount = Convert.ToDouble(amount, CultureInfo.InvariantCulture);- // TODO: Handle conversion from px to `SizePercentage`.return unit switch{- "%" => Convert.ToDouble(amount, CultureInfo.InvariantCulture),- "ppt" => Convert.ToDouble(amount, CultureInfo.InvariantCulture),+ "%" => floatAmount / 100,+ "ppt" => floatAmount / 100,+ "px" => floatAmount * pixelScaleFactor,_ => throw new ArgumentException(null, nameof(resizeAmount)),};}diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.csindex e0721fa2..e98829aa 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowMovedOrResizedHandler.cs@@ -1,10 +1,14 @@using System.Linq;+using GlazeWM.Domain.Common.Enums;using GlazeWM.Domain.Common.Utils;+using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Containers.Events;using GlazeWM.Domain.Monitors;+using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces;using GlazeWM.Infrastructure.Bussing;+using GlazeWM.Infrastructure.WindowsApi;using GlazeWM.Infrastructure.WindowsApi.Events;using Microsoft.Extensions.Logging;@@ -15,19 +19,22 @@ namespace GlazeWM.Domain.Windows.EventHandlersprivate readonly Bus _bus;private readonly WindowService _windowService;private readonly MonitorService _monitorService;+ private readonly ContainerService _containerService;private readonly ILogger<WindowMovedOrResizedHandler> _logger;public WindowMovedOrResizedHandler(Bus bus,WindowService windowService,MonitorService monitorService,- ILogger<WindowMovedOrResizedHandler> logger+ ILogger<WindowMovedOrResizedHandler> logger,+ ContainerService containerService){_bus = bus;_windowService = windowService;_monitorService = monitorService;_logger = logger;+ _containerService = containerService;}public void Handle(WindowMovedOrResizedEvent @event)@@ -35,21 +42,59 @@ namespace GlazeWM.Domain.Windows.EventHandlersvar window = _windowService.GetWindows().FirstOrDefault(window => window.Hwnd == @event.WindowHandle);- if (window is null or not FloatingWindow)+ if (window is null)return;_logger.LogWindowEvent("Window moved/resized", window);- // Update state with new location of the floating window.- UpdateWindowPlacement(window);+ if (window is TilingWindow)+ {+ UpdateTilingWindow(window as TilingWindow);+ return;+ }- // Change floating window's parent workspace if out of its bounds.- UpdateParentWorkspace(window);+ if (window is FloatingWindow)+ UpdateFloatingWindow(window as FloatingWindow);+ }++ private void UpdateTilingWindow(TilingWindow window)+ {+ // Snap window to its original position even if it's not being resized.+ var hasNoResizableSiblings = window.Parent is Workspace+ && !window.SiblingsOfType(typeof(IResizable)).Any();++ if (hasNoResizableSiblings)+ {+ _containerService.ContainersToRedraw.Add(window);+ _bus.Invoke(new RedrawContainersCommand());+ return;+ }++ // Remove invisible borders from current placement to be able to compare window width/height.+ var currentPlacement = WindowService.GetPlacementOfHandle(window.Hwnd).NormalPosition;+ var adjustedPlacement = new WindowRect+ {+ Left = currentPlacement.Left + window.BorderDelta.DeltaLeft,+ Right = currentPlacement.Right - window.BorderDelta.DeltaRight,+ Top = currentPlacement.Top + window.BorderDelta.DeltaTop,+ Bottom = currentPlacement.Bottom - window.BorderDelta.DeltaBottom,+ };++ var deltaWidth = adjustedPlacement.Width - window.Width;+ var deltaHeight = adjustedPlacement.Height - window.Height;++ // TODO: Avoid unnecessary resize call if either delta is 0.+ _bus.Invoke(new ResizeWindowCommand(window, ResizeDimension.WIDTH, $"{deltaWidth}px"));+ _bus.Invoke(new ResizeWindowCommand(window, ResizeDimension.HEIGHT, $"{deltaHeight}px"));}- private static void UpdateWindowPlacement(Window window)+ private void UpdateFloatingWindow(FloatingWindow window){+ // Update state with new location of the floating window.window.FloatingPlacement = WindowService.GetPlacementOfHandle(window.Hwnd).NormalPosition;++ // Change floating window's parent workspace if out of its bounds.+ UpdateParentWorkspace(window);}private void UpdateParentWorkspace(Window window)diff --git a/README.md b/README.mdindex 7d887375..e78b043d 100644--- a/README.md+++ b/README.md@@ -7,8 +7,8 @@ Why use a tiling window manager? A tiling WM lets you easily organize windows an- Simple YAML configuration- Multi-monitor support- Customizable bar window-- Handles monitor connections & disconnections- Customizable rules for specific windows+- Easy one-click installation@@ -30,7 +30,7 @@ To build for other runtimes than Windows x64, see [here](https://docs.microsoft.- Improve handling of fullscreen and maximized windows.- More bar components.-- Change the size of sibling windows when a tiling window is resized with the sizing border.+- Reload user config without restarting WM.# Configuration