GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: cursor follows the focused container (#205)
blamdf534188e8a9
diff --git a/GlazeWM.Bootstrapper/sample-config.yaml b/GlazeWM.Bootstrapper/sample-config.yamlindex c7338cb8..a1a8237b 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: true+ cursor_follows_focus: truebar:height: "30px"diff --git a/GlazeWM.Domain/Containers/CommandHandlers/CenterCursorOnContainerHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/CenterCursorOnContainerHandler.csnew file mode 100644index 00000000..d60ad35b--- /dev/null+++ b/GlazeWM.Domain/Containers/CommandHandlers/CenterCursorOnContainerHandler.cs@@ -0,0 +1,39 @@+using System;+using GlazeWM.Domain.Containers.Commands;+using GlazeWM.Domain.UserConfigs;++using GlazeWM.Infrastructure.Bussing;++using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;++namespace GlazeWM.Domain.Containers.CommandHandlers+{+ internal class CenterCursorOnContainerHandler : ICommandHandler<CenterCursorOnContainerCommand>+ {+ private readonly ContainerService _containerService;+ private readonly UserConfigService _userConfigService;++ public CenterCursorOnContainerHandler(ContainerService containerService, UserConfigService userConfigService)+ {+ _containerService = containerService;+ _userConfigService = userConfigService;+ }++ public CommandResponse Handle(CenterCursorOnContainerCommand command)+ {+ var enabled = _userConfigService.GeneralConfig.CursorFollowsFocus;+ if (!enabled)+ return CommandResponse.Ok;++ var target = command.TargetContainer;++ // Calculate center point of focused window+ var centerX = target.X + target.Width / 2;+ var centerY = target.Y + target.Height / 2;++ SetCursorPos(centerX, centerY);++ return CommandResponse.Ok;+ }+ }+}diff --git a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.csindex 5ba53fb6..e09ad8c3 100644--- a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs+++ b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs@@ -43,6 +43,9 @@ namespace GlazeWM.Domain.Containers.CommandHandlersshouldAdjustSize);+ // Center cursor in focused window's new location+ _bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToMove));+return CommandResponse.Ok;}@@ -87,6 +90,9 @@ namespace GlazeWM.Domain.Containers.CommandHandlerstargetParentAncestor);+ // Center cursor in focused window's new location+ _bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToMove));+return CommandResponse.Ok;}diff --git a/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.csindex 2e4a5093..1b6eb883 100644--- a/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs+++ b/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs@@ -42,6 +42,8 @@ namespace GlazeWM.Domain.Containers.CommandHandlers_bus.Emit(new FocusChangedEvent(containerToFocus));}+ _bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToFocus));+return CommandResponse.Ok;}}diff --git a/GlazeWM.Domain/Containers/Commands/CenterCursorOnContainerCommand.cs b/GlazeWM.Domain/Containers/Commands/CenterCursorOnContainerCommand.csnew file mode 100644index 00000000..f944e5cd--- /dev/null+++ b/GlazeWM.Domain/Containers/Commands/CenterCursorOnContainerCommand.cs@@ -0,0 +1,17 @@+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Containers.Commands+{+ public class CenterCursorOnContainerCommand : Command+ {+ public Container TargetContainer { get; }++ /// <summary>+ /// Center cursor in the middle of target container+ /// </summary>+ public CenterCursorOnContainerCommand(Container target)+ {+ TargetContainer = target;+ }+ }+}diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.csindex e36448dc..09d5b3b9 100644--- a/GlazeWM.Domain/DependencyInjection.cs+++ b/GlazeWM.Domain/DependencyInjection.cs@@ -39,6 +39,7 @@ namespace GlazeWM.Domainservices.AddSingleton<ICommandHandler<SetBindingModeCommand>, SetBindingModeHandler>();services.AddSingleton<ICommandHandler<AttachAndResizeContainerCommand>, AttachAndResizeContainerHandler>();services.AddSingleton<ICommandHandler<AttachContainerCommand>, AttachContainerHandler>();+ services.AddSingleton<ICommandHandler<CenterCursorOnContainerCommand>, CenterCursorOnContainerHandler>();services.AddSingleton<ICommandHandler<ChangeContainerLayoutCommand>, ChangeContainerLayoutHandler>();services.AddSingleton<ICommandHandler<DetachAndResizeContainerCommand>, DetachAndResizeContainerHandler>();services.AddSingleton<ICommandHandler<DetachContainerCommand>, DetachContainerHandler>();diff --git a/GlazeWM.Domain/UserConfigs/GeneralConfig.cs b/GlazeWM.Domain/UserConfigs/GeneralConfig.csindex 90cae50a..9d81d267 100644--- a/GlazeWM.Domain/UserConfigs/GeneralConfig.cs+++ b/GlazeWM.Domain/UserConfigs/GeneralConfig.cs@@ -6,5 +6,9 @@ namespace GlazeWM.Domain.UserConfigs/// Whether to show floating windows as always on top./// </summary>public bool ShowFloatingOnTop { get; set; } = true;+ /// <summary>+ /// Center the cursor in the middle of a newly focused window+ /// </summary>+ public bool CursorFollowsFocus { get; set; } = true;}}diff --git a/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs b/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.csindex 49562c91..a07c5712 100644--- a/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs+++ b/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs@@ -214,6 +214,9 @@ namespace GlazeWM.Infrastructure.WindowsApi[DllImport("user32.dll")]public static extern bool SetFocus(IntPtr hWnd);+ [DllImport("user32.dll")]+ public static extern bool SetCursorPos(int X, int Y);+/// <summary>/// Params that can be passed to `ShowWindow`. Only the subset of flags relevant to/// this application are included.