Rewrote FocusNearestView to be understandable

This commit is contained in:
Tig
2024-07-26 06:32:24 -04:00
parent 66f83ad2e6
commit 911f2c66de
2 changed files with 23 additions and 19 deletions

View File

@@ -32,7 +32,7 @@ internal static class ApplicationNavigation
}
/// <summary>
/// Sets the focus to the next view in the <see cref="View.TabIndexes"/> list. If the last view is focused, the first view is focused.
/// INTERNAL API that sets the focus to the next view in <paramref name="viewsInTabIndexes"/>. If the last view is focused, the first view is focused.
/// </summary>
/// <param name="viewsInTabIndexes"></param>
/// <param name="direction"></param>
@@ -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<View> 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 ();
}
}
}
/// <summary>
/// Moves the focus to the next view. Honors <see cref="ViewArrangement.Overlapped"/> and will only move to the next subview
/// Moves the focus to the next focusable view.
/// Honors <see cref="ViewArrangement.Overlapped"/> and will only move to the next subview
/// if the current and next subviews are not overlapped.
/// </summary>
internal static void MoveNextView ()

View File

@@ -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.
/// <summary>
/// Focuses the first focusable view in <see cref="View.TabIndexes"/> if one exists. If there are no views in
/// <see cref="View.TabIndexes"/> 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;
}