GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat(domain): change `LastFocusedDescendantOfType` to a depth-first search for matching container
Lars Bergere4ae18e69dcf
diff --git a/GlazeWM.Domain/Containers/Container.cs b/GlazeWM.Domain/Containers/Container.csindex 9cce0ff9..e8f6c4ec 100644--- a/GlazeWM.Domain/Containers/Container.cs+++ b/GlazeWM.Domain/Containers/Container.cs@@ -72,6 +72,11 @@ namespace GlazeWM.Domain.Containers/// </summary>public Container NextSibling => SelfAndSiblings.ElementAtOrDefault(Index + 1);+ /// <summary>+ /// The sibling at the previous index to this container.+ /// </summary>+ public bool IsDescendable => false;+/// <summary>/// The sibling at the previous index to this container./// </summary>@@ -152,20 +157,24 @@ namespace GlazeWM.Domain.Containers/// </summary>public Container LastFocusedDescendantOfType(Type type){- // Create variable for `LastFocusedDescendant` to avoid calling getter multiple times.- var lastFocusedDescendant = LastFocusedDescendant;+ var stack = new Stack<Container>();+ stack.Push(this);- if (type.IsAssignableFrom(lastFocusedDescendant.GetType()))- return lastFocusedDescendant;+ // Depth-first search using `ChildFocusOrder` list.+ while (stack.Any())+ {+ var current = stack.Pop();- var descendantFromFocusOrder = lastFocusedDescendant.Parent.ChildFocusOrder- .FirstOrDefault(container => type.IsAssignableFrom(container.GetType()));+ var isMatch = type.IsAssignableFrom(current.GetType()) && !current.HasChildren();- if (descendantFromFocusOrder != null)- return descendantFromFocusOrder;+ if (isMatch)+ return current;- return lastFocusedDescendant.Siblings- .FirstOrDefault(container => type.IsAssignableFrom(container.GetType()));+ foreach (var focusChild in current.ChildFocusOrder)+ stack.Push(focusChild);+ }++ return null;}}}