#nullable enable
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 = null;
///
/// Gets the most focused in the application, if there is one.
///
public View? GetFocused () { return _focused; }
///
/// 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);
return;
}
///
/// Raised when the most focused in the application has changed.
///
public event EventHandler? FocusedChanged;
///
/// 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)
{
return true;
}
foreach (View subView in start.Subviews)
{
if (view == subView)
{
return true;
}
var found = IsInHierarchy (subView, view);
if (found)
{
return found;
}
}
return false;
}
///
/// Gets the deepest focused subview of the specified .
///
///
///
internal static View? GetDeepestFocusedSubview (View? view)
{
if (view is null)
{
return null;
}
foreach (View v in view.Subviews)
{
if (v.HasFocus)
{
return GetDeepestFocusedSubview (v);
}
}
return view;
}
///
/// Moves the focus to the next focusable view.
/// Honors and will only move to the next subview
/// if the current and next subviews are not overlapped.
///
internal static void MoveNextView ()
{
View? old = GetDeepestFocusedSubview (Application.Current!.Focused);
if (!Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop))
{
Application.Current.AdvanceFocus (NavigationDirection.Forward, null);
}
if (old != Application.Current.Focused && old != Application.Current.Focused?.Focused)
{
old?.SetNeedsDisplay ();
Application.Current.Focused?.SetNeedsDisplay ();
}
else
{
ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes, NavigationDirection.Forward);
}
}
///
/// Moves the focus to the next subview or the next subview that has
/// set.
///
internal static void MoveNextViewOrTop ()
{
if (ApplicationOverlapped.OverlappedTop is null)
{
Toplevel? top = Application.Current!.Modal ? Application.Current : Application.Top;
if (!Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabGroup))
{
Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
if (Application.Current.Focused is null)
{
Application.Current.RestoreFocus ();
}
}
if (top != Application.Current.Focused && top != Application.Current.Focused?.Focused)
{
top?.SetNeedsDisplay ();
Application.Current.Focused?.SetNeedsDisplay ();
}
else
{
ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes, NavigationDirection.Forward);
}
//top!.AdvanceFocus (NavigationDirection.Forward);
//if (top.Focused is null)
//{
// top.AdvanceFocus (NavigationDirection.Forward);
//}
//top.SetNeedsDisplay ();
ApplicationOverlapped.BringOverlappedTopToFront ();
}
else
{
ApplicationOverlapped.OverlappedMoveNext ();
}
}
// TODO: These methods should return bool to indicate if the focus was moved or not.
///
/// Moves the focus to the next view. Honors and will only move to the next
/// subview
/// if the current and next subviews are not overlapped.
///
internal static void MovePreviousView ()
{
View? old = GetDeepestFocusedSubview (Application.Current!.Focused);
if (!Application.Current.AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop))
{
Application.Current.AdvanceFocus (NavigationDirection.Backward, null);
}
if (old != Application.Current.Focused && old != Application.Current.Focused?.Focused)
{
old?.SetNeedsDisplay ();
Application.Current.Focused?.SetNeedsDisplay ();
}
else
{
ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes?.Reverse (), NavigationDirection.Backward);
}
}
internal static void MovePreviousViewOrTop ()
{
if (ApplicationOverlapped.OverlappedTop is null)
{
Toplevel? top = Application.Current!.Modal ? Application.Current : Application.Top;
top!.AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabGroup);
if (top.Focused is null)
{
top.AdvanceFocus (NavigationDirection.Backward, null);
}
top.SetNeedsDisplay ();
ApplicationOverlapped.BringOverlappedTopToFront ();
}
else
{
ApplicationOverlapped.OverlappedMovePrevious ();
}
}
}