Skip to content

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

feat(domain): add `Container` getter for getting the last focused descendant of a given type

Lars Bergerc926b3a1e028
diff --git a/GlazeWM.Domain/Containers/Container.cs b/GlazeWM.Domain/Containers/Container.cs
index 7a154b8b..9cce0ff9 100644
--- a/GlazeWM.Domain/Containers/Container.cs
+++ b/GlazeWM.Domain/Containers/Container.cs
@@ -146,5 +146,26 @@ namespace GlazeWM.Domain.Containers
.Reverse()
.FirstOrDefault(container => type.IsAssignableFrom(container.GetType()));
}
+
+ /// <summary>
+ /// Get the last focused descendant that matches the given type.
+ /// </summary>
+ public Container LastFocusedDescendantOfType(Type type)
+ {
+ // Create variable for `LastFocusedDescendant` to avoid calling getter multiple times.
+ var lastFocusedDescendant = LastFocusedDescendant;
+
+ if (type.IsAssignableFrom(lastFocusedDescendant.GetType()))
+ return lastFocusedDescendant;
+
+ var descendantFromFocusOrder = lastFocusedDescendant.Parent.ChildFocusOrder
+ .FirstOrDefault(container => type.IsAssignableFrom(container.GetType()));
+
+ if (descendantFromFocusOrder != null)
+ return descendantFromFocusOrder;
+
+ return lastFocusedDescendant.Siblings
+ .FirstOrDefault(container => type.IsAssignableFrom(container.GetType()));
+ }
}
}