Skip to content

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

feat: add `FocusedContainerMovedEvent` and `NativeFocusReassignedEvent` (#378)

blam7f2e1dee888c
diff --git a/GlazeWM.App.IpcServer/IpcMessageHandler.cs b/GlazeWM.App.IpcServer/IpcMessageHandler.cs
index 2fdbbdc6..10bc009c 100644
--- a/GlazeWM.App.IpcServer/IpcMessageHandler.cs
+++ b/GlazeWM.App.IpcServer/IpcMessageHandler.cs
@@ -60,6 +60,7 @@ namespace GlazeWM.App.IpcServer
{
DomainEvent.BindingModeChanged,
DomainEvent.FocusChanged,
+ DomainEvent.FocusedContainerMoved,
DomainEvent.MonitorAdded,
DomainEvent.MonitorRemoved,
DomainEvent.TilingDirectionChanged,
diff --git a/GlazeWM.App.WindowManager/WmStartup.cs b/GlazeWM.App.WindowManager/WmStartup.cs
index 373fcf55..94291d37 100644
--- a/GlazeWM.App.WindowManager/WmStartup.cs
+++ b/GlazeWM.App.WindowManager/WmStartup.cs
@@ -74,10 +74,23 @@ namespace GlazeWM.App.WindowManager
_systemTrayIcon = new SystemTrayIcon(systemTrayIconConfig);
_systemTrayIcon.Show();
+ var nativeFocusReassigned = _bus.Events
+ .OfType<NativeFocusReassignedEvent>()
+ .Select((@event) => @event.FocusedContainer);
+
if (_userConfigService.FocusBorderConfig.Active.Enabled ||
_userConfigService.FocusBorderConfig.Inactive.Enabled)
- _bus.Events.OfType<FocusChangedEvent>().Subscribe((@event) =>
- _bus.InvokeAsync(new SetActiveWindowBorderCommand(@event.FocusedContainer as Window)));
+ {
+ var focusChanged = _bus.Events
+ .OfType<FocusChangedEvent>()
+ .Select(@event => @event.FocusedContainer);
+
+ focusChanged
+ .Merge(nativeFocusReassigned)
+ .Subscribe((container) =>
+ _bus.InvokeAsync(new SetActiveWindowBorderCommand(container as Window))
+ );
+ }
// Hook mouse event for focus follows cursor.
if (_userConfigService.GeneralConfig.FocusFollowsCursor)
@@ -87,6 +100,18 @@ namespace GlazeWM.App.WindowManager
_bus.InvokeAsync(new FocusContainerUnderCursorCommand(@event.Point));
});
+ // Setup cursor follows focus
+ if (_userConfigService.GeneralConfig.CursorFollowsFocus)
+ {
+ var focusedContainerMoved = _bus.Events
+ .OfType<FocusedContainerMovedEvent>()
+ .Select(@event => @event.FocusedContainer);
+
+ focusedContainerMoved.Merge(nativeFocusReassigned)
+ .Where(container => container is Window)
+ .Subscribe((window) => _bus.InvokeAsync(new CenterCursorOnContainerCommand(window)));
+ }
+
System.Windows.Forms.Application.Run();
return ExitCode.Success;
}
diff --git a/GlazeWM.Domain/Common/DomainEvent.cs b/GlazeWM.Domain/Common/DomainEvent.cs
index 0bc986ce..a5291668 100644
--- a/GlazeWM.Domain/Common/DomainEvent.cs
+++ b/GlazeWM.Domain/Common/DomainEvent.cs
@@ -4,8 +4,10 @@ namespace GlazeWM.Domain.Common
{
public const string BindingModeChanged = "binding_mode_changed";
public const string FocusChanged = "focus_changed";
+ public const string FocusedContainerMoved = "focused_container_moved";
public const string MonitorAdded = "monitor_added";
public const string MonitorRemoved = "monitor_removed";
+ public const string NativeFocusReassigned = "native_focus_reassigned";
public const string TilingDirectionChanged = "tiling_direction_changed";
public const string UserConfigReloaded = "user_config_reloaded";
public const string WindowManaged = "window_managed";
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
index 661ceb73..882b1273 100644
--- a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
+++ b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using GlazeWM.Domain.Containers.Commands;
+using GlazeWM.Domain.Containers.Events;
using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Infrastructure.Utils;
@@ -9,10 +10,12 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
internal sealed class MoveContainerWithinTreeHandler : ICommandHandler<MoveContainerWithinTreeCommand>
{
private readonly Bus _bus;
+ private readonly ContainerService _containerService;
- public MoveContainerWithinTreeHandler(Bus bus)
+ public MoveContainerWithinTreeHandler(Bus bus, ContainerService containerService)
{
_bus = bus;
+ _containerService = containerService;
}
public CommandResponse Handle(MoveContainerWithinTreeCommand command)
@@ -32,6 +35,8 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
targetParent
);
+ var focusedContainer = _containerService.FocusedContainer;
+
// Handle case where target parent is the LCA (eg. in the case of swapping sibling containers
// or moving a container to a direct ancestor).
if (targetParent == lowestCommonAncestor)
@@ -43,8 +48,8 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
shouldAdjustSize
);
- // Center cursor in focused window's new location
- _bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToMove));
+ if (containerToMove == focusedContainer)
+ _bus.Emit(new FocusedContainerMovedEvent(containerToMove));
return CommandResponse.Ok;
}
@@ -90,8 +95,8 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
targetParentAncestor
);
- // Center cursor in focused window's new location
- _bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToMove));
+ if (containerToMove == focusedContainer)
+ _bus.Emit(new FocusedContainerMovedEvent(containerToMove));
return CommandResponse.Ok;
}
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs
index 1909bd25..e8dbf9b8 100644
--- a/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs
+++ b/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs
@@ -36,6 +36,8 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
KeybdEvent(0, 0, 0, 0);
SetForegroundWindow(handleToFocus);
+ _bus.Emit(new NativeFocusReassignedEvent(containerToFocus));
+
// Setting focus to the desktop window does not emit `EVENT_SYSTEM_FOREGROUND` window event,
// so `SetFocusedDescendantCommand` has to be manually called.
// TODO: This is called twice unnecessarily when setting workspace focus on unmanage.
@@ -45,9 +47,6 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
_bus.Emit(new FocusChangedEvent(containerToFocus));
}
- // Center cursor in focused window's new location
- _bus.InvokeAsync(new CenterCursorOnContainerCommand(containerToFocus));
-
return CommandResponse.Ok;
}
}
diff --git a/GlazeWM.Domain/Containers/Events/FocusedContainerMovedEvent.cs b/GlazeWM.Domain/Containers/Events/FocusedContainerMovedEvent.cs
new file mode 100644
index 00000000..a781a904
--- /dev/null
+++ b/GlazeWM.Domain/Containers/Events/FocusedContainerMovedEvent.cs
@@ -0,0 +1,8 @@
+using GlazeWM.Domain.Common;
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Domain.Containers.Events
+{
+ public record FocusedContainerMovedEvent(Container FocusedContainer)
+ : Event(DomainEvent.FocusedContainerMoved);
+}
diff --git a/GlazeWM.Domain/Containers/Events/NativeFocusReassignedEvent.cs b/GlazeWM.Domain/Containers/Events/NativeFocusReassignedEvent.cs
new file mode 100644
index 00000000..7c1795d0
--- /dev/null
+++ b/GlazeWM.Domain/Containers/Events/NativeFocusReassignedEvent.cs
@@ -0,0 +1,8 @@
+using GlazeWM.Domain.Common;
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Domain.Containers.Events
+{
+ public record NativeFocusReassignedEvent(Container FocusedContainer)
+ : Event(DomainEvent.NativeFocusReassigned);
+}