Skip to content

Commit

Permalink
Merge branch 'bug/null-ref-exception-fix' into feat/window-focus-foll…
Browse files Browse the repository at this point in the history
…ows-cursor
  • Loading branch information
blam committed Jan 18, 2023
1 parent 13df734 commit 1903acf
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 47 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public CommandResponse Handle(MoveContainerWithinTreeCommand command)
);

// Center cursor in focused window's new location
_bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToMove));
_bus.InvokeAsync(new CenterCursorOnRectCommand(containerToMove.ToRect()));

return CommandResponse.Ok;
}
Expand Down Expand Up @@ -91,7 +91,7 @@ public CommandResponse Handle(MoveContainerWithinTreeCommand command)
);

// Center cursor in focused window's new location
_bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToMove));
_bus.InvokeAsync(new CenterCursorOnRectCommand(containerToMove.ToRect()));

return CommandResponse.Ok;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public CommandResponse Handle(SetNativeFocusCommand command)
_bus.Emit(new FocusChangedEvent(containerToFocus));
}

_bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToFocus));
_bus.InvokeAsync(new CenterCursorOnRectCommand(containerToFocus.ToRect()));

return CommandResponse.Ok;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 1 addition & 1 deletion GlazeWM.Domain/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static IServiceCollection AddDomainServices(this IServiceCollection servi
services.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>();
Expand Down
8 changes: 8 additions & 0 deletions GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,25 @@ internal class ManageWindowHandler : ICommandHandler<ManageWindowCommand>
private readonly ContainerService _containerService;
private readonly ILogger<ManageWindowHandler> _logger;
private readonly MonitorService _monitorService;
private readonly WindowService _windowService;
private readonly UserConfigService _userConfigService;

public ManageWindowHandler(
Bus bus,
ContainerService 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)
Expand Down Expand Up @@ -70,6 +75,9 @@ public CommandResponse Handle(ManageWindowCommand command)
var 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;
Expand Down

0 comments on commit 1903acf

Please sign in to comment.