GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: add command for setting window size to a specific value (#268)
Max Lefebvreabb758d4a79d
diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.csindex 3fc68a5b..abff3d90 100644--- a/GlazeWM.Domain/DependencyInjection.cs+++ b/GlazeWM.Domain/DependencyInjection.cs@@ -70,6 +70,7 @@ namespace GlazeWM.Domainservices.AddSingleton<ICommandHandler<SetMaximizedCommand>, SetMaximizedHandler>();services.AddSingleton<ICommandHandler<SetMinimizedCommand>, SetMinimizedHandler>();services.AddSingleton<ICommandHandler<SetTilingCommand>, SetTilingHandler>();+ services.AddSingleton<ICommandHandler<SetWindowSizeCommand>, SetWindowSizeHandler>();services.AddSingleton<ICommandHandler<ShowAllWindowsCommand>, ShowAllWindowsHandler>();services.AddSingleton<ICommandHandler<ToggleFloatingCommand>, ToggleFloatingHandler>();services.AddSingleton<ICommandHandler<ToggleMaximizedCommand>, ToggleMaximizedHandler>();diff --git a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs b/GlazeWM.Domain/UserConfigs/CommandParsingService.csindex 70452f3c..dc7428ef 100644--- a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs+++ b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs@@ -224,6 +224,12 @@ namespace GlazeWM.Domain.UserConfigs"tiling" => subjectContainer is Window? new SetTilingCommand(subjectContainer as Window): new NoopCommand(),+ "width" => subjectContainer is Window+ ? new SetWindowSizeCommand(subjectContainer as Window, ResizeDimension.Width, commandParts[2])+ : new NoopCommand(),+ "height" => subjectContainer is Window+ ? new SetWindowSizeCommand(subjectContainer as Window, ResizeDimension.Height, commandParts[2])+ : new NoopCommand(),_ => throw new ArgumentException(null, nameof(commandParts)),};}diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.csindex 5eb3259a..c5066d03 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs@@ -1,7 +1,5 @@using System;-using System.Globalization;using System.Linq;-using System.Text.RegularExpressions;using GlazeWM.Domain.Common.Enums;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;@@ -10,7 +8,6 @@ using GlazeWM.Domain.Monitors;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces;using GlazeWM.Infrastructure.Bussing;-using GlazeWM.Infrastructure.Exceptions;using GlazeWM.Infrastructure.WindowsApi;namespace GlazeWM.Domain.Windows.CommandHandlers@@ -52,7 +49,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlersreturn CommandResponse.Ok;// Convert `resizeAmount` to a percentage to increase/decrease the window size by.- var resizePercentage = ConvertToResizePercentage(+ var resizePercentage = ResizeParsingService.ParseResizePercentage(containerToResize,dimensionToResize,resizeAmount@@ -88,54 +85,12 @@ namespace GlazeWM.Domain.Windows.CommandHandlersreturn isInverseResize ? parent : windowToResize;}- private static double ConvertToResizePercentage(- Container containerToResize,- ResizeDimension dimensionToResize,- string resizeAmount)- {- try- {- 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);-- return unit switch- {- "%" => floatAmount / 100,- "ppt" => floatAmount / 100,- "px" => floatAmount * GetPixelScaleFactor(containerToResize, dimensionToResize),- _ => throw new ArgumentException(null, nameof(resizeAmount)),- };- }- catch- {- 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<IResizable>().Aggregate(- 1.0,- (sum, container) =>- dimensionToResize == ResizeDimension.Width- ? sum + container.Width- : sum + container.Height- );-- return 1.0 / resizableLength;- }-private void ResizeFloatingWindow(Window windowToResize, ResizeDimension dimensionToResize, string resizeAmount){const int MIN_WIDTH = 250;const int MIN_HEIGHT = 140;- var resizePercentage = ConvertToResizePercentage(windowToResize, dimensionToResize, resizeAmount);+ var resizePercentage = ResizeParsingService.ParseResizePercentage(windowToResize, dimensionToResize, resizeAmount);var currentMonitor = MonitorService.GetMonitorFromChildContainer(windowToResize);var amount = (int)(currentMonitor.Width * resizePercentage);diff --git a/GlazeWM.Domain/Windows/CommandHandlers/SetWindowSizeHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/SetWindowSizeHandler.csnew file mode 100644index 00000000..9769c1fa--- /dev/null+++ b/GlazeWM.Domain/Windows/CommandHandlers/SetWindowSizeHandler.cs@@ -0,0 +1,58 @@+using GlazeWM.Domain.Common.Enums;+using GlazeWM.Domain.Windows.Commands;+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Windows.CommandHandlers+{+ internal sealed class SetWindowSizeHandler : ICommandHandler<SetWindowSizeCommand>+ {+ private readonly Bus _bus;++ public SetWindowSizeHandler(Bus bus)+ {+ _bus = bus;+ }++ public CommandResponse Handle(SetWindowSizeCommand command)+ {+ // calculate the delta required to achivee the desired window size+ var resizeAmount = CalculateResizeAmount(command);++ // invoke the resize command with the calculated delta+ _bus.Invoke(new ResizeWindowCommand(command.WindowToResize, command.DimensionToResize, resizeAmount));++ return CommandResponse.Ok;+ }++ private static string CalculateResizeAmount(SetWindowSizeCommand command)+ {+ // get the parent dimension (width or height)+ var parentSize = command.DimensionToResize switch+ {+ ResizeDimension.Width => command.WindowToResize.Parent.Width,+ ResizeDimension.Height => command.WindowToResize.Parent.Height,+ _ => 0,+ };++ // get the current window dimension (width or height)+ var actualSize = command.DimensionToResize switch+ {+ ResizeDimension.Width => command.WindowToResize.Width,+ ResizeDimension.Height => command.WindowToResize.Height,+ _ => 0,+ };++ // calculate the desired size (based on parent size)+ var desiredSize = parentSize * ResizeParsingService.ParseResizePercentage(+ command.WindowToResize,+ command.DimensionToResize,+ command.ResizeAmount);++ // calculate the delta required to achieve the desired size+ var resizeDelta = desiredSize - actualSize;++ // return delta (in px)+ return resizeDelta > 0 ? $"+{resizeDelta}px" : $"{resizeDelta}px";+ }+ }+}diff --git a/GlazeWM.Domain/Windows/Commands/SetWindowSizeCommand.cs b/GlazeWM.Domain/Windows/Commands/SetWindowSizeCommand.csnew file mode 100644index 00000000..1d0dadd6--- /dev/null+++ b/GlazeWM.Domain/Windows/Commands/SetWindowSizeCommand.cs@@ -0,0 +1,22 @@+using GlazeWM.Domain.Common.Enums;+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Windows.Commands+{+ public class SetWindowSizeCommand : Command+ {+ public Window WindowToResize { get; }+ public ResizeDimension DimensionToResize { get; }+ public string ResizeAmount { get; }++ public SetWindowSizeCommand(+ Window windowToResize,+ ResizeDimension dimensionToResize,+ string resizeAmount)+ {+ WindowToResize = windowToResize;+ DimensionToResize = dimensionToResize;+ ResizeAmount = resizeAmount;+ }+ }+}diff --git a/GlazeWM.Domain/Windows/ResizeParsingService.cs b/GlazeWM.Domain/Windows/ResizeParsingService.csnew file mode 100644index 00000000..e0c45bff--- /dev/null+++ b/GlazeWM.Domain/Windows/ResizeParsingService.cs@@ -0,0 +1,55 @@+using System;+using System.Globalization;+using System.Linq;+using System.Text.RegularExpressions;+using GlazeWM.Domain.Common.Enums;+using GlazeWM.Domain.Containers;+using GlazeWM.Infrastructure.Exceptions;++namespace GlazeWM.Domain.Windows+{+ public class ResizeParsingService+ {+ public static double ParseResizePercentage(+ Container containerToResize,+ ResizeDimension dimensionToResize,+ string resizeAmount)+ {+ try+ {+ 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);++ return unit switch+ {+ "%" => floatAmount / 100,+ "ppt" => floatAmount / 100,+ "px" => floatAmount * GetPixelScaleFactor(containerToResize, dimensionToResize),+ _ => throw new ArgumentException(null, nameof(resizeAmount)),+ };+ }+ catch+ {+ 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<IResizable>().Aggregate(+ 1.0,+ (sum, container) =>+ dimensionToResize == ResizeDimension.Width+ ? sum + container.Width+ : sum + container.Height+ );++ return 1.0 / resizableLength;+ }+ }+}diff --git a/README.md b/README.mdindex 0eefca95..46bfe05a 100644--- a/README.md+++ b/README.md@@ -331,6 +331,7 @@ window_rules:- resize \<height | width> \<amount in px | amount in %> (eg. `resize height 3%` or `resize width 20px`)- resize borders [\<shorthand property>](#shorthand-properties) (eg. `resize borders 0px -7px -7px -7px`)- set \<floating | tiling | minimized | maximized>+- set \<width | height> \<amount in px | amount in %> (eg. `set height 30%` or `set width 200px`)- toggle \<floating | maximized>- toggle focus mode- tiling direction toggle