Skip to content

Add PopupExtensions.ClosePopup() #2671

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

Conversation

TheCodeTraveler
Copy link
Collaborator

@TheCodeTraveler TheCodeTraveler commented May 25, 2025

Description of Change

This PR adds two additional extension methods to PopupExtensions.shared.cs:

public static Task ClosePopup(this INavigation, CancellationToken);
public static Task ClosePopup(this Page, CancellationToken);

After reading through @bijington's docs (amazing job, by the way!) , I realized that we don't give developers a way to close a Popup when they pass in a View (not passing in a Popup):

var label = new Label { Text = "Hello World" };
await this.ShowPopupAsync(label);
// How do I close the popup that `CommunityToolkit.Maui` auto-generated for the developer under the hood? 

With this PR, developers can now use PopupExtensions.Close():

var label = new Label { Text = "Hello World" };
await this.ShowPopupAsync(label);

await Task.Delay(TimeSpan.FromSeconds(2));

await this.ClosePopup();

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard

Additional information

This PR is targeting the #1581 PR. In other words, when this PR is merged, it will be merged into #1581, not the main branch.

@TheCodeTraveler TheCodeTraveler requested a review from bijington May 25, 2025 22:11
@TheCodeTraveler TheCodeTraveler changed the title Add PopupExtensions.Close() Add PopupExtensions.ClosePopup() May 25, 2025
This ensures we do not forget to remove the Obsolete Popup classes in our .NET 10 release
Copy link
Contributor

@bijington bijington left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! The only possible extra question I have is: should we add an example to the sample app that covers this functionality? Or was it already there and we just missed it during testing?

@TheCodeTraveler
Copy link
Collaborator Author

should we add an example to the sample app that covers this functionality

Good call! Done.

@TheCodeTraveler TheCodeTraveler merged commit 6915726 into feature/sl/remove-popup-constraint-from-popup-service May 26, 2025
2 checks passed
@TheCodeTraveler TheCodeTraveler deleted the Add-PopupExtensions.ClosePopup() branch May 26, 2025 19:59
TheCodeTraveler added a commit that referenced this pull request May 26, 2025
* Initial musings on showing popups without having to create your own popup

* Remove commented code

* Pass on the BindingContext of a view up to the popup

* Expand IPopupService API to enable the ability to supply the configurable properties

The aim is to allow us to hide the Popup away from developers, or at least simply the usage of showing a popup to not needing to create a Popup implementation

* Name the cancellation token usage

* Include DynamicallyAccessedMembers

* Popup V2

* Update samples

* Fix tests

* register all popups

* fix tizen build

* await tasks

* fix xaml

* Fix tests

* Fix popup Result

* Fix tests, fix samples, rework popup service

* fix sample

* Fix back button

* Fix tests

* Add Navigation parameter

* Fix after rebase

* Remove old page

* Add docs

* Use Partial Property

* Disable `PushModalAsync` and `PopModalAsync` Animations

* Remove Circular Reference to PopupContainer

* Remove Null Forgiving Operator

* Update Event Name

* Add CancellationToken Parameter

* Remove duplicate `()`

* Add PopupExtension method for `Page`

* Remove `return await`

* Update formatting

* Use `WeakEventManager`

* Remvoe Duplicate `SemanticOrderViewPageViewModel` Mapping

* Use Bindings to ensure `Popup.BindingContext` and `PopupContainer.BindingContext` always matches Popup.BindingContext

* Remove `PopupLifecycleController`, Organize Folder Structure

