question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] Add `Popup.CloseAsync()`

See original GitHub issue

Feature name

Add Popup.CloseAsync()

Link to discussion

Discussed in June 2023 Standup:

https://github.com/CommunityToolkit/Maui/wiki/2023-June-Standup

Progress tracker

Summary

This Proposal adds the following API to Popup:

public async Task CloseAsync(object? result = null);

CloseAsync() returns once the operating system has dismissed Popup from the page.

Motivation

Currently, Popup only offers one method to programmatically dismiss it from the screen: public void Close();.

However, on MacCatalyst and iOS, the code required to dismiss the Popup uses async/await to dismiss the UIViewController:

https://github.com/CommunityToolkit/Maui/blob/e89e7dabd2c348601f75dc68867f6cf6486d8595/src/CommunityToolkit.Maui.Core/Handlers/Popup/PopupHandler.macios.cs#L14-L23

The existing Close() API is thus acting in a fire-and-forget manner; the method is returning to the caller before the Popup has been dismissed on iOS + MacCatalyst.

Detailed Design

IPopup.shared.cs

This API update requires a TaskCompletionSource in IPopup that can be referenced by both the Handler and the Control.

This is required because the PropertyMappers / CommandMappers that .NET MAUI use for Handlers are not asynchronous (they cannot be awaitd).

We will instead tell the Control to await PopupDismissedTaskCompletionSource.Task after IPopup.Closed() has been called. Then, in PopupHandler.MapOnClosed, we will call PopupDismissedTaskCompletionSource TrySetResult() after the operating system has dismissed the Popup from the page.

public interface IPopup : IElement, IVisualTreeElement
{
    // ...
    // Existing APIs
    //..

    /// <summary>
    /// <see cref="TaskCompletionSource"/> that completes when the operating system has dismissed <see cref="IPopup"/> from the screen
    /// </summary>
    TaskCompletionSource PopupDismissedTaskCompletionSource { get; }
}

Popup.shared.cs

This Proposal also requires a new API public Task Popup.CloseAsync(object? result = null);

/// <summary>
/// Close the current popup.
/// </summary>
/// <remarks>
/// Returns once the operating system has dismissed the <see cref="IPopup"/> from the page
/// </remarks>
/// <param name="result">
/// The result to return.
/// </param>
public async Task CloseAsync(object? result = null)
{
  await OnClosed(result, false);
  taskCompletionSource.TrySetResult(result);
}

Usage Syntax

async void Button_Clicked(object? sender, EventArgs e)
{
    await CloseAsync();
    await Toast.Make("Popup Dismissed By Button").Show();
}

Drawbacks

Only iOS + MacCatalyst defer to a different thread when dismissing the Popup; Android and Windows can dismiss the Popup synchronously. Adding a method that returns Task adds a bit of overhead to our users, however, the overhead should be negligible as users typically only display, and subsequently close, one Popup at a time.

Alternatives

This Proposal can be updated to remove the fire-and-forget method, void Close().

However, removing an existing API is a breaking change.

Unresolved Questions

Should we instead return ValueTask?

public async ValueTask CloseAsync(object? result = null)

Issue Analytics

  • State:closed
  • Created 4 months ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
brminnickcommented, Jun 12, 2023

Does it solve #1111 ?

Yup! Thanks, I’ve added linked it to the PR.

1reaction
bijingtoncommented, Jun 8, 2023

I’m happy to approve this also

Read more comments on GitHub >

github_iconTop Results From Across the Web

New Feature Proposals
[Proposal] Consider adding Fluent API / Markup style methods for the types defined in the Toolkit similar to that of existing Markup package...
Read more >
Popup - .NET MAUI Community Toolkit
The easiest way to create a Popup is to add a new . ... operating system has dismissed the Popup from the screen,...
Read more >
How do I completely disable "proposals" popups for ...
I've tried disabling all "hovers" and unchecking everything on the Java Proposals page in the Eclipse preferences dialog, but that's had no ...
Read more >
Learn How to Create interactive Pop-ups - YouTube
Convert any object to a button and trigger popups on rollover or click without a single line of code. This tutorial shows you...
Read more >
The Pop-Up Proposal | Unique Proposal Idea
The perfect engagement proposal idea starts and ends here. ... We do not provide provide food, only Charcuterie boxes or an add on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found