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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns:viewModels="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Views"
x:TypeArguments="viewModels:PopupLayoutAlignmentViewModel"
x:DataType="viewModels:PopupLayoutAlignmentViewModel"
Padding="20"
Title="Popup Layout Alignment">

<Grid RowDefinitions="64,44,32,44,24,44,*"
Expand Down Expand Up @@ -41,7 +42,6 @@

<Entry Grid.Row="1"
Grid.Column="5"
Grid.ColumnSpan="2"
Text="100"
x:Name="heightEntry"
Keyboard="Numeric"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<Label Grid.Row="0"
Grid.ColumnSpan="3"
Style="{StaticResource Header}"
Text="Popup's can be positioned anywhere on the screen using VerticalOptions and HorizontalOptions. Tap the arrows below to see how this works."
Text="Popups can be positioned anywhere on the screen using VerticalOptions and HorizontalOptions. Tap the arrows below to see how this works."
HorizontalTextAlignment="Center"/>

<Button Grid.Row="1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<ScrollView>
<VerticalStackLayout Spacing="12">
<Button Text="Simple Popup" Clicked="HandleSimplePopupButtonClicked" />

<Button Text="Self Closing Popup" Clicked="HandleSelfClosingPopupButtonClicked"/>

<Button Text="Button Popup" Clicked="HandleButtonPopupButtonClicked" />

Expand Down Expand Up @@ -45,6 +47,7 @@
<Button Text="Popup Style Page" Clicked="HandleStylePopupButtonClicked" />

<Button Text="OnDisappearing Popup" Clicked="HandleOnDisappearingPopupClicked" />

</VerticalStackLayout>
</ScrollView>
</ContentPage.Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Maui.Markup;
using CommunityToolkit.Maui.Sample.Pages.Views.Popup;
using CommunityToolkit.Maui.Sample.ViewModels.Views;
using CommunityToolkit.Maui.Sample.Views.Popups;
Expand Down Expand Up @@ -116,4 +118,16 @@ async void HandleOnDisappearingPopupClicked(object sender, EventArgs e)
{
await Navigation.PushModalAsync(new PopupOnDisappearingPage());
}

