GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: adjust windows for invisible borders (#62)
lars-berger043030fe9062
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/RedrawContainersHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/RedrawContainersHandler.csindex fdf37b59..310d1438 100644--- a/GlazeWM.Domain/Containers/CommandHandlers/RedrawContainersHandler.cs+++ b/GlazeWM.Domain/Containers/CommandHandlers/RedrawContainersHandler.cs@@ -1,8 +1,6 @@using System;using System.Linq;using GlazeWM.Domain.Containers.Commands;-using GlazeWM.Domain.Monitors;-using GlazeWM.Domain.UserConfigs;using GlazeWM.Domain.Windows;using GlazeWM.Infrastructure.Bussing;using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;@@ -48,14 +46,14 @@ namespace GlazeWM.Domain.Containers.CommandHandlerselseflags |= SWP.SWP_SHOWWINDOW;- SetWindowPos(window.Hwnd, IntPtr.Zero, window.X, window.Y, window.Width, window.Height, flags);+ SetWindowPosition(window, flags);- // When there's a mismatch between the DPI of the monitor and the window, `SetWindowPos` might- // size the window incorrectly. By calling `SetWindowPos` twice, inconsistencies after the first- // move are resolved.+ // When there's a mismatch between the DPI of the monitor and the window, `SetWindowPos`+ // might size the window incorrectly. By calling `SetWindowPos` twice, inconsistencies after+ // the first move are resolved.if (window.HasPendingDpiAdjustment){- SetWindowPos(window.Hwnd, IntPtr.Zero, window.X, window.Y, window.Width, window.Height, flags);+ SetWindowPosition(window, flags);window.HasPendingDpiAdjustment = false;}}@@ -64,5 +62,34 @@ namespace GlazeWM.Domain.Containers.CommandHandlersreturn CommandResponse.Ok;}++ private void SetWindowPosition(Window window, SWP flags)+ {+ if (window is TilingWindow)+ {+ SetWindowPos(+ window.Hwnd,+ IntPtr.Zero,+ window.X - window.BorderDelta.DeltaLeft,+ window.Y - window.BorderDelta.DeltaTop,+ window.Width + window.BorderDelta.DeltaLeft + window.BorderDelta.DeltaRight,+ window.Height + window.BorderDelta.DeltaTop + window.BorderDelta.DeltaRight,+ flags+ );+ return;+ }++ // Avoid adjusting the borders of floating windows. Otherwise the window will increase in size+ // from its original placement.+ SetWindowPos(+ window.Hwnd,+ IntPtr.Zero,+ window.X,+ window.Y,+ window.Width,+ window.Height,+ flags+ );+ }}}diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.csindex ac8bd9e1..5337b7a5 100644--- a/GlazeWM.Domain/DependencyInjection.cs+++ b/GlazeWM.Domain/DependencyInjection.cs@@ -59,6 +59,7 @@ namespace GlazeWM.Domainservices.AddSingleton<ICommandHandler<MoveFocusedWindowCommand>, MoveFocusedWindowHandler>();services.AddSingleton<ICommandHandler<RemoveWindowCommand>, RemoveWindowHandler>();services.AddSingleton<ICommandHandler<ResizeFocusedWindowCommand>, ResizeFocusedWindowHandler>();+ services.AddSingleton<ICommandHandler<ResizeFocusedWindowBordersCommand>, ResizeFocusedWindowBordersHandler>();services.AddSingleton<ICommandHandler<ShowAllWindowsCommand>, ShowAllWindowsHandler>();services.AddSingleton<ICommandHandler<ToggleFloatingCommand>, ToggleFloatingHandler>();services.AddSingleton<ICommandHandler<ToggleFocusedWindowFloatingCommand>, ToggleFocusedWindowFloatingHandler>();diff --git a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs b/GlazeWM.Domain/UserConfigs/CommandParsingService.csindex d084e861..bc9c7f33 100644--- a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs+++ b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs@@ -1,4 +1,5 @@using System;+using System.Linq;using System.Text.RegularExpressions;using GlazeWM.Domain.Common.Commands;using GlazeWM.Domain.Common.Enums;@@ -6,6 +7,8 @@ using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces.Commands;using GlazeWM.Infrastructure.Bussing;+using GlazeWM.Infrastructure.Utils;+using GlazeWM.Infrastructure.WindowsApi;namespace GlazeWM.Domain.UserConfigs{@@ -90,6 +93,9 @@ namespace GlazeWM.Domain.UserConfigs{"height" => new ResizeFocusedWindowCommand(ResizeDimension.HEIGHT, commandParts[2]),"width" => new ResizeFocusedWindowCommand(ResizeDimension.WIDTH, commandParts[2]),+ "borders" => new ResizeFocusedWindowBordersCommand(+ ShorthandToRectDelta(string.Join(" ", commandParts[2..]))+ ),_ => throw new ArgumentException(),};}@@ -130,5 +136,21 @@ namespace GlazeWM.Domain.UserConfigsreturn workspaceName;}++ private RectDelta ShorthandToRectDelta(string shorthand)+ {+ var shorthandParts = shorthand.Split(" ")+ .Select(shorthandPart => UnitsHelper.TrimUnits(shorthandPart))+ .ToList();++ return shorthandParts.Count() switch+ {+ 1 => new RectDelta(shorthandParts[0], shorthandParts[0], shorthandParts[0], shorthandParts[0]),+ 2 => new RectDelta(shorthandParts[1], shorthandParts[0], shorthandParts[1], shorthandParts[0]),+ 3 => new RectDelta(shorthandParts[1], shorthandParts[0], shorthandParts[1], shorthandParts[2]),+ 4 => new RectDelta(shorthandParts[3], shorthandParts[0], shorthandParts[1], shorthandParts[2]),+ _ => throw new ArgumentException(),+ };+ }}}diff --git a/GlazeWM.Domain/UserConfigs/UserConfigService.cs b/GlazeWM.Domain/UserConfigs/UserConfigService.csindex c2d4f7dc..f659ac24 100644--- a/GlazeWM.Domain/UserConfigs/UserConfigService.cs+++ b/GlazeWM.Domain/UserConfigs/UserConfigService.cs@@ -2,6 +2,7 @@using System.Collections.Generic;using System.IO;using System.Linq;+using GlazeWM.Domain.Windows;namespace GlazeWM.Domain.UserConfigs{@@ -82,6 +83,26 @@ namespace GlazeWM.Domain.UserConfigswindowRules.Add(windowRule);}+ var chromiumBrowserProcessNames = new List<string> {+ "chrome",+ "msedge",+ "opera",+ "vivaldi",+ "brave",+ };++ // Electron apps do not have invisible borders and are thus over-corrected by the default+ // border fix. To match these apps, get windows with the class name 'Chrome_WidgetWin_1' that+ // are not Chromium-based browsers (since the browser windows do have invisble borders).+ var resizeElectronBorderWindowRule = new WindowRuleConfig()+ {+ MatchProcessName = $"/^(?!({string.Join("|", chromiumBrowserProcessNames)})$)/",+ MatchClassName = "Chrome_WidgetWin_1",+ Command = "resize borders 0px -7px -7px -7px",+ };++ windowRules.Add(resizeElectronBorderWindowRule);+return windowRules;}@@ -91,5 +112,22 @@ namespace GlazeWM.Domain.UserConfigs(workspaceConfig) => workspaceConfig.Name == workspaceName);}++ public IEnumerable<WindowRuleConfig> GetMatchingWindowRules(Window window)+ {+ return UserConfig.WindowRules.Where(rule =>+ {+ if (rule.ProcessNameRegex?.IsMatch(window.ProcessName) == false)+ return false;++ if (rule.ClassNameRegex?.IsMatch(window.ClassName) == false)+ return false;++ if (rule.TitleRegex?.IsMatch(window.Title) == false)+ return false;++ return true;+ });+ }}}diff --git a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.csindex f6022727..baa68dfc 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs@@ -1,5 +1,4 @@-using System.Collections.Generic;-using System.Linq;+using System.Linq;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Monitors;@@ -7,7 +6,6 @@ using GlazeWM.Domain.UserConfigs;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces;using GlazeWM.Infrastructure.Bussing;-using GlazeWM.Infrastructure.WindowsApi;using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;namespace GlazeWM.Domain.Windows.CommandHandlers@@ -65,7 +63,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlers// Create the window instance.var window = new TilingWindow(command.WindowHandle, floatingPlacement);- var matchingWindowRules = GetMatchingWindowRules(window);+ var matchingWindowRules = _userConfigService.GetMatchingWindowRules(window);var commandStrings = matchingWindowRules.SelectMany(rule => rule.CommandList)@@ -120,24 +118,5 @@ namespace GlazeWM.Domain.Windows.CommandHandlersreturn (focusedContainer.Parent as SplitContainer, focusedContainer.Index + 1);}-- private List<WindowRuleConfig> GetMatchingWindowRules(Window window)- {- return _userConfigService.UserConfig.WindowRules- .Where(rule =>- {- if (rule.ProcessNameRegex != null && !rule.ProcessNameRegex.IsMatch(window.ProcessName))- return false;-- if (rule.ClassNameRegex != null && !rule.ClassNameRegex.IsMatch(window.ClassName))- return false;-- if (rule.TitleRegex != null && !rule.TitleRegex.IsMatch(window.Title))- return false;-- return true;- })- .ToList();- }}}diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ResizeFocusedWindowBordersHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ResizeFocusedWindowBordersHandler.csnew file mode 100644index 00000000..e2da2b69--- /dev/null+++ b/GlazeWM.Domain/Windows/CommandHandlers/ResizeFocusedWindowBordersHandler.cs@@ -0,0 +1,47 @@+using GlazeWM.Domain.Containers;+using GlazeWM.Domain.Containers.Commands;+using GlazeWM.Domain.Windows.Commands;+using GlazeWM.Infrastructure.Bussing;+using GlazeWM.Infrastructure.WindowsApi;++namespace GlazeWM.Domain.Windows.CommandHandlers+{+ class ResizeFocusedWindowBordersHandler : ICommandHandler<ResizeFocusedWindowBordersCommand>+ {+ private readonly Bus _bus;+ private readonly ContainerService _containerService;++ public ResizeFocusedWindowBordersHandler(Bus bus, ContainerService containerService)+ {+ _bus = bus;+ _containerService = containerService;+ }++ public CommandResponse Handle(ResizeFocusedWindowBordersCommand command)+ {+ var borderDelta = command.BorderDelta;+ var focusedWindow = _containerService.FocusedContainer as Window;++ // Ignore cases where focused container is not a window.+ if (focusedWindow == null)+ return CommandResponse.Ok;++ // Adjust the existing border delta of the window.+ focusedWindow.BorderDelta = new RectDelta(+ focusedWindow.BorderDelta.DeltaLeft + borderDelta.DeltaLeft,+ focusedWindow.BorderDelta.DeltaTop + borderDelta.DeltaTop,+ focusedWindow.BorderDelta.DeltaRight + borderDelta.DeltaRight,+ focusedWindow.BorderDelta.DeltaBottom + borderDelta.DeltaBottom+ );++ if (!(focusedWindow is TilingWindow))+ return CommandResponse.Ok;++ // Only redraw the window if it's tiling.+ _containerService.ContainersToRedraw.Add(focusedWindow);+ _bus.Invoke(new RedrawContainersCommand());++ return CommandResponse.Ok;+ }+ }+}diff --git a/GlazeWM.Domain/Windows/Commands/ResizeFocusedWindowBordersCommand.cs b/GlazeWM.Domain/Windows/Commands/ResizeFocusedWindowBordersCommand.csnew file mode 100644index 00000000..2be2927a--- /dev/null+++ b/GlazeWM.Domain/Windows/Commands/ResizeFocusedWindowBordersCommand.cs@@ -0,0 +1,15 @@+using GlazeWM.Infrastructure.Bussing;+using GlazeWM.Infrastructure.WindowsApi;++namespace GlazeWM.Domain.Windows.Commands+{+ public class ResizeFocusedWindowBordersCommand : Command+ {+ public RectDelta BorderDelta { get; }++ public ResizeFocusedWindowBordersCommand(RectDelta borderDelta)+ {+ BorderDelta = borderDelta;+ }+ }+}diff --git a/GlazeWM.Domain/Windows/Window.cs b/GlazeWM.Domain/Windows/Window.csindex dd4119c6..5a69a58e 100644--- a/GlazeWM.Domain/Windows/Window.cs+++ b/GlazeWM.Domain/Windows/Window.cs@@ -23,6 +23,13 @@ namespace GlazeWM.Domain.Windows/// </summary>public bool HasPendingDpiAdjustment { get; set; } = false;+ /// <summary>+ /// The difference in window dimensions to adjust for invisible borders. This is typically 7px+ /// on the left, right, and bottom edges. This needs to be adjusted for to draw a window with+ /// exact dimensions.+ /// </summary>+ public RectDelta BorderDelta { get; set; } = new RectDelta(7, 0, 7, 7);+private WindowService _windowService = ServiceLocator.Provider.GetRequiredService<WindowService>();private WorkspaceService _workspaceService = ServiceLocator.Provider.GetRequiredService<WorkspaceService>();diff --git a/GlazeWM.Infrastructure/Utils/UnitsHelper.cs b/GlazeWM.Infrastructure/Utils/UnitsHelper.csnew file mode 100644index 00000000..064de90f--- /dev/null+++ b/GlazeWM.Infrastructure/Utils/UnitsHelper.cs@@ -0,0 +1,16 @@+using System;+using System.Globalization;+using System.Text.RegularExpressions;++namespace GlazeWM.Infrastructure.Utils+{+ public static class UnitsHelper+ {+ public static int TrimUnits(string amountWithUnits)+ {+ var unitsRegex = new Regex(@"(%|ppt|px)");+ var amount = unitsRegex.Replace(amountWithUnits, "").Trim();+ return Convert.ToInt32(amount, CultureInfo.InvariantCulture);+ }+ }+}diff --git a/GlazeWM.Infrastructure/WindowsApi/RectDelta.cs b/GlazeWM.Infrastructure/WindowsApi/RectDelta.csnew file mode 100644index 00000000..c8ce2942--- /dev/null+++ b/GlazeWM.Infrastructure/WindowsApi/RectDelta.cs@@ -0,0 +1,33 @@+namespace GlazeWM.Infrastructure.WindowsApi+{+ public class RectDelta+ {+ /// <summary>+ /// The difference in x-coordinates of the upper-left corner of the rectangle.+ /// </summary>+ public int DeltaLeft { get; }++ /// <summary>+ /// The difference in y-coordinates of the upper-left corner of the rectangle.+ /// </summary>+ public int DeltaTop { get; }++ /// <summary>+ /// The difference in x-coordinates of the lower-right corner of the rectangle.+ /// </summary>+ public int DeltaRight { get; }++ /// <summary>+ /// The difference in y-coordinates of the lower-right corner of the rectangle.+ /// </summary>+ public int DeltaBottom { get; }++ public RectDelta(int deltaLeft, int deltaTop, int deltaRight, int deltaBottom)+ {+ DeltaLeft = deltaLeft;+ DeltaRight = deltaRight;+ DeltaTop = deltaTop;+ DeltaBottom = deltaBottom;+ }+ }+}diff --git a/GlazeWM.Infrastructure/WindowsApi/WindowRect.cs b/GlazeWM.Infrastructure/WindowsApi/WindowRect.csindex 24479697..fee3b155 100644--- a/GlazeWM.Infrastructure/WindowsApi/WindowRect.cs+++ b/GlazeWM.Infrastructure/WindowsApi/WindowRect.cs@@ -30,16 +30,37 @@public int Height => Bottom - Top;- public WindowRect TranslateToCoordinates(int x, int y)+ /// <summary>+ /// Creates a new `WindowRect` from coordinates of its upper-left and lower-right corners.+ /// </summary>+ public static WindowRect FromLTRB(int left, int top, int right, int bottom){- var translatedRect = new WindowRect();+ return new WindowRect()+ {+ Left = left,+ Right = right,+ Top = top,+ Bottom = bottom,+ };+ }- translatedRect.Left = x;- translatedRect.Right = x + Width;- translatedRect.Top = y;- translatedRect.Bottom = y + Height;+ /// <summary>+ /// Creates a new `WindowRect` from its X/Y coordinates and size.+ /// </summary>+ public static WindowRect FromXYCoordinates(int x, int y, int width, int height)+ {+ return new WindowRect()+ {+ Left = x,+ Right = x + width,+ Top = y,+ Bottom = y + height,+ };+ }- return translatedRect;+ public WindowRect TranslateToCoordinates(int x, int y)+ {+ return FromXYCoordinates(x, y, Width, Height);}public WindowRect TranslateToCenter(WindowRect outerRect)