Skip to content

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

fix: focus reassignment after window removal (#142)

lars-bergerd756342f8d36
diff --git a/GlazeWM.Domain/Containers/Container.cs b/GlazeWM.Domain/Containers/Container.cs
index d0920aaf..81804569 100644
--- a/GlazeWM.Domain/Containers/Container.cs
+++ b/GlazeWM.Domain/Containers/Container.cs
@@ -110,6 +110,32 @@ namespace GlazeWM.Domain.Containers
}
}
+ /// <summary>
+ /// Leaf nodes (ie. windows and workspaces) in order of last focus.
+ /// </summary>
+ public IEnumerable<Container> DescendantFocusOrder
+ {
+ get
+ {
+ var stack = new Stack<Container>();
+ stack.Push(this);
+
+ // Do a depth-first search using child focus order.
+ while (stack.Count > 0)
+ {
+ var current = stack.Pop();
+
+ // Get containers that have no children. Descendant also cannot be the container itself.
+ if (current != this && !current.HasChildren())
+ yield return current;
+
+ // Reverse the child focus order so that the first element is pushed last/popped first.
+ foreach (var focusChild in current.ChildFocusOrder.AsEnumerable().Reverse())
+ stack.Push(focusChild);
+ }
+ }
+ }
+
public bool IsDetached()
{
return Parent is null;
@@ -181,47 +207,9 @@ namespace GlazeWM.Domain.Containers
/// </summary>
public Container LastFocusedDescendantOfType<T>()
{
- return LastFocusedDescendantWithPredicate(
+ return DescendantFocusOrder.FirstOrDefault(
container => typeof(T).IsAssignableFrom(container.GetType())
);
}
-
- /// <summary>
- /// Get the last focused descendant that is not the given container.
- /// </summary>
- public Container LastFocusedDescendantExcluding(Container containerToExclude)
- {
- return LastFocusedDescendantWithPredicate(
- container => container != containerToExclude
- );
- }
-
- /// <summary>
- /// Get the last focused descendant that matches a given predicate.
- /// </summary>
- private Container LastFocusedDescendantWithPredicate(Predicate<Container> predicate)
- {
- var stack = new Stack<Container>();
- stack.Push(this);
-
- // Do a depth-first search using child focus order.
- while (stack.Count > 0)
- {
- var current = stack.Pop();
-
- // The focused descendant cannot be the container itself or any containers that have
- // children.
- var isMatch = current != this && !current.HasChildren() && predicate(current);
-
- if (isMatch)
- return current;
-
- // Reverse the child focus order so that the first element is pushed last/popped first.
- foreach (var focusChild in current.ChildFocusOrder.AsEnumerable().Reverse())
- stack.Push(focusChild);
- }
-
- return null;
- }
}
}
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/UnmanageWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/UnmanageWindowHandler.cs
index c19731ae..d7212525 100644
--- a/GlazeWM.Domain/Windows/CommandHandlers/UnmanageWindowHandler.cs
+++ b/GlazeWM.Domain/Windows/CommandHandlers/UnmanageWindowHandler.cs
@@ -18,9 +18,8 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
{
var window = command.Window;
- // Keep references to the window's parent and grandparent prior to detaching.
- var parent = window.Parent;
- var grandparent = parent.Parent;
+ // Get container to switch focus to after the window has been removed.
+ var focusTarget = WindowService.GetFocusTargetAfterRemoval(window);
if (window is IResizable)
_bus.Invoke(new DetachAndResizeContainerCommand(window));
@@ -30,11 +29,9 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
// 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: Container to focus should depend on focus mode.
// TODO: Consider moving this out to `WindowHiddenHandler` and `WindowClosedHandler` after
// redraw. More likely that it runs after OS focus event.
- var containerToFocus = parent.LastFocusedDescendant ?? grandparent.LastFocusedDescendant;
- _bus.InvokeAsync(new SetNativeFocusCommand(containerToFocus));
+ _bus.InvokeAsync(new SetNativeFocusCommand(focusTarget));
return CommandResponse.Ok;
}
diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs
index 7b4cb306..2994e75c 100644
--- a/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs
+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowMinimizedHandler.cs
@@ -52,10 +52,12 @@ namespace GlazeWM.Domain.Windows.EventHandlers
previousState
);
+ // Get container to switch focus to after the window has been minimized.
+ var focusTarget = WindowService.GetFocusTargetAfterRemoval(window);
+
_bus.Invoke(new ReplaceContainerCommand(minimizedWindow, window.Parent, window.Index));
- // TODO: Container to focus should depend on focus mode.
- var focusTarget = workspace.LastFocusedDescendantExcluding(minimizedWindow) ?? workspace;
+ // Reassign focus to appropriate container.
_bus.InvokeAsync(new SetNativeFocusCommand(focusTarget));
_containerService.ContainersToRedraw.Add(workspace);
diff --git a/GlazeWM.Domain/Windows/WindowService.cs b/GlazeWM.Domain/Windows/WindowService.cs
index 34c77f94..9c347097 100644
--- a/GlazeWM.Domain/Windows/WindowService.cs
+++ b/GlazeWM.Domain/Windows/WindowService.cs
@@ -6,6 +6,7 @@ using System.Runtime.InteropServices;
using System.Text;
using GlazeWM.Domain.Containers;
using GlazeWM.Domain.Monitors;
+using GlazeWM.Domain.Workspaces;
using GlazeWM.Infrastructure.WindowsApi;
using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;
@@ -217,5 +218,37 @@ namespace GlazeWM.Domain.Windows
_ => throw new ArgumentException(null, nameof(window)),
};
}
+
+ /// <summary>
+ /// Get container to focus after the given window is unmanaged, minimized, or moved to another
+ /// workspace.
+ /// </summary>
+ public static Container GetFocusTargetAfterRemoval(Window removedWindow)
+ {
+ var parentWorkspace = WorkspaceService.GetWorkspaceFromChildContainer(removedWindow);
+
+ // Get descendant focus order excluding the removed container.
+ var descendantFocusOrder = parentWorkspace.DescendantFocusOrder.Where(
+ descendant => descendant != removedWindow
+ );
+
+ // Get focus target that matches the removed window type (only for tiling/floating windows).
+ var focusTargetOfType = descendantFocusOrder.FirstOrDefault(
+ (descendant) =>
+ (removedWindow is FloatingWindow && descendant is FloatingWindow) ||
+ (removedWindow is TilingWindow && descendant is TilingWindow)
+ );
+
+ if (focusTargetOfType is not null)
+ return focusTargetOfType;
+
+ var nonMinimizedFocusTarget = descendantFocusOrder.FirstOrDefault(
+ (descendant) => descendant is not MinimizedWindow
+ );
+
+ return nonMinimizedFocusTarget ??
+ descendantFocusOrder.FirstOrDefault() ??
+ parentWorkspace;
+ }
}
}
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs
index 9347831c..0451394e 100644
--- a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs
+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs
@@ -39,8 +39,7 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
var workspaceToFocus = _workspaceService.GetActiveWorkspaceByName(workspaceName)
?? ActivateWorkspace(workspaceName);
- // Get the currently focused workspace. This can be null if there currently
- // isn't a container that has focus.
+ // Get the currently focused workspace.
var focusedWorkspace = _workspaceService.GetFocusedWorkspace();
if (focusedWorkspace == workspaceToFocus)
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.cs
index 7ffe3c74..8f51d1e5 100644
--- a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.cs
+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.cs
@@ -39,6 +39,9 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
var targetWorkspace = _workspaceService.GetActiveWorkspaceByName(workspaceName)
?? ActivateWorkspace(workspaceName, windowToMove);
+ if (currentWorkspace == targetWorkspace)
+ return CommandResponse.Ok;
+
// Since target workspace could be on a different monitor, adjustments might need to be made
// because of DPI.
if (MonitorService.HasDpiDifference(currentWorkspace, targetWorkspace))
@@ -49,13 +52,19 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
windowToMove.FloatingPlacement =
windowToMove.FloatingPlacement.TranslateToCenter(targetWorkspace.ToRect());
+ var focusTarget = WindowService.GetFocusTargetAfterRemoval(windowToMove);
+
if (windowToMove is TilingWindow)
MoveTilingWindowToWorkspace(windowToMove as TilingWindow, targetWorkspace);
else
_bus.Invoke(new MoveContainerWithinTreeCommand(windowToMove, targetWorkspace, false));
- // Reassign focus to descendant within the current workspace.
- ReassignFocusWithinWorkspace(currentWorkspace);
+ // Reassign focus to descendant within the current workspace. Need to call
+ // `SetFocusedDescendantCommand` for when commands like `FocusWorkspaceCommand` are called
+ // immediately afterwards and they should behave as if `focusTarget` is the focused
+ // descendant.
+ _bus.Invoke(new SetFocusedDescendantCommand(focusTarget));
+ _bus.Invoke(new SetNativeFocusCommand(focusTarget));
_containerService.ContainersToRedraw.Add(currentWorkspace);
_containerService.ContainersToRedraw.Add(windowToMove);
@@ -97,16 +106,5 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
)
);
}
-
- private void ReassignFocusWithinWorkspace(Workspace workspace)
- {
- var containerToFocus = workspace.LastFocusedDescendant ?? workspace;
-
- // 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));
- }
}
}