#nullable enable using System.Diagnostics; namespace Terminal.Gui; /// /// Helper class for navigation. Held by /// public class ApplicationNavigation { /// /// Initializes a new instance of the class. /// public ApplicationNavigation () { // TODO: Move navigation key bindings here from AddApplicationKeyBindings } private View? _focused; /// /// Raised when the most focused in the application has changed. /// public event EventHandler? FocusedChanged; /// /// Gets the most focused in the application, if there is one. /// public View? GetFocused () { return _focused; } /// /// Gets whether is in the Subview hierarchy of . /// /// /// /// public static bool IsInHierarchy (View? start, View? view) { if (view is null) { return false; } if (view == start || start is null) { return true; } foreach (View subView in start.Subviews) { if (view == subView) { return true; } bool found = IsInHierarchy (subView, view); if (found) { return found; } } return false; } /// /// INTERNAL method to record the most focused in the application. /// /// /// Raises . /// internal void SetFocused (View? value) { if (_focused == value) { return; } _focused = value; FocusedChanged?.Invoke (null, EventArgs.Empty); } /// /// Advances the focus to the next or previous view in the focus chain, based on /// . /// /// /// /// If there is no next/previous view, the focus remains on the current view. /// /// /// The direction to advance. /// The tab behavior. /// /// if focus was changed to another subview (or stayed on this one), /// otherwise. /// public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior) { return Application.Top is { } && Application.Top.AdvanceFocus (direction, behavior); } }