GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: add internal command for setting OS focus to a container (#139)
lars-bergerf5c5251c216d
diff --git a/GlazeWM.Domain/Common/CommandHandlers/PopulateInitialStateHandler.cs b/GlazeWM.Domain/Common/CommandHandlers/PopulateInitialStateHandler.csindex 7af6d937..8b1ec26e 100644--- a/GlazeWM.Domain/Common/CommandHandlers/PopulateInitialStateHandler.cs+++ b/GlazeWM.Domain/Common/CommandHandlers/PopulateInitialStateHandler.cs@@ -9,7 +9,6 @@ using GlazeWM.Domain.UserConfigs.Commands;using GlazeWM.Domain.Windows;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces;-using GlazeWM.Domain.Workspaces.Commands;using GlazeWM.Infrastructure.Bussing;using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;@@ -62,11 +61,7 @@ namespace GlazeWM.Domain.Common.CommandHandlers_bus.Invoke(new SetFocusedDescendantCommand(containerToFocus));_bus.Emit(new FocusChangedEvent(containerToFocus));-- if (containerToFocus is Window)- _bus.Invoke(new FocusWindowCommand(containerToFocus as Window));- else if (containerToFocus is Workspace)- _bus.Invoke(new FocusWorkspaceCommand((containerToFocus as Workspace).Name));+ _bus.Invoke(new SetNativeFocusCommand(containerToFocus));return CommandResponse.Ok;}diff --git a/GlazeWM.Domain/Containers/CommandHandlers/FocusInDirectionHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/FocusInDirectionHandler.csindex 21ba8090..ddb23293 100644--- a/GlazeWM.Domain/Containers/CommandHandlers/FocusInDirectionHandler.cs+++ b/GlazeWM.Domain/Containers/CommandHandlers/FocusInDirectionHandler.cs@@ -3,9 +3,7 @@ using GlazeWM.Domain.Common.Enums;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Monitors;using GlazeWM.Domain.Windows;-using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces;-using GlazeWM.Domain.Workspaces.Commands;using GlazeWM.Infrastructure.Bussing;namespace GlazeWM.Domain.Containers.CommandHandlers@@ -58,17 +56,14 @@ namespace GlazeWM.Domain.Containers.CommandHandlersif (focusTarget == null || focusTarget == focusedContainer)return;- _bus.Invoke(new FocusWindowCommand(focusTarget as FloatingWindow));+ _bus.Invoke(new SetNativeFocusCommand(focusTarget));}private void FocusFromTilingContainer(Container focusedContainer, Direction direction){var focusTarget = GetFocusTarget(focusedContainer, direction);- if (focusTarget is Window)- _bus.Invoke(new FocusWindowCommand(focusTarget as Window));- else if (focusTarget is Workspace)- _bus.Invoke(new FocusWorkspaceCommand((focusTarget as Workspace).Name));+ _bus.Invoke(new SetNativeFocusCommand(focusTarget));}private Container GetFocusTarget(Container focusedContainer, Direction direction)diff --git a/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.csnew file mode 100644index 00000000..b14ab233--- /dev/null+++ b/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs@@ -0,0 +1,44 @@+using System;+using GlazeWM.Domain.Containers.Commands;+using GlazeWM.Domain.Windows;+using GlazeWM.Domain.Workspaces;+using GlazeWM.Infrastructure.Bussing;+using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;++namespace GlazeWM.Domain.Containers.CommandHandlers+{+ internal class SetNativeFocusHandler : ICommandHandler<SetNativeFocusCommand>+ {+ private readonly Bus _bus;++ public SetNativeFocusHandler(Bus bus)+ {+ _bus = bus;+ }++ public CommandResponse Handle(SetNativeFocusCommand command)+ {+ var containerToFocus = command.ContainerToFocus;++ var handleToFocus = containerToFocus switch+ {+ Window window => window.Handle,+ Workspace => GetDesktopWindow(),+ _ => throw new Exception("Invalid container type to focus. This is a bug."),+ };++ // Set focus to the given window handle. If the container is a normal window, then this+ // will trigger `EVENT_SYSTEM_FOREGROUND` window event and its handler. This, in turn,+ // calls `SetFocusedDescendantCommand`.+ KeybdEvent(0, 0, 0, 0);+ SetForegroundWindow(handleToFocus);++ // Setting focus to the desktop window does not emit `EVENT_SYSTEM_FOREGROUND` window event,+ // so `SetFocusedDescendantCommand` has to be manually called.+ if (containerToFocus is Workspace)+ _bus.Invoke(new SetFocusedDescendantCommand(containerToFocus));++ return CommandResponse.Ok;+ }+ }+}diff --git a/GlazeWM.Domain/Containers/CommandHandlers/ToggleFocusModeHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/ToggleFocusModeHandler.csindex 5a6f90a1..412f7234 100644--- a/GlazeWM.Domain/Containers/CommandHandlers/ToggleFocusModeHandler.cs+++ b/GlazeWM.Domain/Containers/CommandHandlers/ToggleFocusModeHandler.cs@@ -1,7 +1,6 @@using GlazeWM.Domain.Common.Enums;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Windows;-using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces;using GlazeWM.Infrastructure.Bussing;@@ -30,25 +29,26 @@ namespace GlazeWM.Domain.Containers.CommandHandlers? FocusMode.FLOATING: FocusMode.TILING;- var focusedWorkspace = _workspaceService.GetFocusedWorkspace();-- Window windowToFocus;+ var windowToFocus = GetWindowToFocus(targetFocusMode);- if (targetFocusMode == FocusMode.FLOATING)- // Get the last focused tiling window within the workspace.- windowToFocus = focusedWorkspace.LastFocusedDescendantOfType<FloatingWindow>()- as Window;- else- // Get the last focused floating window within the workspace.- windowToFocus = focusedWorkspace.LastFocusedDescendantOfType<TilingWindow>()- as Window;-- if (windowToFocus == null)+ if (windowToFocus is null)return CommandResponse.Ok;- _bus.Invoke(new FocusWindowCommand(windowToFocus));+ _bus.Invoke(new SetNativeFocusCommand(windowToFocus));return CommandResponse.Ok;}++ private Window GetWindowToFocus(FocusMode targetFocusMode)+ {+ var focusedWorkspace = _workspaceService.GetFocusedWorkspace();++ if (targetFocusMode == FocusMode.FLOATING)+ // Get the last focused tiling window within the workspace.+ return focusedWorkspace.LastFocusedDescendantOfType<FloatingWindow>() as Window;++ // Get the last focused floating window within the workspace.+ return focusedWorkspace.LastFocusedDescendantOfType<TilingWindow>() as Window;+ }}}diff --git a/GlazeWM.Domain/Containers/Commands/SetNativeFocusCommand.cs b/GlazeWM.Domain/Containers/Commands/SetNativeFocusCommand.csnew file mode 100644index 00000000..968f6e82--- /dev/null+++ b/GlazeWM.Domain/Containers/Commands/SetNativeFocusCommand.cs@@ -0,0 +1,14 @@+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Containers.Commands+{+ public class SetNativeFocusCommand : Command+ {+ public Container ContainerToFocus { get; }++ public SetNativeFocusCommand(Container containerToFocus)+ {+ ContainerToFocus = containerToFocus;+ }+ }+}diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.csindex 5dfa94ac..5ce44b50 100644--- a/GlazeWM.Domain/DependencyInjection.cs+++ b/GlazeWM.Domain/DependencyInjection.cs@@ -47,6 +47,7 @@ namespace GlazeWM.Domainservices.AddSingleton<ICommandHandler<RedrawContainersCommand>, RedrawContainersHandler>();services.AddSingleton<ICommandHandler<ReplaceContainerCommand>, ReplaceContainerHandler>();services.AddSingleton<ICommandHandler<SetFocusedDescendantCommand>, SetFocusedDescendantHandler>();+ services.AddSingleton<ICommandHandler<SetNativeFocusCommand>, SetNativeFocusHandler>();services.AddSingleton<ICommandHandler<ToggleFocusModeCommand>, ToggleFocusModeHandler>();services.AddSingleton<ICommandHandler<AddMonitorCommand>, AddMonitorHandler>();services.AddSingleton<ICommandHandler<RefreshMonitorStateCommand>, RefreshMonitorStateHandler>();@@ -55,7 +56,6 @@ namespace GlazeWM.Domainservices.AddSingleton<ICommandHandler<RegisterKeybindingsCommand>, RegisterKeybindingsHandler>();services.AddSingleton<ICommandHandler<ReloadUserConfigCommand>, ReloadUserConfigHandler>();services.AddSingleton<ICommandHandler<CloseWindowCommand>, CloseWindowHandler>();- services.AddSingleton<ICommandHandler<FocusWindowCommand>, FocusWindowHandler>();services.AddSingleton<ICommandHandler<IgnoreWindowCommand>, IgnoreWindowHandler>();services.AddSingleton<ICommandHandler<ManageWindowCommand>, ManageWindowHandler>();services.AddSingleton<ICommandHandler<MoveWindowCommand>, MoveWindowHandler>();diff --git a/GlazeWM.Domain/Windows/CommandHandlers/FocusWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/FocusWindowHandler.csdeleted file mode 100644index 38da2869..00000000--- a/GlazeWM.Domain/Windows/CommandHandlers/FocusWindowHandler.cs+++ /dev/null@@ -1,21 +0,0 @@-using GlazeWM.Domain.Windows.Commands;-using GlazeWM.Infrastructure.Bussing;-using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;--namespace GlazeWM.Domain.Windows.CommandHandlers-{- internal class FocusWindowHandler : ICommandHandler<FocusWindowCommand>- {- public CommandResponse Handle(FocusWindowCommand command)- {- var window = command.Window;-- // Set as foreground window if it's not already set. This will trigger `EVENT_SYSTEM_FOREGROUND`- // window event and its handler.- KeybdEvent(0, 0, 0, 0);- SetForegroundWindow(window.Handle);-- return CommandResponse.Ok;- }- }-}diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.csindex 8357168f..b94dd0e1 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs@@ -78,7 +78,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlers_bus.Invoke(new RedrawContainersCommand());// Set OS focus to the newly added window in case it's not already focused.- _bus.Invoke(new FocusWindowCommand(window));+ _bus.Invoke(new SetNativeFocusCommand(window));return CommandResponse.Ok;}diff --git a/GlazeWM.Domain/Windows/Commands/FocusWindowCommand.cs b/GlazeWM.Domain/Windows/Commands/FocusWindowCommand.csdeleted file mode 100644index cac64f8e..00000000--- a/GlazeWM.Domain/Windows/Commands/FocusWindowCommand.cs+++ /dev/null@@ -1,14 +0,0 @@-using GlazeWM.Infrastructure.Bussing;--namespace GlazeWM.Domain.Windows.Commands-{- public class FocusWindowCommand : Command- {- public Window Window { get; set; }-- public FocusWindowCommand(Window window)- {- Window = window;- }- }-}diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.csindex 0e356e22..36ab1712 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs@@ -3,12 +3,9 @@ using GlazeWM.Domain.Common.Utils;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Containers.Events;-using GlazeWM.Domain.Windows.Commands;-using GlazeWM.Domain.Workspaces;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.Common.Events;using Microsoft.Extensions.Logging;-using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;namespace GlazeWM.Domain.Windows.EventHandlers{@@ -38,15 +35,7 @@ namespace GlazeWM.Domain.Windows.EventHandlers// Override the container to set focus to (ie. when changing focus after a window is closed).if (pendingFocusContainer != null){- if (pendingFocusContainer is Window)- _bus.Invoke(new FocusWindowCommand(pendingFocusContainer as Window));- else if (pendingFocusContainer is Workspace)- {- _bus.Invoke(new SetFocusedDescendantCommand(pendingFocusContainer));- KeybdEvent(0, 0, 0, 0);- SetForegroundWindow(GetDesktopWindow());- }-+ _bus.Invoke(new SetNativeFocusCommand(pendingFocusContainer));_containerService.PendingFocusContainer = null;return;}diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.csindex 1a004b14..bfd9563a 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs@@ -2,12 +2,10 @@ using System.Linq;using GlazeWM.Domain.Common.Utils;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;-using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.Common.Events;using Microsoft.Extensions.Logging;-using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;namespace GlazeWM.Domain.Windows.EventHandlers{@@ -57,15 +55,7 @@ namespace GlazeWM.Domain.Windows.EventHandlers_bus.Invoke(new ReplaceContainerCommand(minimizedWindow, window.Parent, window.Index));var focusTarget = workspace.LastFocusedDescendantExcluding(minimizedWindow) ?? workspace;-- if (focusTarget is Window)- _bus.Invoke(new FocusWindowCommand(focusTarget as Window));- else if (focusTarget is Workspace)- {- _bus.Invoke(new SetFocusedDescendantCommand(focusTarget));- KeybdEvent(0, 0, 0, 0);- SetForegroundWindow(GetDesktopWindow());- }+ _bus.Invoke(new SetNativeFocusCommand(focusTarget));_containerService.ContainersToRedraw.Add(workspace);_bus.Invoke(new RedrawContainersCommand());diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.csindex 6291430c..fd9706a8 100644--- a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs@@ -2,13 +2,10 @@using GlazeWM.Domain.Monitors;using GlazeWM.Domain.UserConfigs;using GlazeWM.Domain.Workspaces.Commands;-using GlazeWM.Domain.Windows.Commands;-using GlazeWM.Domain.Windows;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Events;using GlazeWM.Domain.Containers.Commands;using System.Linq;-using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;namespace GlazeWM.Domain.Workspaces.CommandHandlers{@@ -63,15 +60,7 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers_containerService.ContainersToRedraw.Add(workspaceToFocus);_bus.Invoke(new RedrawContainersCommand());- // Container to focus is either a window or a workspace.- if (containerToFocus is Window)- _bus.Invoke(new FocusWindowCommand(containerToFocus as Window));- else- {- // Remove focus from whichever window currently has focus.- KeybdEvent(0, 0, 0, 0);- SetForegroundWindow(GetDesktopWindow());- }+ _bus.Invoke(new SetNativeFocusCommand(containerToFocus));// Get empty workspace to destroy (if any are found). Cannot destroy empty workspaces if// they're the only workspace on the monitor or are pending focus.diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.csindex 3540f6a3..7ffe3c74 100644--- a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.cs+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.cs@@ -4,9 +4,7 @@ using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.UserConfigs;using GlazeWM.Domain.Windows;-using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces.Commands;-using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;namespace GlazeWM.Domain.Workspaces.CommandHandlers{@@ -103,15 +101,12 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlersprivate void ReassignFocusWithinWorkspace(Workspace workspace){var containerToFocus = workspace.LastFocusedDescendant ?? workspace;- _bus.Invoke(new SetFocusedDescendantCommand(containerToFocus));- if (containerToFocus is Window)- _bus.Invoke(new FocusWindowCommand(containerToFocus as Window));- else- {- KeybdEvent(0, 0, 0, 0);- SetForegroundWindow(GetDesktopWindow());- }+ // Need to call `SetFocusedDescendantCommand` for when commands like `FocusWorkspaceCommand`+ // are called immediately afterwards and they should behave as if `containerToFocus` is the+ // focused descendant.+ _bus.Invoke(new SetFocusedDescendantCommand(containerToFocus));+ _bus.Invoke(new SetNativeFocusCommand(containerToFocus));}}}