Skip to content

Commit

Permalink
[iOS] Better Find Next Textfield Algo (dotnet#13174)
Browse files Browse the repository at this point in the history
* Use the better algorithm

* prepare for nulls and index less

---------

Co-authored-by: TJ Lambert <tjlambert@microsoft.com>
  • Loading branch information
tj-devel709 and TJ Lambert committed Feb 21, 2023
1 parent e494dda commit 2d2b971
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/Core/src/Platform/iOS/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -735,40 +735,49 @@ internal static void UpdateLayerBorder(this CoreAnimation.CALayer layer, IButton
return null;
}

internal static UIView? FindNextView(this UIView view, UIView superView, Func<UIView, bool> isValidType)
internal static UIView? FindNextView(this UIView? view, UIView containerView, Func<UIView, bool> isValidType)
{
var passedOriginal = false;
UIView? nextView = null;

var nextView = superView.FindNextView(view, ref passedOriginal, isValidType);
while (view is not null && view != containerView && nextView is null)
{
var siblings = view.Superview?.Subviews;

if (siblings is null)
break;

nextView = view.FindNextView(siblings.IndexOf(view) + 1, isValidType);

view = view.Superview;
}

// if we did not find the next view, try to find the first one
nextView ??= superView.FindNextView(null, ref passedOriginal, isValidType);
nextView ??= containerView.Subviews?[0]?.FindNextView(0, isValidType);

return nextView;
}

static UIView? FindNextView(this UIView view, UIView? origView, ref bool passedOriginal, Func<UIView, bool> isValidType)
static UIView? FindNextView(this UIView? view, int index, Func<UIView, bool> isValidType)
{
foreach (var child in view.Subviews)
{
if (isValidType(child))
{
if (origView is null)
return child;
// search through the view's siblings and traverse down their branches
var siblings = view?.Superview?.Subviews;

if (passedOriginal)
return child;
if (siblings is null)
return null;

if (child == origView)
passedOriginal = true;
}
for (int i = index; i < siblings.Length; i++)
{
var sibling = siblings[i];

else if (child.Subviews.Length > 0 && !child.Hidden && child.Alpha > 0f)
if (sibling.Subviews is not null && sibling.Subviews.Length > 0)
{
var nextLevel = child.FindNextView(origView, ref passedOriginal, isValidType);
if (nextLevel is not null)
return nextLevel;
var childVal = sibling.Subviews[0].FindNextView(0, isValidType);
if (childVal is not null)
return childVal;
}

if (isValidType(sibling))
return sibling;
}

return null;
Expand Down

0 comments on commit 2d2b971

Please sign in to comment.