Skip to content

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

feat(domain): add `Container` getter for the last focused descendant excluding a given container

Lars Bergerba2479e0990e
diff --git a/GlazeWM.Domain/Containers/Container.cs b/GlazeWM.Domain/Containers/Container.cs
index e1f84c79..49f30e63 100644
--- a/GlazeWM.Domain/Containers/Container.cs
+++ b/GlazeWM.Domain/Containers/Container.cs
@@ -134,5 +134,31 @@ namespace GlazeWM.Domain.Containers
return null;
}
+
+ /// <summary>
+ /// Get the last focused descendant that is not the given container.
+ /// </summary>
+ public Container LastFocusedDescendantExcluding(Container containerToExclude)
+ {
+ var stack = new Stack<Container>();
+ stack.Push(this);
+
+ // Do a depth-first search using child focus order.
+ while (stack.Any())
+ {
+ var current = stack.Pop();
+
+ var isMatch = current != containerToExclude && !current.HasChildren();
+
+ 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;
+ }
}
}