async void HandleSelfClosingPopupButtonClicked(object? sender, EventArgs e)
{
this.ShowPopup(new Label().Text("This Popup Will Close Automatically in 2 Seconds"), new PopupOptions
{
CanBeDismissedByTappingOutsideOfPopup = false
});

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

await this.ClosePopupAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<!-- Explicit styles -->
<Style x:Key="PopupLayout" TargetType="Layout" ApplyToDerivedTypes="true">
<Setter Property="Padding" Value="{OnPlatform Android=20, WinUI=20, iOS=5, MacCatalyst=5, Tizen=20}" />
<Setter Property="Padding" Value="{OnPlatform Android=5, WinUI=20, iOS=5, MacCatalyst=5, Tizen=20}" />
</Style>

<!-- Implicit styles -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
BackgroundColor="Red">
BackgroundColor="LightCoral">

<VerticalStackLayout Spacing="6">
<VerticalStackLayout.Resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public ButtonPopup()

void Button_Clicked(object? sender, EventArgs e)
{
Close();
CloseAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
xmlns:system="clr-namespace:System;assembly=System.Runtime"
BackgroundColor="White">

<VerticalStackLayout>
<VerticalStackLayout Spacing="6">
<VerticalStackLayout.Resources>
<ResourceDictionary>
<Style x:Key="Title" TargetType="Label">
Expand Down Expand Up @@ -37,6 +37,7 @@
<Setter Property="VerticalOptions" Value="End" />
<Setter Property="HorizontalOptions" Value="Center" />
<Setter Property="Spacing" Value="20" />
<Setter Property="Margin" Value="8" />
</Style>
</ResourceDictionary>
</VerticalStackLayout.Resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public MultipleButtonPopup()

async void Cancel_Clicked(object? sender, EventArgs e)
{
await Close(false);
await CloseAsync(false);
}

async void Okay_Clicked(object? sender, EventArgs e)
{
await Close(true);
await CloseAsync(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public NoOutsideTapDismissPopup()

async void Button_Clicked(object? sender, EventArgs e)
{
await Close();
await CloseAsync();
await Toast.Make("Popup Dismissed By Button").Show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public ReturnResultPopup()

async void Button_Clicked(object? sender, EventArgs e)
{
await Close("Close button tapped");
await CloseAsync("Close button tapped");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public partial class TransparentPopup : Maui.Views.Popup

void CloseButtonClicked(object? sender, EventArgs args)
{
Close();
CloseAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,45 @@ public PopupExtensionsTests()
navigation = page.Navigation;
}

[Fact]
public void ShowPopupAsync_WithPopupType_ShowsPopup()
[Fact(Timeout = (int)TestDuration.Short)]
public async Task ClosePopup_TokenExpired_ShouldThrowOperationCancelledException()
{
// Arrange
var cts = new CancellationTokenSource();

// Act
await cts.CancelAsync();

// Assert
await Assert.ThrowsAsync<OperationCanceledException>(() => navigation.ClosePopupAsync(cts.Token));
}

[Fact(Timeout = (int)TestDuration.Short)]
public async Task ClosePopup_NoExistingPopup_ShouldThrowPopupNotFoundException()
{
// Arrange

// Act

// Assert
await Assert.ThrowsAsync<PopupNotFoundException>(() => navigation.ClosePopupAsync(TestContext.Current.CancellationToken));
}

[Fact(Timeout = (int)TestDuration.Short)]
public async Task ClosePopup_PopupBlocked_ShouldThrowPopupBlockedException()
{
// Arrange

// Act
navigation.ShowPopup(new Button());
await navigation.PushModalAsync(new ContentPage());

// Assert
await Assert.ThrowsAsync<PopupBlockedException>(() => navigation.ClosePopupAsync(TestContext.Current.CancellationToken));
}

[Fact(Timeout = (int)TestDuration.Short)]
public async Task ShowPopupAsync_WithPopupType_ShowsPopupAndClosesPopup()
{
// Arrange
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
Expand All @@ -43,10 +80,16 @@ public void ShowPopupAsync_WithPopupType_ShowsPopup()
// Assert
Assert.Single(navigation.ModalStack);
Assert.IsType<PopupPage>(navigation.ModalStack[0]);

// Act
await navigation.ClosePopupAsync(TestContext.Current.CancellationToken);

// Assert
Assert.Empty(navigation.ModalStack);
}

[Fact]
public void ShowPopupAsync_Shell_WithPopupType_ShowsPopup()
[Fact(Timeout = (int)TestDuration.Short)]
public async Task ShowPopupAsync_Shell_WithPopupType_ShowsPopupAndClosesPopup()
{
// Arrange
var shell = new Shell();
Expand All @@ -63,6 +106,12 @@ public void ShowPopupAsync_Shell_WithPopupType_ShowsPopup()
// Assert
Assert.Single(shellNavigation.ModalStack);
Assert.IsType<PopupPage>(shellNavigation.ModalStack[0]);

// Act
await navigation.ClosePopupAsync(TestContext.Current.CancellationToken);

// Assert
Assert.Empty(navigation.ModalStack);
}

[Fact]
Expand Down Expand Up @@ -138,7 +187,7 @@ public async Task ShowPopupAsync_AwaitingShowPopupAsync_EnsurePreviousPopupClose
}

[Fact(Timeout = (int)TestDuration.Short)]
public async Task qShowPopupAsync_Shell_AwaitingShowPopupAsync_EnsurePreviousPopupClosed()
public async Task ShowPopupAsync_Shell_AwaitingShowPopupAsync_EnsurePreviousPopupClosed()
{
// Arrange
var shell = new Shell();
Expand Down Expand Up @@ -809,7 +858,7 @@ public async Task ShowPopupAsyncWithView_ShouldValidateProperBindingContext()

var popupPage = (PopupPage)navigation.ModalStack[0];

await popupPage.Close(new PopupResult<object?>(null, false), TestContext.Current.CancellationToken);
await popupPage.CloseAsync(new PopupResult<object?>(null, false), TestContext.Current.CancellationToken);
await showPopupTask;

// Assert
Expand All @@ -835,7 +884,7 @@ public async Task ShowPopupAsyncWithView_Shell_ShouldValidateProperBindingContex
var showPopupTask = shell.ShowPopupAsync<object?>(view, PopupOptions.Empty, shellParameters, TestContext.Current.CancellationToken);

var popupPage = (PopupPage)shellNavigation.ModalStack[0];
await popupPage.Close(new PopupResult<object?>(null, false), TestContext.Current.CancellationToken);
await popupPage.CloseAsync(new PopupResult<object?>(null, false), TestContext.Current.CancellationToken);

await showPopupTask;

Expand Down Expand Up @@ -897,7 +946,7 @@ public async Task ShowPopupAsyncWithView_ShouldReturnResultOnceClosed()

var popupPage = (PopupPage)navigation.ModalStack[0];

await popupPage.Close(expectedPopupResult, TestContext.Current.CancellationToken);
await popupPage.CloseAsync(expectedPopupResult, TestContext.Current.CancellationToken);
var actualPopupResult = await showPopupTask;

// Assert
Expand Down Expand Up @@ -927,7 +976,7 @@ public async Task ShowPopupAsyncWithView_Shell_ShouldReturnResultOnceClosed()

var popupPage = (PopupPage)shellNavigation.ModalStack[0];

await popupPage.Close(expectedPopupResult, TestContext.Current.CancellationToken);
await popupPage.CloseAsync(expectedPopupResult, TestContext.Current.CancellationToken);
var actualPopupResult = await showPopupTask;

// Assert
Expand Down Expand Up @@ -1005,7 +1054,7 @@ public async Task ShowPopupAsync_ShouldReturnNullResult_WhenPopupIsClosedWithout
var showPopupTask = navigation.ShowPopupAsync<object?>(new Popup(), PopupOptions.Empty, TestContext.Current.CancellationToken);

var popupPage = (PopupPage)navigation.ModalStack.Last();
await popupPage.Close(new PopupResult(true), TestContext.Current.CancellationToken);
await popupPage.CloseAsync(new PopupResult(true), TestContext.Current.CancellationToken);
var result = await showPopupTask;

// Assert
Expand All @@ -1029,7 +1078,7 @@ public async Task ShowPopupAsync_Shell_ShouldReturnNullResult_WhenPopupIsClosedW
var showPopupTask = shell.ShowPopupAsync<object?>(new Popup(), PopupOptions.Empty, shellParameters, TestContext.Current.CancellationToken);

var popupPage = (PopupPage)shellNavigation.ModalStack.Last();
await popupPage.Close(new PopupResult(true), TestContext.Current.CancellationToken);
await popupPage.CloseAsync(new PopupResult(true), TestContext.Current.CancellationToken);
var result = await showPopupTask;

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ async void HandleTick(object? sender, EventArgs e)
timer.Tick -= HandleTick;
try
{
await Close(Result);
await CloseAsync(Result);
}
catch (InvalidOperationException)
{
// If test has already ended, Popup.Close will throw an InvalidOperationException
// If test has already ended, Popup.CloseAsync will throw an InvalidOperationException
// because all Popups are removed from ModalStack in BaseHandlerTest.DisposeAsyncCore()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public async Task Close_ShouldThrowInvalidOperationException_NoPopupPageFound()
var popupPage = new PopupPage<string>(view, popupOptions);

// Act / Assert
await Assert.ThrowsAsync<PopupNotFoundException>(async () => await popupPage.Close(new PopupResult(false), CancellationToken.None));
await Assert.ThrowsAnyAsync<InvalidOperationException>(async () => await popupPage.Close(new PopupResult(false), CancellationToken.None));
await Assert.ThrowsAsync<PopupNotFoundException>(async () => await popupPage.CloseAsync(new PopupResult(false), CancellationToken.None));
await Assert.ThrowsAnyAsync<InvalidOperationException>(async () => await popupPage.CloseAsync(new PopupResult(false), CancellationToken.None));
}

[Fact]
Expand All @@ -80,7 +80,7 @@ public async Task Close_ShouldSetResultAndPopModalAsync()

await navigation.PushModalAsync(popupPage);

await popupPage.Close(expectedResult, CancellationToken.None);
await popupPage.CloseAsync(expectedResult, CancellationToken.None);
var actualResult = await tcs.Task;

// Assert
Expand Down Expand Up @@ -112,7 +112,7 @@ public async Task Close_ShouldThrowOperationCanceledException_WhenTokenIsCancell
await cts.CancelAsync();

// Assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => popupPage.Close(result, cts.Token));
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => popupPage.CloseAsync(result, cts.Token));
Assert.Single(mainPage.Navigation.ModalStack.OfType<PopupPage>());
}

Expand Down Expand Up @@ -196,8 +196,9 @@ public async Task PopupPageT_CloseAfterAdditionalModalPage_ShouldThrowInvalidOpe
await navigation.PushModalAsync(new ContentPage());

// Assert
await Assert.ThrowsAsync<InvalidPopupOperationException>(async () => await popupPage.Close(new PopupResult(false), CancellationToken.None));
await Assert.ThrowsAnyAsync<InvalidOperationException>(async () => await popupPage.Close(new PopupResult(false), CancellationToken.None));
await Assert.ThrowsAsync<PopupBlockedException>(async () => await popupPage.CloseAsync(new PopupResult(false), CancellationToken.None));
await Assert.ThrowsAnyAsync<InvalidPopupOperationException>(async () => await popupPage.CloseAsync(new PopupResult(false), CancellationToken.None));
await Assert.ThrowsAnyAsync<InvalidOperationException>(async () => await popupPage.CloseAsync(new PopupResult(false), CancellationToken.None));
}

[Fact]
Expand Down Expand Up @@ -402,7 +403,7 @@ public async Task PopupClosedEvent_ShouldTriggerOnce_WhenPopupIsClosed()

var popupPage = (PopupPage)navigation.ModalStack[0];
popupPage.PopupClosed += (sender, args) => eventTriggered = true;
await popupPage.Close(new PopupResult(false), CancellationToken.None);
await popupPage.CloseAsync(new PopupResult(false), CancellationToken.None);

// Assert
Assert.True(eventTriggered);
Expand Down Expand Up @@ -455,7 +456,7 @@ public async Task Close_ShouldThrowException_WhenCalledOnNonModalPopup()
var popupPage = new PopupPage(view, popupOptions);

// Act & Assert
await Assert.ThrowsAsync<PopupNotFoundException>(async () => await popupPage.Close(new PopupResult(false), CancellationToken.None));
await Assert.ThrowsAsync<PopupNotFoundException>(async () => await popupPage.CloseAsync(new PopupResult(false), CancellationToken.None));
}

[Fact]
Expand Down
Loading