* Remove `BuildPopupContent()`, `BuildPopupContent` and `ConfigurePopupContainer ` from `PopupExtensions

* Use correct `ShowPopup` method

* Make PopupOptions Bindable, Fix Popup Samples, Add `PopupService.ShowPopup`

* Propogate BindableProperties

* `dotnet format`

* Fix binding

* Apply suggestions from code review

Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com>

* Create `IPopupOptions`, Add `PopupOptions.Empty`, Fix File Naming, Namespaces + Folder Structure

* Add `PopupOptionsDefaults`

* Fix file names

* `dotnet format`

* Move to `CommunityToolkit.Maui`

* Remove `ConvertToLayoutOptions`

* Add PopupContainer Tests

* Add PopupServiceTests

* Add PopupTests

* Remove `PopupAnchor`

* Make `PopupOptions` properties readonly on `Popup`

* Mark `PopupOptions` `BindableProperty`s  as `[Obsolete]`

* Update XML Comments, Add `Background`

* Update XML Comments

* Add XC0618 to `WarningsAsErrors`

* Add Unit Tests

* Remove duplicate GetPopupContainer()

* Update PopupServiceTests.cs

* Update Popup Tests

* Add Popup Tests

* Fix Popup_Close_ShouldNotThrowExceptionWhenCloseIsOverridden

* Update Popup Tests

* Update Popup.shared.cs

* Update Popup.shared.cs

* Add CancellationToken to PopupT.Close()

* Add PopupExtensionsTests

* Fix namespace

* Add `PopupOptionsTests`

* Add `IPopupOptionsTests`

* Format + Remove unused code

* Add `PopupContainer Tests`

* Update Unit Tests

* Rename to `bindablePopupOptions`

* Add PopupOptions.BorderStroke

* Rename `PopupOptions.BackgroundColor` -> `PopupOptions.PageOverlayColor`

* Refactor Popup BackgroundColor

* Fix Default Padding and Background Bindings

* Remove duplicate BindingContext Binding

* Add ShowPopupAsync_ShouldReturnResult_WhenPopupIsClosedByTappingOutsidePopup

* Remove duplicate code

* Update PopupResult.shared.cs

* Update PopupResult.shared.cs

* Ensure PopupResult<T> generic type is always nullable

* Refactor `IsNullable()`

* Implement `IPopupResult`

* Update Popup Unit Tests

* Remove XAML from CSharpBindingPopup

* Add PopupOptionsDefaults.PopupBackgroundColor

* Set default Shape + BorderColor

* Add IPopupOptions.Shadow

* Add Shadow, Update Unit Tests

* Add `ArgumentNullException.ThrowIfNull(view);`

* Update Sample Popups

* Ensure PopupContainer.Close closes the correct popup

* Add `popupContainersInModalStackList.Count` check

* Refactor Close

* Add IAsyncDisposable

* Fix failing PopupTests

* Fix PopupSizingIssuesPage

* Update Popup Styling

* Use `PopupOptions.Shape` for BorderStroke and StrokeThickness

* `dotnet format`

* Rename `PopupContainer` -> `PopupPage`

* Update ServiceCollectionExtensions.shared.cs

* Remove `CommunityToolkit.Maui.Extensions.NullableExtensions`

* Rename to `PopupPage`

* Update Sample Popups

* Fix Default `StrokeThickness`

* Refactor `DisplayPopup`

* Add `string? shellRoute`

* Add `string? shellRoute` parameter

* Add `PopupOnDisappearingPage`

* Refactor OnDisappearingPopupPage

* Update PopupOnDisappearingPage.cs

* Remove default parameters from `internal` methods

* Flow Popup Properties through to PopupPageLayout.Content

* Update src/CommunityToolkit.Maui/Extensions/PopupExtensions.shared.cs

Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com>

* Unsubscribe event handler

* Fix XML Comments

* Add `ThrowIfCancellationRequested()`

* Update XML Docs, Create InvalidPopupOperationException, Update Unit Tests

* Add `AddTransientPopup()`, `AddSingletonPopup()` and `AddScopedPopup()`

* Add Unit Tests

* Add Unit Tests

* Update Sample App Popups

* Use Colors.LightGray for Default BorderStroke

* Reduce Sample App Popup VerticalStackLayout Spacing to `6`

* Reduce `BoxView` Margin

* Fix Sample App Build Errors

* Make default constructor internal

* Add Support for Shell + `IQueryAttributable` for PopupV2 (#2661)

* Re-add Handlers with `[Obsolete]`

* Remove `Microsoft.NET.Test.Sdk`

* `dotnet format`

* Update PopupResultException

* Update NuGet Packages

* Fix formatting

* Use `PopupDefaults.Padding` when Padding is not set

* Add `HorizontalOptionsConverter` and `VerticalOptionsConverter` to ensure PopupDefaults are set

* Remove `LayoutOptions.Fill` from `PopupLayoutAlignmentPage`

* Add `ShowPopupAsync_WithViewType_SetsCorrectDefaults`

* Add .NET 10 Compiler Error

This ensures we do not forget to remove the Obsolete Popup classes in our .NET 10 release

* Add `[EditorBrowsable(EditorBrowsableState.Never)]` to Obsolete classes

* Add `PopupExtensions.ClosePopup()` (#2671)

* Add `Close()`

* Update PopupExtensions.shared.cs

* Add CancellationToken

* Add Unit Tests

* Add Support for Shell

* Add .NET 10 Compiler Error

This ensures we do not forget to remove the Obsolete Popup classes in our .NET 10 release

* Rename `Close()` -> `CloseAsync()`

* Add `[EditorBrowsable(EditorBrowsableState.Never)]` to Obsolete classes

* Update Samples

* Fix XML

* Use async/await

* Rename to `CloseAsync`

* Bump `TOOLKIT_NET_VERSION` to 9.0.300

* Update global.json

* Add Page Overloads to IPopupService

---------

Co-authored-by: Shaun Lawrence <17139988+bijington@users.noreply.github.com>
Co-authored-by: Vladislav Antonyuk <vladislav.antonyuk@dataart.com>
Co-authored-by: Vladislav Antonyuk <vlad.antonyuk@gmail.com>
Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com>
Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com>
Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com>
Co-authored-by: James Crutchley <ne0rmatrix@gmail.com>
@github-actions github-actions bot locked and limited conversation to collaborators May 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants