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

Don't run hide on tapped code if window is null #17758

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ void OnPageNavigatedFrom(object? sender, NavigatedFromEventArgs e)
return null;
}

if (ve.Window is null)
{
// This means the xplat IsFocused value has lagged behind navigation events.
// This might happen if navigated has fired on the incoming page but the
// "LostFocus" event hasn't propagated from the previous one
return null;
}

IDisposable? platformToken = SetupHideSoftInputOnTapped(platformView);

#if ANDROID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public partial class ControlsHandlerTestBase : HandlerTestBase, IDisposable
protected override MauiAppBuilder ConfigureBuilder(MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddSingleton<IApplication>((_) => new ApplicationStub());
mauiAppBuilder.Services.AddScoped(_ => new HideSoftInputOnTappedChangedManager());
return mauiAppBuilder.ConfigureTestBuilder();
}

Expand Down
44 changes: 44 additions & 0 deletions src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void SetupBuilder()
SetupShellHandlers(handlers);
handlers.AddHandler(typeof(NavigationPage), typeof(NavigationViewHandler));
handlers.AddHandler(typeof(Button), typeof(ButtonHandler));
handlers.AddHandler(typeof(Entry), typeof(EntryHandler));
handlers.AddHandler(typeof(Controls.ContentView), typeof(ContentViewHandler));
handlers.AddHandler(typeof(ScrollView), typeof(ScrollViewHandler));
handlers.AddHandler(typeof(CollectionView), typeof(CollectionViewHandler));
Expand Down Expand Up @@ -1001,6 +1002,49 @@ public async Task PagesDoNotLeak()
Assert.False(pageReference.IsAlive, "Page should not be alive!");
}

[Fact(DisplayName = "HideSoftInputOnTapped Doesnt Crash If Entry Is Still Focused After Window Is Null")]
public async Task HideSoftInputOnTappedDoesntCrashIfEntryIsStillFocusedAfterWindowIsNull()
{
SetupBuilder();

Entry entry1;
Entry entry2;

var rootPage = CreatePage(out entry1);
var nextPage = CreatePage(out entry2);

var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = rootPage;
});

await CreateHandlerAndAddToWindow<IWindowHandler>(shell, async (handler) =>
{
entry1.Focus();
await entry1.WaitForFocused();

await rootPage.Navigation.PushAsync(nextPage);
entry2.Focus();
await entry2.WaitForFocused();

await shell.GoToAsync("..", false);
});

ContentPage CreatePage(out Entry entry)
{
entry = new Entry();

return new ContentPage()
{
HideSoftInputOnTapped = true,
Content = new VerticalStackLayout()
{
entry
}
};
}
}

[Fact(DisplayName = "Can Reuse Pages")]
public async Task CanReusePages()
{
Expand Down