GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
fix: correct null reference when centering cursor on container (#212)
blame52a5dcbb94a
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/CenterCursorOnContainerHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/CenterCursorOnContainerHandler.csdeleted file mode 100644index d60ad35b..00000000--- a/GlazeWM.Domain/Containers/CommandHandlers/CenterCursorOnContainerHandler.cs+++ /dev/null@@ -1,39 +0,0 @@-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/CenterCursorOnRectHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/CenterCursorOnRectHandler.csnew file mode 100644index 00000000..e672a1cf--- /dev/null+++ b/GlazeWM.Domain/Containers/CommandHandlers/CenterCursorOnRectHandler.cs@@ -0,0 +1,34 @@+using GlazeWM.Domain.Containers.Commands;+using GlazeWM.Domain.UserConfigs;++using GlazeWM.Infrastructure.Bussing;+using GlazeWM.Infrastructure.WindowsApi;+using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;++namespace GlazeWM.Domain.Containers.CommandHandlers+{+ internal class CenterCursorOnRectHandler : ICommandHandler<CenterCursorOnRectCommand>+ {+ private readonly UserConfigService _userConfigService;++ public CenterCursorOnRectHandler(UserConfigService userConfigService)+ {+ _userConfigService = userConfigService;+ }++ public CommandResponse Handle(CenterCursorOnRectCommand command)+ {+ var enabled = _userConfigService.GeneralConfig.CursorFollowsFocus;+ if (!enabled)+ return CommandResponse.Ok;++ Rect TargetRect = command.TargetRect;++ // Calculate center point of focused window+ var centerX = TargetRect.X + (TargetRect.Width / 2);+ var centerY = TargetRect.Y + (TargetRect.Height / 2);+ SetCursorPos(centerX, centerY);+ return CommandResponse.Ok;+ }+ }+}diff --git a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.csindex e09ad8c3..ee6ceec5 100644--- a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs+++ b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs@@ -44,7 +44,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlers);// Center cursor in focused window's new location- _bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToMove));+ _bus.InvokeAsync(new CenterCursorOnRectCommand(containerToMove.ToRect()));return CommandResponse.Ok;}@@ -91,7 +91,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlers);// Center cursor in focused window's new location- _bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToMove));+ _bus.InvokeAsync(new CenterCursorOnRectCommand(containerToMove.ToRect()));return CommandResponse.Ok;}diff --git a/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.csindex 1b6eb883..54653a26 100644--- a/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs+++ b/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs@@ -42,7 +42,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlers_bus.Emit(new FocusChangedEvent(containerToFocus));}- _bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToFocus));+ _bus.InvokeAsync(new CenterCursorOnRectCommand(containerToFocus.ToRect()));return CommandResponse.Ok;}diff --git a/GlazeWM.Domain/Containers/Commands/CenterCursorOnContainerCommand.cs b/GlazeWM.Domain/Containers/Commands/CenterCursorOnRectCommand.cssimilarity index 50%rename from GlazeWM.Domain/Containers/Commands/CenterCursorOnContainerCommand.csrename to GlazeWM.Domain/Containers/Commands/CenterCursorOnRectCommand.csindex f944e5cd..b7004783 100644--- a/GlazeWM.Domain/Containers/Commands/CenterCursorOnContainerCommand.cs+++ b/GlazeWM.Domain/Containers/Commands/CenterCursorOnRectCommand.cs@@ -1,17 +1,18 @@using GlazeWM.Infrastructure.Bussing;+using GlazeWM.Infrastructure.WindowsApi;namespace GlazeWM.Domain.Containers.Commands{- public class CenterCursorOnContainerCommand : Command+ public class CenterCursorOnRectCommand : Command{- public Container TargetContainer { get; }+ public Rect TargetRect { get; }/// <summary>/// Center cursor in the middle of target container/// </summary>- public CenterCursorOnContainerCommand(Container target)+ public CenterCursorOnRectCommand(Rect target){- TargetContainer = target;+ TargetRect = target;}}}diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.csindex 09d5b3b9..c9329cab 100644--- a/GlazeWM.Domain/DependencyInjection.cs+++ b/GlazeWM.Domain/DependencyInjection.cs@@ -39,7 +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<CenterCursorOnRectCommand>, CenterCursorOnRectHandler>();services.AddSingleton<ICommandHandler<ChangeContainerLayoutCommand>, ChangeContainerLayoutHandler>();services.AddSingleton<ICommandHandler<DetachAndResizeContainerCommand>, DetachAndResizeContainerHandler>();services.AddSingleton<ICommandHandler<DetachContainerCommand>, DetachContainerHandler>();diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.csindex b94dd0e1..8386564f 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs@@ -19,6 +19,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlersprivate readonly ContainerService _containerService;private readonly ILogger<ManageWindowHandler> _logger;private readonly MonitorService _monitorService;+ private readonly WindowService _windowService;private readonly UserConfigService _userConfigService;public ManageWindowHandler(@@ -26,13 +27,17 @@ namespace GlazeWM.Domain.Windows.CommandHandlersContainerService containerService,ILogger<ManageWindowHandler> logger,MonitorService monitorService,+ WindowService windowService,UserConfigService userConfigService){_bus = bus;_containerService = containerService;_logger = logger;_monitorService = monitorService;+ _windowService = windowService;+_userConfigService = userConfigService;+}public CommandResponse Handle(ManageWindowCommand command)@@ -70,6 +75,9 @@ namespace GlazeWM.Domain.Windows.CommandHandlersvar windowRules = _userConfigService.GetMatchingWindowRules(window);_bus.Invoke(new RunWindowRulesCommand(window, windowRules));+ // Update window in case the reference changes.+ window = _windowService.GetWindowByHandle(window.Handle);+// Window might be detached if 'ignore' command has been invoked.if (window.IsDetached())return CommandResponse.Ok;