Skip to content

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

feat: move and resize floating windows (#238)

Pamestabcc1337ee43d
diff --git a/GlazeWM.Bootstrapper/sample-config.yaml b/GlazeWM.Bootstrapper/sample-config.yaml
index f87d5dcb..9372e113 100644
--- a/GlazeWM.Bootstrapper/sample-config.yaml
+++ b/GlazeWM.Bootstrapper/sample-config.yaml
@@ -5,6 +5,7 @@ gaps:
general:
# Whether to show floating windows as always on top.
show_floating_on_top: false
+ floating_window_move_amount: "5%"
bar:
height: "30px"
diff --git a/GlazeWM.Domain/UserConfigs/GeneralConfig.cs b/GlazeWM.Domain/UserConfigs/GeneralConfig.cs
index ca61e247..6bd1c6c0 100644
--- a/GlazeWM.Domain/UserConfigs/GeneralConfig.cs
+++ b/GlazeWM.Domain/UserConfigs/GeneralConfig.cs
@@ -11,5 +11,9 @@ namespace GlazeWM.Domain.UserConfigs
/// TODO: Not officially released because implementation is buggy. Use at own risk.
/// </summary>
public bool CursorFollowsFocus { get; set; }
+ /// <summary>
+ /// Amount by which to move floating windows
+ /// </summary>
+ public string FloatingWindowMoveAmount { get; set; } = "5%";
}
}
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/MoveWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/MoveWindowHandler.cs
index 4d4252e8..b830df03 100644
--- a/GlazeWM.Domain/Windows/CommandHandlers/MoveWindowHandler.cs
+++ b/GlazeWM.Domain/Windows/CommandHandlers/MoveWindowHandler.cs
@@ -1,12 +1,17 @@
+using System;
using System.Linq;
using GlazeWM.Domain.Common.Enums;
using GlazeWM.Domain.Containers;
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.Containers.Events;
using GlazeWM.Domain.Monitors;
+using GlazeWM.Domain.UserConfigs;
using GlazeWM.Domain.Windows.Commands;
using GlazeWM.Domain.Workspaces;
using GlazeWM.Infrastructure.Bussing;
+using GlazeWM.Infrastructure.Common.Events;
+using GlazeWM.Infrastructure.Utils;
+using GlazeWM.Infrastructure.WindowsApi;
namespace GlazeWM.Domain.Windows.CommandHandlers
{
@@ -15,59 +20,38 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
private readonly Bus _bus;
private readonly ContainerService _containerService;
private readonly MonitorService _monitorService;
+ private readonly UserConfigService _userConfigService;
public MoveWindowHandler(
Bus bus,
ContainerService containerService,
- MonitorService monitorService)
+ MonitorService monitorService,
+ UserConfigService userConfigService)
{
_bus = bus;
_containerService = containerService;
_monitorService = monitorService;
+ _userConfigService = userConfigService;
}
public CommandResponse Handle(MoveWindowCommand command)
{
- var windowToMove = command.WindowToMove as TilingWindow;
+ var windowToMove = command.WindowToMove;
var direction = command.Direction;
- // Ignore cases where window is not tiling.
- if (windowToMove is null)
- return CommandResponse.Ok;
-
- var layoutForDirection = direction.GetCorrespondingLayout();
- var parentMatchesLayout =
- (windowToMove.Parent as SplitContainer).Layout == direction.GetCorrespondingLayout();
-
- if (parentMatchesLayout && HasSiblingInDirection(windowToMove, direction))
- {
- SwapSiblingContainers(windowToMove, direction);
- return CommandResponse.Ok;
- }
-
- // Attempt to the move window to workspace in given direction.
- if (parentMatchesLayout && windowToMove.Parent is Workspace)
+ if (windowToMove is FloatingWindow)
{
- MoveToWorkspaceInDirection(windowToMove, direction);
+ MoveFloatingWindow(windowToMove as FloatingWindow, direction);
return CommandResponse.Ok;
}
- // The window cannot be moved within the parent container, so traverse upwards to find a
- // suitable ancestor to move to.
- var ancestorWithLayout = windowToMove.Parent.Ancestors.FirstOrDefault(
- container => (container as SplitContainer)?.Layout == layoutForDirection
- ) as SplitContainer;
-
- // Change the layout of the workspace to layout for direction.
- if (ancestorWithLayout == null)
+ if (windowToMove is TilingWindow)
{
- ChangeWorkspaceLayout(windowToMove, direction);
+ MoveTilingWindow(windowToMove as TilingWindow, direction);
return CommandResponse.Ok;
}
- InsertIntoAncestor(windowToMove, direction, ancestorWithLayout);
-
- return CommandResponse.Ok;
+ return CommandResponse.Fail;
}
/// <summary>
@@ -216,5 +200,110 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
_bus.Invoke(new RedrawContainersCommand());
}
+
+ private void MoveTilingWindow(TilingWindow windowToMove, Direction direction)
+ {
+ var layoutForDirection = direction.GetCorrespondingLayout();
+ var parentMatchesLayout =
+ (windowToMove.Parent as SplitContainer).Layout == direction.GetCorrespondingLayout();
+
+ if (parentMatchesLayout && HasSiblingInDirection(windowToMove, direction))
+ {
+ SwapSiblingContainers(windowToMove, direction);
+ return;
+ }
+
+ // Attempt to the move window to workspace in given direction.
+ if (parentMatchesLayout && windowToMove.Parent is Workspace)
+ {
+ MoveToWorkspaceInDirection(windowToMove, direction);
+ return;
+ }
+
+ // The window cannot be moved within the parent container, so traverse upwards to find a
+ // suitable ancestor to move to.
+ var ancestorWithLayout = windowToMove.Parent.Ancestors.FirstOrDefault(
+ container => (container as SplitContainer)?.Layout == layoutForDirection
+ ) as SplitContainer;
+
+ // Change the layout of the workspace to layout for direction.
+ if (ancestorWithLayout == null)
+ {
+ ChangeWorkspaceLayout(windowToMove, direction);
+ return;
+ }
+
+ InsertIntoAncestor(windowToMove, direction, ancestorWithLayout);
+ }
+
+ private void MoveFloatingWindow(Window windowToMove, Direction direction)
+ {
+ var valueFromConfig = _userConfigService.GeneralConfig.FloatingWindowMoveAmount;
+
+ var amount = UnitsHelper.TrimUnits(valueFromConfig);
+ var units = UnitsHelper.GetUnits(valueFromConfig);
+ var currentMonitor = MonitorService.GetMonitorFromChildContainer(windowToMove);
+
+ amount = units switch
+ {
+ "%" => (int)(amount * currentMonitor.Width / 100),
+ "ppt" => (int)(amount * currentMonitor.Width / 100),
+ "px" => amount,
+ // in case user only provided a number in the config;
+ // TODO: somehow validate floating_window_move_amount in config on startup
+ _ => amount
+ // _ => throw new ArgumentException(null, nameof(amount)),
+ };
+
+ var x = windowToMove.FloatingPlacement.X;
+ var y = windowToMove.FloatingPlacement.Y;
+
+ _ = direction switch
+ {
+ Direction.Left => x -= amount,
+ Direction.Right => x += amount,
+ Direction.Up => y -= amount,
+ Direction.Down => y += amount,
+ _ => throw new ArgumentException(null, nameof(direction))
+ };
+
+ // Make sure grabbable space on top is always visible
+ var monitorAbove = _monitorService.GetMonitorInDirection(Direction.Up, currentMonitor);
+ if (y < currentMonitor.Y && monitorAbove == null)
+ {
+ y = currentMonitor.Y;
+ }
+
+ var newPlacement = Rect.FromXYCoordinates(x, y, windowToMove.FloatingPlacement.Width, windowToMove.FloatingPlacement.Height);
+ var center = newPlacement.GetCenterPoint();
+
+ // If new placement wants to cross monitors
+ // && direction == ... is for edge case when user places window center outside a monitor with a mouse
+ if ((center.X >= currentMonitor.Width + currentMonitor.X && direction == Direction.Right) ||
+ (center.X < currentMonitor.X && direction == Direction.Left) ||
+ (center.Y < currentMonitor.Y && direction == Direction.Up) ||
+ (center.Y >= currentMonitor.Height + currentMonitor.Y && direction == Direction.Down))
+ {
+ var monitorInDirection = _monitorService.GetMonitorInDirection(direction, currentMonitor);
+ var workspaceInDirection = monitorInDirection?.DisplayedWorkspace;
+
+ if (workspaceInDirection == null)
+ {
+ return;
+ }
+
+ // Change the window's parent workspace.
+ _bus.Invoke(new MoveContainerWithinTreeCommand(windowToMove, workspaceInDirection, false));
+ _bus.Emit(new FocusChangedEvent(windowToMove));
+
+ // Redrawing twice to fix weird WindowsOS dpi behaviour
+ windowToMove.HasPendingDpiAdjustment = true;
+ }
+
+ windowToMove.FloatingPlacement = newPlacement;
+
+ _containerService.ContainersToRedraw.Add(windowToMove);
+ _bus.Invoke(new RedrawContainersCommand());
+ }
}
}
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs
index 5d1061a2..5248bfde 100644
--- a/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs
+++ b/GlazeWM.Domain/Windows/CommandHandlers/ResizeWindowHandler.cs
@@ -5,10 +5,14 @@ using System.Text.RegularExpressions;
using GlazeWM.Domain.Common.Enums;
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.Common.Events;
using GlazeWM.Infrastructure.Exceptions;
+using GlazeWM.Infrastructure.WindowsApi;
namespace GlazeWM.Domain.Windows.CommandHandlers
{
@@ -16,11 +20,13 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
{
private readonly Bus _bus;
private readonly ContainerService _containerService;
+ private readonly MonitorService _monitorService;
- public ResizeWindowHandler(Bus bus, ContainerService containerService)
+ public ResizeWindowHandler(Bus bus, ContainerService containerService, MonitorService monitorService)
{
_bus = bus;
_containerService = containerService;
+ _monitorService = monitorService;
}
public CommandResponse Handle(ResizeWindowCommand command)
@@ -29,6 +35,11 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
var resizeAmount = command.ResizeAmount;
var windowToResize = command.WindowToResize;
+ if (windowToResize is FloatingWindow)
+ {
+ ResizeFloatingWindow(windowToResize, dimensionToResize, resizeAmount);
+ return CommandResponse.Ok;
+ }
// Ignore cases where window is not tiling.
if (windowToResize is not TilingWindow)
return CommandResponse.Ok;
@@ -119,5 +130,58 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
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 currentMonitor = MonitorService.GetMonitorFromChildContainer(windowToResize);
+
+ int amount = (int)(currentMonitor.Width * resizePercentage);
+
+ var width = windowToResize.FloatingPlacement.Width;
+ var height = windowToResize.FloatingPlacement.Height;
+
+ _ = dimensionToResize switch
+ {
+ ResizeDimension.Width => width += amount,
+ ResizeDimension.Height => height += amount,
+ _ => throw new ArgumentException(null, nameof(dimensionToResize))
+ };
+
+ //Return if resize gonna make window smaller than allowed
+ //but allow increasing size (for situations if user made the window smaller
+ // than MIN_WIDHT or MIN_HEIGHT with the mouse)
+ if ((width < MIN_WIDTH || height < MIN_HEIGHT) && amount < 0)
+ return;
+
+ windowToResize.FloatingPlacement = Rect.FromXYCoordinates(windowToResize.FloatingPlacement.X, windowToResize.FloatingPlacement.Y, width, height);
+
+ _containerService.ContainersToRedraw.Add(windowToResize);
+ _bus.Invoke(new RedrawContainersCommand());
+
+ // Check if window now takes up more of another screen after moving
+ var currentWorkspace = WorkspaceService.GetWorkspaceFromChildContainer(windowToResize);
+
+ // Get workspace that encompasses most of the window after moving
+ var targetMonitor = _monitorService.GetMonitorFromHandleLocation(windowToResize.Handle);
+ var targetWorkspace = targetMonitor.DisplayedWorkspace;
+
+ // Ignore if window is still within the bounds of its current workspace.
+ if (currentWorkspace == targetWorkspace)
+ {
+ return;
+ }
+
+ // Change the window's parent workspace.
+ _bus.Invoke(new MoveContainerWithinTreeCommand(windowToResize, targetWorkspace, false));
+ _bus.Emit(new FocusChangedEvent(windowToResize));
+
+ // Redrawing again to fix weird WindowsOS dpi change behaviour
+ _containerService.ContainersToRedraw.Add(windowToResize);
+ _bus.Invoke(new RedrawContainersCommand());
+ }
}
}
diff --git a/GlazeWM.Infrastructure/Utils/UnitsHelper.cs b/GlazeWM.Infrastructure/Utils/UnitsHelper.cs
index a5f31e7a..67204959 100644
--- a/GlazeWM.Infrastructure/Utils/UnitsHelper.cs
+++ b/GlazeWM.Infrastructure/Utils/UnitsHelper.cs
@@ -6,11 +6,23 @@ namespace GlazeWM.Infrastructure.Utils
{
public static class UnitsHelper
{
+ /// <summary>
+ /// Returns the number part of amount with units as signed integer
+ /// </summary>
public static int TrimUnits(string amountWithUnits)
{
var unitsRegex = new Regex("(%|ppt|px)");
var amount = unitsRegex.Replace(amountWithUnits, "").Trim();
return Convert.ToInt32(amount, CultureInfo.InvariantCulture);
}
+ /// <summary>
+ /// Returns the unit part of amount with units as a string
+ /// </summary>
+ public static string GetUnits(string amountWithUnits)
+ {
+ var unitsRegex = new Regex("(%|ppt|px)");
+ var match = unitsRegex.Match(amountWithUnits);
+ return match.Value;
+ }
}
}
diff --git a/GlazeWM.Infrastructure/WindowsApi/WindowRect.cs b/GlazeWM.Infrastructure/WindowsApi/WindowRect.cs
index e484f7f9..8fd04a61 100644
--- a/GlazeWM.Infrastructure/WindowsApi/WindowRect.cs
+++ b/GlazeWM.Infrastructure/WindowsApi/WindowRect.cs
@@ -70,5 +70,21 @@ namespace GlazeWM.Infrastructure.WindowsApi
outerRect.Y + (outerRect.Height / 2) - (Height / 2)
);
}
+ public Point GetCenterPoint()
+ {
+ return new Point
+ {
+ X = X + (Width / 2),
+ Y = Y + (Height / 2),
+ };
+ }
+ public Point GetCenterPoint(Rect rect)
+ {
+ return new Point
+ {
+ X = rect.X + (rect.Width / 2),
+ Y = rect.Y + (rect.Height / 2),
+ };
+ }
}
}