GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
fix: improve consistency of focus reset (#227)
lars-berger7778823e1c5c
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.csindex 15dafce1..d290ae77 100644--- a/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs+++ b/GlazeWM.Domain/Containers/CommandHandlers/SetNativeFocusHandler.cs@@ -11,10 +11,12 @@ namespace GlazeWM.Domain.Containers.CommandHandlersinternal sealed class SetNativeFocusHandler : ICommandHandler<SetNativeFocusCommand>{private readonly Bus _bus;+ private readonly WindowService _windowService;- public SetNativeFocusHandler(Bus bus)+ public SetNativeFocusHandler(Bus bus, WindowService windowService){_bus = bus;+ _windowService = windowService;}public CommandResponse Handle(SetNativeFocusCommand command)@@ -24,7 +26,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlersvar handleToFocus = containerToFocus switch{Window window => window.Handle,- Workspace => GetDesktopWindow(),+ Workspace => _windowService.DesktopWindowHandle,_ => throw new Exception("Invalid container type to focus. This is a bug."),};@@ -36,6 +38,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlers// 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.if (containerToFocus is Workspace){_bus.Invoke(new SetFocusedDescendantCommand(containerToFocus));diff --git a/GlazeWM.Domain/Containers/ContainerService.cs b/GlazeWM.Domain/Containers/ContainerService.csindex 8832432c..9914c308 100644--- a/GlazeWM.Domain/Containers/ContainerService.cs+++ b/GlazeWM.Domain/Containers/ContainerService.cs@@ -38,6 +38,12 @@ namespace GlazeWM.Domain.Containers/// </summary>public string ActiveBindingMode { get; set; }+ /// <summary>+ /// If set, this container overrides the target container to set focus to on the next+ /// focus window event (ie. `EVENT_SYSTEM_FOREGROUND`).+ /// </summary>+ public Container PendingFocusContainer { get; set; }+private readonly UserConfigService _userConfigService;public ContainerService(UserConfigService userConfigService)diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.csindex df90c01d..8bb90b3f 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs@@ -77,7 +77,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlerswindow = _windowService.GetWindowByHandle(window.Handle);// Window might be detached if 'ignore' command has been invoked.- if (window.IsDetached())+ if (window?.IsDetached() != false)return CommandResponse.Ok;if (shouldRedraw)diff --git a/GlazeWM.Domain/Windows/CommandHandlers/UnmanageWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/UnmanageWindowHandler.csindex ea62595a..c46a35a8 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/UnmanageWindowHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/UnmanageWindowHandler.cs@@ -8,10 +8,12 @@ namespace GlazeWM.Domain.Windows.CommandHandlersinternal sealed class UnmanageWindowHandler : ICommandHandler<UnmanageWindowCommand>{private readonly Bus _bus;+ private readonly ContainerService _containerService;- public UnmanageWindowHandler(Bus bus)+ public UnmanageWindowHandler(Bus bus, ContainerService containerService){_bus = bus;+ _containerService = containerService;}public CommandResponse Handle(UnmanageWindowCommand command)@@ -26,11 +28,13 @@ namespace GlazeWM.Domain.Windows.CommandHandlerselse_bus.Invoke(new DetachContainerCommand(window));- // The OS automatically switches focus to a different window after closing. Use `InvokeAsync`- // to ensure focus gets set to `containerToFocus` *after* the OS sets focus. This will cause- // focus to briefly flicker to the OS focus target and then to the WM's focus target.- // TODO: Consider moving this out to `WindowHiddenHandler` and `WindowClosedHandler` after- // redraw. More likely that it runs after OS focus event.+ // The OS automatically switches focus to a different window after closing. If+ // there are focusable windows, then set focus *after* the OS sets focus. This will+ // cause focus to briefly flicker to the OS focus target and then to the WM's focus+ // target.++ _bus.Invoke(new RedrawContainersCommand());+ _containerService.PendingFocusContainer = focusTarget;_bus.InvokeAsync(new SetNativeFocusCommand(focusTarget));return CommandResponse.Ok;diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowDestroyedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowDestroyedHandler.csindex 2c318b51..1ab21123 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowDestroyedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowDestroyedHandler.cs@@ -1,6 +1,5 @@using System.Linq;using GlazeWM.Domain.Common.Utils;-using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Monitors.Commands;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Infrastructure.Bussing;@@ -46,7 +45,6 @@ namespace GlazeWM.Domain.Windows.EventHandlers// If window is in tree, detach the removed window from its parent._bus.Invoke(new UnmanageWindowCommand(window));- _bus.Invoke(new RedrawContainersCommand());}}}diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.csindex de0d90e5..70d1379c 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs@@ -1,7 +1,9 @@using System.Linq;using GlazeWM.Domain.Common.Utils;+using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Containers.Events;+using GlazeWM.Domain.Workspaces;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.Common.Events;using Microsoft.Extensions.Logging;@@ -11,21 +13,56 @@ namespace GlazeWM.Domain.Windows.EventHandlersinternal sealed class WindowFocusedHandler : IEventHandler<WindowFocusedEvent>{private readonly Bus _bus;- private readonly WindowService _windowService;+ private readonly ContainerService _containerService;private readonly ILogger<WindowFocusedHandler> _logger;+ private readonly WindowService _windowService;public WindowFocusedHandler(Bus bus,- WindowService windowService,- ILogger<WindowFocusedHandler> logger)+ ContainerService containerService,+ ILogger<WindowFocusedHandler> logger,+ WindowService windowService){_bus = bus;- _windowService = windowService;+ _containerService = containerService;_logger = logger;+ _windowService = windowService;}public void Handle(WindowFocusedEvent @event){+ var windowHandle = @event.WindowHandle;+ var pendingFocusContainer = _containerService.PendingFocusContainer;++ // Override the container to set focus to (ie. when changing focus after a window is+ // closed or hidden).+ // TODO: This can be simplified a lot. Might also need to handle case where window+ // is null or not displayed.+ if (pendingFocusContainer is not null)+ {+ // If the container gaining focus is the pending focus container, then reset it.+ if (pendingFocusContainer is Window && @event.WindowHandle == (pendingFocusContainer as Window).Handle)+ {+ _bus.Invoke(new SetFocusedDescendantCommand(pendingFocusContainer));+ _bus.Emit(new FocusChangedEvent(pendingFocusContainer));+ _containerService.PendingFocusContainer = null;+ return;+ }++ var isDesktopWindow = windowHandle == _windowService.DesktopWindowHandle;++ if (pendingFocusContainer is Workspace && isDesktopWindow)+ {+ _bus.Invoke(new SetFocusedDescendantCommand(pendingFocusContainer));+ _bus.Emit(new FocusChangedEvent(pendingFocusContainer));+ _containerService.PendingFocusContainer = null;+ return;+ }++ _bus.InvokeAsync(new SetNativeFocusCommand(pendingFocusContainer));+ return;+ }+var window = _windowService.GetWindows().FirstOrDefault(window => window.Handle == @event.WindowHandle);diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.csindex 47c36430..439b3803 100644--- a/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs@@ -1,6 +1,5 @@using System.Linq;using GlazeWM.Domain.Common.Utils;-using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Monitors.Commands;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Infrastructure.Bussing;@@ -49,7 +48,6 @@ namespace GlazeWM.Domain.Windows.EventHandlers// Detach the hidden window from its parent._bus.Invoke(new UnmanageWindowCommand(window));- _bus.Invoke(new RedrawContainersCommand());}}}diff --git a/GlazeWM.Domain/Windows/WindowService.cs b/GlazeWM.Domain/Windows/WindowService.csindex da8b7487..b04ffb4b 100644--- a/GlazeWM.Domain/Windows/WindowService.cs+++ b/GlazeWM.Domain/Windows/WindowService.cs@@ -25,6 +25,11 @@ namespace GlazeWM.Domain.Windows/// </summary>public List<IntPtr> IgnoredHandles { get; set; } = new();+ /// <summary>+ /// Handle to the desktop window.+ /// </summary>+ public IntPtr DesktopWindowHandle { get; } = GetDesktopWindowHandle();+private readonly ContainerService _containerService;private readonly MonitorService _monitorService;@@ -115,6 +120,16 @@ namespace GlazeWM.Domain.Windowsreturn windowHandles;}+ private static IntPtr GetDesktopWindowHandle()+ {+ return GetAllWindowHandles().Find(handle =>+ {+ var className = GetClassNameOfHandle(handle);+ var process = GetProcessOfHandle(handle);+ return className == "Progman" && process.ProcessName == "explorer";+ });+ }+public static WindowStylesEx GetWindowStylesEx(IntPtr handle){return unchecked((WindowStylesEx)GetWindowLongPtr(handle, GWLEXSTYLE).ToInt64());@@ -162,6 +177,16 @@ namespace GlazeWM.Domain.Windowspublic static bool IsHandleManageable(IntPtr handle){+ if (GetProcessOfHandle(handle) is not null)+ {+ var processName = GetProcessOfHandle(handle)?.ProcessName;+ var title = GetTitleOfHandle(handle);++ // TODO: Temporary fix for managing Flow Launcher until a force manage command is added.+ if (processName == "Flow.Launcher" && title == "Flow.Launcher")+ return true;+ }+// Ignore windows that are hidden.if (!IsHandleVisible(handle))return false;