diff --git a/Terminal.Gui/Application/Application.Navigation.cs b/Terminal.Gui/Application/Application.Navigation.cs
index 4d85c2ba1..5922151fe 100644
--- a/Terminal.Gui/Application/Application.Navigation.cs
+++ b/Terminal.Gui/Application/Application.Navigation.cs
@@ -32,7 +32,7 @@ internal static class ApplicationNavigation
}
///
- /// Sets the focus to the next view in the list. If the last view is focused, the first view is focused.
+ /// INTERNAL API that sets the focus to the next view in . If the last view is focused, the first view is focused.
///
///
///
@@ -43,39 +43,41 @@ internal static class ApplicationNavigation
return;
}
- var found = false;
- var focusProcessed = false;
- var idx = 0;
+ bool foundCurrentView = false;
+ bool focusSet = false;
+ IEnumerable indexes = viewsInTabIndexes as View [] ?? viewsInTabIndexes.ToArray ();
+ int viewCount = indexes.Count ();
+ int currentIndex = 0;
- foreach (View v in viewsInTabIndexes)
+ foreach (View view in indexes)
{
- if (v == Application.Current)
+ if (view == Application.Current)
{
- found = true;
+ foundCurrentView = true;
}
-
- if (found && v != Application.Current)
+ else if (foundCurrentView && !focusSet)
{
Application.Current!.SuperView?.AdvanceFocus (direction);
+ focusSet = true;
- focusProcessed = true;
-
- if (Application.Current.SuperView?.Focused is { } && Application.Current.SuperView.Focused != Application.Current)
+ if (Application.Current.SuperView?.Focused != Application.Current)
{
return;
}
}
- else if (found && !focusProcessed && idx == viewsInTabIndexes.Count () - 1)
- {
- viewsInTabIndexes.ToList () [0].SetFocus ();
- }
- idx++;
+ currentIndex++;
+
+ if (foundCurrentView && !focusSet && currentIndex == viewCount)
+ {
+ indexes.First ().SetFocus ();
+ }
}
}
///
- /// Moves the focus to the next view. Honors and will only move to the next subview
+ /// 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 ()
diff --git a/Terminal.Gui/View/View.Navigation.cs b/Terminal.Gui/View/View.Navigation.cs
index 5df5576a3..81fdd0cc8 100644
--- a/Terminal.Gui/View/View.Navigation.cs
+++ b/Terminal.Gui/View/View.Navigation.cs
@@ -444,7 +444,6 @@ public partial class View // Focus and cross-view navigation management (TabStop
}
}
- // TODO: Combine FocusFirst and FocusLast into a single method that takes a direction parameter for less code duplication and easier testing.
///
/// Focuses the first focusable view in if one exists. If there are no views in
/// then the focus is set to the view itself.
@@ -622,7 +621,10 @@ public partial class View // Focus and cross-view navigation management (TabStop
if (Focused is { })
{
+ // Leave
Focused.SetHasFocus (false, this);
+
+ // Signal that nothing is focused, and callers should try a peer-subview
Focused = null;
}