Skip to content

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

refactor(infra,domain): move logic for translating windows to `WindowRect` methods

Lars Bergerbcdb2e2d756a
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs
index 9724c2da..719cf311 100644
--- a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs
+++ b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs
@@ -60,10 +60,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
// Calculate where window should be placed when floating is enabled. Use the original
// width/height of the window, but position it in the center of the workspace.
var originalPlacement = _windowService.GetPlacementOfHandle(windowHandle).NormalPosition;
- var floatingPlacement = WindowRect.TranslateToCenter(
- originalPlacement,
- targetWorkspace.ToRectangle()
- );
+ var floatingPlacement = originalPlacement.TranslateToCenter(targetWorkspace.ToRectangle());
// Create the window instance.
var window = new TilingWindow(command.WindowHandle, floatingPlacement);
diff --git a/GlazeWM.Domain/Windows/FloatingWindow.cs b/GlazeWM.Domain/Windows/FloatingWindow.cs
index 705e1cc0..c712aecf 100644
--- a/GlazeWM.Domain/Windows/FloatingWindow.cs
+++ b/GlazeWM.Domain/Windows/FloatingWindow.cs
@@ -5,13 +5,13 @@ namespace GlazeWM.Domain.Windows
{
public sealed class FloatingWindow : Window
{
- public override int Width => FloatingPlacement.Right - FloatingPlacement.Left;
+ public override int Width => FloatingPlacement.Width;
- public override int Height => FloatingPlacement.Bottom - FloatingPlacement.Top;
+ public override int Height => FloatingPlacement.Height;
- public override int X => FloatingPlacement.Left;
+ public override int X => FloatingPlacement.X;
- public override int Y => FloatingPlacement.Top;
+ public override int Y => FloatingPlacement.Y;
public FloatingWindow(IntPtr hwnd, WindowRect floatingPlacement) : base(hwnd, floatingPlacement)
{
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.cs
index 7d697cbd..4521e424 100644
--- a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.cs
+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.cs
@@ -72,19 +72,21 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
// to adjust its position.
if (currentMonitor != targetMonitor)
{
- var relativeX = focusedWindow.X - currentWorkspace.X + (focusedWindow.Width / 2);
- var relativeY = focusedWindow.Y - currentWorkspace.Y + (focusedWindow.Height / 2);
-
- // TODO: Clean this up.
- var updatedPlacement = new WindowRect
- {
- Left = targetWorkspace.X + (relativeX * targetWorkspace.Width / currentWorkspace.Width - (focusedWindow.Width / 2)),
- Right = targetWorkspace.X + (relativeX * targetWorkspace.Width / currentWorkspace.Width - (focusedWindow.Width / 2)) + focusedWindow.Width,
- Top = targetWorkspace.Y + (relativeY * targetWorkspace.Height / currentWorkspace.Height - (focusedWindow.Height / 2)),
- Bottom = targetWorkspace.Y + (relativeY * targetWorkspace.Height / currentWorkspace.Height - (focusedWindow.Height / 2)) + focusedWindow.Height,
- };
-
- focusedWindow.FloatingPlacement = updatedPlacement;
+ // Get the window's X/Y coordinates relative to the current workspace's X/Y coordinates.
+ var workspaceOffsetX = focusedWindow.X - currentWorkspace.X;
+ var workspaceOffsetY = focusedWindow.Y - currentWorkspace.Y;
+
+ // Get the window's X/Y coordinates relative to the target workspace's X/Y coordinates.
+ var targetWorkspaceOffsetX = (workspaceOffsetX * targetWorkspace.Width)
+ / currentWorkspace.Width;
+ var targetWorkspaceOffsetY = (workspaceOffsetY * targetWorkspace.Height)
+ / currentWorkspace.Height;
+
+ // TODO: Need to scale width/height according to monitor DPI?
+ focusedWindow.FloatingPlacement = focusedWindow.FloatingPlacement.TranslateToCoordinates(
+ targetWorkspace.X + targetWorkspaceOffsetX,
+ targetWorkspace.Y + targetWorkspaceOffsetY
+ );
}
// Change the window's parent workspace.
diff --git a/GlazeWM.Infrastructure/WindowsApi/WindowRect.cs b/GlazeWM.Infrastructure/WindowsApi/WindowRect.cs
index 4db357fa..3ed4fc6f 100644
--- a/GlazeWM.Infrastructure/WindowsApi/WindowRect.cs
+++ b/GlazeWM.Infrastructure/WindowsApi/WindowRect.cs
@@ -30,15 +30,39 @@
public int Height => Bottom - Top;
- public static WindowRect TranslateToCenter(WindowRect rect, WindowRect outerRect)
+ public WindowRect Translate(int deltaX, int deltaY)
{
var translatedRect = new WindowRect();
- translatedRect.Left = outerRect.X + (outerRect.Width / 2) - (rect.Width / 2);
- translatedRect.Right = translatedRect.Left + rect.Width;
+ translatedRect.Left = Left + deltaX;
+ translatedRect.Right = Left + deltaX + Width;
+ translatedRect.Top = Top + deltaY;
+ translatedRect.Bottom = Top + deltaY + Height;
- translatedRect.Top = outerRect.Y + (outerRect.Height / 2) - (rect.Height / 2);
- translatedRect.Bottom = translatedRect.Top + rect.Height;
+ return translatedRect;
+ }
+
+ public WindowRect TranslateToCoordinates(int x, int y)
+ {
+ var translatedRect = new WindowRect();
+
+ translatedRect.Left = x;
+ translatedRect.Right = x + Width;
+ translatedRect.Top = y;
+ translatedRect.Bottom = y + Height;
+
+ return translatedRect;
+ }
+
+ public WindowRect TranslateToCenter(WindowRect outerRect)
+ {
+ var translatedRect = new WindowRect();
+
+ translatedRect.Left = outerRect.X + (outerRect.Width / 2) - (Width / 2);
+ translatedRect.Right = translatedRect.Left + Width;
+
+ translatedRect.Top = outerRect.Y + (outerRect.Height / 2) - (Height / 2);
+ translatedRect.Bottom = translatedRect.Top + Height;
return translatedRect;
}