Skip to content

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

feat(domain): adjust for DPI difference after moving window across monitors

Lars Berger0ddb185f66fd
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs
index 7a381677..ad55b10d 100644
--- a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs
+++ b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs
@@ -2,6 +2,7 @@
using System.Linq;
using GlazeWM.Domain.Containers;
using GlazeWM.Domain.Containers.Commands;
+using GlazeWM.Domain.Monitors;
using GlazeWM.Domain.UserConfigs;
using GlazeWM.Domain.Windows.Commands;
using GlazeWM.Domain.Workspaces;
@@ -16,14 +17,23 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
private UserConfigService _userConfigService;
private CommandParsingService _commandParsingService;
private WindowService _windowService;
-
- public AddWindowHandler(Bus bus, ContainerService containerService, UserConfigService userConfigService, CommandParsingService commandParsingService, WindowService windowService)
+ private MonitorService _monitorService;
+
+ public AddWindowHandler(
+ Bus bus,
+ ContainerService containerService,
+ UserConfigService userConfigService,
+ CommandParsingService commandParsingService,
+ WindowService windowService,
+ MonitorService monitorService
+ )
{
_bus = bus;
_containerService = containerService;
_userConfigService = userConfigService;
_commandParsingService = commandParsingService;
_windowService = windowService;
+ _monitorService = monitorService;
}
public CommandResponse Handle(AddWindowCommand command)
@@ -57,7 +67,15 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
// sibling of the focused container.
AttachChildWindow(window, targetParent);
- // Set the newly added window as focus descendant.
+ // The OS might spawn the window on a different monitor to the target parent, so adjustments
+ // might need to be made because of DPI.
+ var monitor = _monitorService.GetMonitorFromUnmanagedHandle(windowHandle);
+ if (_monitorService.HasDpiDifference(monitor, window.Parent))
+ window.HasPendingDpiAdjustment = true;
+
+ // Set the newly added window as focus descendant. This is necessary because
+ // `EVENT_SYSTEM_FOREGROUND` is emitted before `EVENT_OBJECT_SHOW` and thus, focus state
+ // isn't updated automatically.
_bus.Invoke(new SetFocusedDescendantCommand(window));
var parsedCommands = commandStrings
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.cs
index 66f8bbec..5b3189af 100644
--- a/GlazeWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.cs
+++ b/GlazeWM.Domain/Windows/CommandHandlers/MoveFocusedWindowHandler.cs
@@ -125,15 +125,16 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
if (workspaceInDirection == null)
return;
+ // Since window is crossing monitors, adjustments might need to be made because of DPI.
+ if (_monitorService.HasDpiDifference(focusedWindow, workspaceInDirection))
+ focusedWindow.HasPendingDpiAdjustment = true;
+
// TODO: Descend into container if possible.
if (direction == Direction.UP || direction == Direction.LEFT)
_bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, workspaceInDirection, true));
else
_bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, workspaceInDirection, 0, true));
- // Since window has crossed monitors, adjustments might need to be made because of DPI.
- focusedWindow.HasPendingDpiAdjustment = true;
-
_bus.Invoke(new RedrawContainersCommand());
// Refresh state in bar of which workspace has focus.
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.cs
index b63fb7c5..c6df7b74 100644
--- a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.cs
+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveFocusedWindowToWorkspaceHandler.cs
@@ -37,13 +37,25 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
var targetWorkspace = _workspaceService.GetActiveWorkspaceByName(workspaceName)
?? ActivateWorkspace(workspaceName);
+ // Since target workspace could be on a different monitor, adjustments might need to be made
+ // because of DPI.
+ if (_monitorService.HasDpiDifference(currentWorkspace, targetWorkspace))
+ focusedWindow.HasPendingDpiAdjustment = true;
+
var insertionTarget = targetWorkspace.LastFocusedDescendant;
// Insert the focused window into the target workspace.
if (insertionTarget == null)
_bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, targetWorkspace, true));
else
- _bus.Invoke(new MoveContainerWithinTreeCommand(focusedWindow, insertionTarget.Parent, insertionTarget.Index + 1, true));
+ _bus.Invoke(
+ new MoveContainerWithinTreeCommand(
+ focusedWindow,
+ insertionTarget.Parent,
+ insertionTarget.Index + 1,
+ true
+ )
+ );
// Reassign focus to descendant within the current workspace.
_bus.Invoke(new FocusWorkspaceCommand(currentWorkspace.Name));