CommunityToolkit / Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
https://learn.microsoft.com/dotnet/communitytoolkit/maui
MIT License
2.27k stars 398 forks source link

[BUG] If the interval between the popup show and the close method is less than 300 ms, the closeAsync() call fails to close the spinner popup on windows #1705

Closed kaushik-sarker closed 3 months ago

kaushik-sarker commented 8 months ago

Is there an existing issue for this?

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

If the interval between the popup show and the close method call is less than 300 ms, the close() call fails to close the spinner popup. Why do I want something like that? I am making a network request in the OnAppearing() method, and I want to display a loading spinner while making the request (please ignore the visual effect on the user). The network request might be resolved very quickly. I noticed that if the interval between Show and Close is very minimal, the popup is not closing, and this happens only inside the OnAppearing overridden method.

Expected Behavior

The popup should be shown and closed properly.

Steps To Reproduce

  1. Open and run the solution.
  2. The popup is displayed but not closing, though I have called the CloseAsync() method.

Please look into the OnAppearing() method inside MainPage.xaml.cs

Link to public reproduction project repository

https://github.com/kaushik-sarker/PopupIssueDemo

Environment

- .NET MAUI CommunityToolkit: 7.0.1
- OS: Windows 10
- .NET MAUI: net8.0

Anything else?

No response

vhugogarcia commented 3 months ago

Thanks @kaushik-sarker for creating this issue, after reviewing this issue in-depth I found the following:

I can confirm that if you try to show up the popup and then close it before 300ms, it won't close. However, the Popup has the following event that is triggered once the UI is fully loaded:

void Spinner_Opened(object? sender, CommunityToolkit.Maui.Core.PopupOpenedEventArgs e)
{
    Console.WriteLine("Popup Openned.");
    ((SpinnerPopup)sender).Close();
    Console.WriteLine("Popup Closed.");
}

On your sample, the popup won't be able to be closed, because the UI has not been initialized for instance it cannot close something that hasn't loaded visually speaking.

If by adding the Task.Delay(300) does not work for your scenario, I have a few recommendations that could work:

I don't think this is an issue of the Popup itself but rather than the native platform itself, and what it takes the time to process the UI and hide it.

I'm closing this issue, however, if none of the recommendations mentioned above work for you, we can discuss it on a discussion topic, and generate ideas to solve it.