Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Better Find Next Textfield Algo #13174

Merged
merged 2 commits into from
Feb 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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