Skip to content

Commit

Permalink
Generalize HideSoftInputOnTapped on Android and iOS to support 3rd pa…
Browse files Browse the repository at this point in the history
…rty input controls.
  • Loading branch information
albyrock87 committed Dec 29, 2023
1 parent 3944004 commit 40ec49b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Android.Widget;
using Microsoft.Maui.Graphics;
using AView = Android.Views.View;
using AViewGroup = Android.Views.ViewGroup;

namespace Microsoft.Maui.Controls
{
Expand Down Expand Up @@ -43,21 +44,12 @@ page.Handler is IPlatformViewHandler pvh &&
// This is called from InputViews as they are added to the visual tree
IDisposable? SetupHideSoftInputOnTapped(AView aView)
{
if (aView is SearchView sv &&
sv.GetFirstChildOfType<EditText>() is EditText editText)
if (aView is AViewGroup vg &&
vg.GetFirstChildOfType<EditText>() is {} editText)
{
aView = editText;
}

if (aView is AndroidX.AppCompat.Widget.SearchView svX &&
svX.GetFirstChildOfType<EditText>() is EditText editTextX)
{
aView = editTextX;
}

if (aView is null)
return null;

if (!FeatureEnabled)
return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@ namespace Microsoft.Maui.Controls
{
partial class HideSoftInputOnTappedChangedManager
{
internal IDisposable? SetupHideSoftInputOnTapped(UIView uIView)
internal IDisposable? SetupHideSoftInputOnTapped(UIView uiView)
{
if (!FeatureEnabled || uIView.Window is null)
if (!FeatureEnabled || uiView.Window is null)
return null;

if (uIView is UISearchBar searchBar &&
searchBar.GetSearchTextField() is UIView textField)
{
uIView = textField;
}
var firstResponder = uiView.FindFirstResponder(v => v is UITextField or UITextView);

if (uIView is null)
if (firstResponder is null)
return null;

return ResignFirstResponderTouchGestureRecognizer
.Update(uIView);
.Update(firstResponder);
}
}
}
4 changes: 2 additions & 2 deletions src/Core/src/Platform/iOS/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -889,12 +889,12 @@ internal static void ChangeFocusedView(this UIView view, UIView? newView)
return null;
}

internal static UIView? FindFirstResponder(this UIView? superview)
internal static UIView? FindFirstResponder(this UIView? superview, Func<UIView, bool>? predicate = null)
{
if (superview is null)
return null;

if (superview.IsFirstResponder)
if (superview.IsFirstResponder && (predicate?.Invoke(superview) ?? true))
return superview;

foreach (var subview in superview.Subviews)
Expand Down

0 comments on commit 40ec49b

Please sign in to comment.