Skip to content

Commit a43bcef

Browse files
[Popup] Add DefaultPopupOptionsSettings and DefaultPopupSettings to .UseMauiCommunityToolkit(Options) (#2759)
* Add `Popup.CanBeDismissedByTappingOutsideOfPopup` * Add GetCanBeDismissedByTappingOutsideOfPopup() * Add PopupDefaults Test * Add `DefaultPopupSettings()` * Add `DefaultPopupSettings` and `DefaultPopupOptionsSettings` * Add Unit Tests * Update Unit Tests * Update DefaultPopupSettingsTests.cs * Update PopupExtensionsTests.cs * Move `PopupDefaults` and `PopupOptionsDefaults` to private nested class * Update PopupExtensionsTests.cs * Resolve Merge Conflicts * Update XML docs * Fix XML
1 parent 5c30d3f commit a43bcef

File tree

19 files changed

+679
-212
lines changed

19 files changed

+679
-212
lines changed

samples/CommunityToolkit.Maui.Sample/MauiProgram.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public static MauiApp CreateMauiApp()
5555
.UseMauiCommunityToolkit(static options =>
5656
{
5757
options.SetShouldEnableSnackbarOnWindows(true);
58+
options.SetPopupDefaults(new DefaultPopupSettings());
59+
options.SetPopupOptionsDefaults(new DefaultPopupOptionsSettings());
5860
})
5961
#else
6062
.UseMauiCommunityToolkit(static options =>

src/CommunityToolkit.Maui.UnitTests/BaseTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ protected virtual void Dispose(bool isDisposing)
8484
options.SetShouldSuppressExceptionsInAnimations(false);
8585
options.SetShouldSuppressExceptionsInBehaviors(false);
8686
options.SetShouldSuppressExceptionsInConverters(false);
87+
options.SetPopupDefaults(new DefaultPopupSettings());
88+
options.SetPopupOptionsDefaults(new DefaultPopupOptionsSettings());
8789

8890
// Restore default MediaElementOptions
8991
var mediaElementOptions = new MediaElementOptions();

src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service
7474
#endregion
7575

7676
#region Register Services for PopupServiceTests
77-
7877
appBuilder.Services.AddTransientPopup<LongLivedSelfClosingPopup, LongLivedMockPageViewModel>();
7978
appBuilder.Services.AddTransientPopup<ShortLivedSelfClosingPopup, ShortLivedMockPageViewModel>();
8079
appBuilder.Services.AddTransientPopup<GarbageCollectionHeavySelfClosingPopup, MockPageViewModel>();

src/CommunityToolkit.Maui.UnitTests/Extensions/AppBuilderExtensionsTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public void ConfirmOptionsDefaultValue()
2121
Assert.False(Options.ShouldSuppressExceptionsInBehaviors);
2222
Assert.False(Options.ShouldSuppressExceptionsInConverters);
2323
Assert.False(isAndroidDialogFragmentServiceInitialized);
24+
Assert.Equal(Options.DefaultPopupSettings, new DefaultPopupSettings());
25+
Assert.Equal(Options.DefaultPopupOptionsSettings, new DefaultPopupOptionsSettings());
2426

2527
Core.AppBuilderExtensions.ShouldUseStatusBarBehaviorOnAndroidModalPageOptionCompleted -= HandleShouldUseStatusBarBehaviorOnAndroidModalPageOptionCompleted;
2628

@@ -50,6 +52,8 @@ public void ConfirmDefaultValueRemainWhenOptionsNull()
5052
Assert.False(Options.ShouldSuppressExceptionsInBehaviors);
5153
Assert.False(Options.ShouldSuppressExceptionsInConverters);
5254
Assert.True(isAndroidDialogFragmentServiceInitialized);
55+
Assert.Equal(Options.DefaultPopupSettings, new DefaultPopupSettings());
56+
Assert.Equal(Options.DefaultPopupOptionsSettings, new DefaultPopupOptionsSettings());
5357

5458
void HandleShouldUseStatusBarBehaviorOnAndroidModalPageOptionCompleted(object? sender, EventArgs e)
5559
{
@@ -88,6 +92,25 @@ public void UseMauiCommunityToolkit_ShouldAssignValues()
8892
// Arrange
8993
var builder = MauiApp.CreateBuilder();
9094
bool isAndroidDialogFragmentServiceInitialized = false;
95+
var defaultPopupSettings = new DefaultPopupSettings
96+
{
97+
CanBeDismissedByTappingOutsideOfPopup = true,
98+
BackgroundColor = Colors.Orange,
99+
HorizontalOptions = LayoutOptions.End,
100+
VerticalOptions = LayoutOptions.Start,
101+
Margin = 72,
102+
Padding = 4
103+
};
104+
105+
var defaultPopupOptionsSettings = new DefaultPopupOptionsSettings
106+
{
107+
CanBeDismissedByTappingOutsideOfPopup = true,
108+
OnTappingOutsideOfPopup = () => Console.WriteLine("Hello World"),
109+
PageOverlayColor = Colors.Green,
110+
Shadow = null,
111+
Shape = null
112+
};
113+
91114
Core.AppBuilderExtensions.ShouldUseStatusBarBehaviorOnAndroidModalPageOptionCompleted += HandleShouldUseStatusBarBehaviorOnAndroidModalPageOptionCompleted;
92115

93116
// Act
@@ -98,6 +121,8 @@ public void UseMauiCommunityToolkit_ShouldAssignValues()
98121
options.SetShouldSuppressExceptionsInBehaviors(!Options.ShouldSuppressExceptionsInBehaviors);
99122
options.SetShouldSuppressExceptionsInConverters(!Options.ShouldSuppressExceptionsInConverters);
100123
options.SetShouldUseStatusBarBehaviorOnAndroidModalPage(!Core.Options.ShouldUseStatusBarBehaviorOnAndroidModalPage);
124+
options.SetPopupDefaults(defaultPopupSettings);
125+
options.SetPopupOptionsDefaults(defaultPopupOptionsSettings);
101126
});
102127

103128
// Assert
@@ -107,6 +132,8 @@ public void UseMauiCommunityToolkit_ShouldAssignValues()
107132
Assert.True(Options.ShouldSuppressExceptionsInBehaviors);
108133
Assert.True(Options.ShouldSuppressExceptionsInConverters);
109134
Assert.False(isAndroidDialogFragmentServiceInitialized);
135+
Assert.Equal(defaultPopupSettings, Options.DefaultPopupSettings);
136+
Assert.Equal(defaultPopupOptionsSettings, Options.DefaultPopupOptionsSettings);
110137

111138
Core.AppBuilderExtensions.ShouldUseStatusBarBehaviorOnAndroidModalPageOptionCompleted -= HandleShouldUseStatusBarBehaviorOnAndroidModalPageOptionCompleted;
112139

src/CommunityToolkit.Maui.UnitTests/Extensions/PopupExtensionsTests.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public void ShowPopupAsync_WithViewType_ShowsPopup()
184184
public void ShowPopupAsync_WithViewType_SetsCorrectDefaults()
185185
{
186186
// Arrange
187+
Border popupBorder;
187188
PopupPage popupPage;
188189
Popup autogeneratedPopup;
189190
var label = new Label();
@@ -192,13 +193,16 @@ public void ShowPopupAsync_WithViewType_SetsCorrectDefaults()
192193
navigation.ShowPopup(label);
193194

194195
popupPage = (PopupPage)navigation.ModalStack[0];
196+
popupBorder = popupPage.Content.PopupBorder;
195197
autogeneratedPopup = (Popup)(popupPage.Content.PopupBorder.Content ?? throw new InvalidOperationException("Border Content cannot be null"));
196198

197199
// Assert
198-
Assert.Equal(PopupDefaults.BackgroundColor, autogeneratedPopup.BackgroundColor);
199-
Assert.Equal(PopupDefaults.HorizontalOptions, autogeneratedPopup.HorizontalOptions);
200-
Assert.Equal(PopupDefaults.VerticalOptions, autogeneratedPopup.VerticalOptions);
201-
Assert.Equal(PopupDefaults.Padding, autogeneratedPopup.Padding);
200+
Assert.Equal(new Thickness(30), popupBorder.Margin);
201+
Assert.Equal(LayoutOptions.Center, popupBorder.VerticalOptions);
202+
Assert.Equal(LayoutOptions.Center, popupBorder.HorizontalOptions);
203+
Assert.Equal(new Thickness(15), autogeneratedPopup.Padding);
204+
Assert.Equal(Colors.White, autogeneratedPopup.BackgroundColor);
205+
Assert.True(autogeneratedPopup.CanBeDismissedByTappingOutsideOfPopup);
202206
}
203207

204208
[Fact]
@@ -465,7 +469,6 @@ public void ShowPopupAsync_WithCustomOptions_AppliesOptions()
465469
// Verify Border Bindings to Border
466470
Assert.Equal(popup.BindingContext, border.BindingContext);
467471
Assert.Equal(popup.Margin, border.Margin);
468-
Assert.Equal(((IPaddingElement)popup).Padding, border.Padding);
469472
Assert.Equal(popup.Background, border.Background);
470473
Assert.Equal(popup.BackgroundColor, border.BackgroundColor);
471474
Assert.Equal(popup.HorizontalOptions, border.HorizontalOptions);
@@ -537,7 +540,6 @@ public void ShowPopupAsync_Shell_WithCustomOptions_AppliesOptions()
537540
// Verify Border Bindings to Border
538541
Assert.Equal(popup.BindingContext, border.BindingContext);
539542
Assert.Equal(popup.Margin, border.Margin);
540-
Assert.Equal(((IPaddingElement)popup).Padding, border.Padding);
541543
Assert.Equal(popup.Background, border.Background);
542544
Assert.Equal(popup.BackgroundColor, border.BackgroundColor);
543545
Assert.Equal(popup.HorizontalOptions, border.HorizontalOptions);
@@ -563,6 +565,8 @@ public void ShowPopupAsyncWithView_WithCustomOptions_AppliesOptions()
563565

564566
var view = new Grid
565567
{
568+
Margin = 20,
569+
BackgroundColor = Colors.Orange,
566570
HorizontalOptions = LayoutOptions.End,
567571
VerticalOptions = LayoutOptions.Start,
568572
};
@@ -606,10 +610,9 @@ public void ShowPopupAsyncWithView_WithCustomOptions_AppliesOptions()
606610
Assert.Equal(view.VerticalOptions, border.VerticalOptions);
607611
Assert.Equal(view.HorizontalOptions, border.HorizontalOptions);
608612

609-
// Verify Border Bindings to Border
613+
// Verify Popup Bindings to Border
610614
Assert.Equal(popup.BindingContext, border.BindingContext);
611615
Assert.Equal(popup.Margin, border.Margin);
612-
Assert.Equal(((IPaddingElement)popup).Padding, border.Padding);
613616
Assert.Equal(popup.Background, border.Background);
614617
Assert.Equal(popup.BackgroundColor, border.BackgroundColor);
615618
Assert.Equal(popup.HorizontalOptions, border.HorizontalOptions);
@@ -676,22 +679,17 @@ public void ShowPopupAsyncWithView_Shell_WithCustomOptions_AppliesOptions()
676679
Assert.Equal(viewWithQueryable.Background, popup.Background);
677680
Assert.Equal(viewWithQueryable.BackgroundColor, popup.BackgroundColor);
678681
Assert.Equal(viewWithQueryable.Margin, popup.Margin);
679-
Assert.Equal(viewWithQueryable.VerticalOptions, popup.VerticalOptions);
680-
Assert.Equal(viewWithQueryable.HorizontalOptions, popup.HorizontalOptions);
681682

682683
// Verify View options Binding to Border
683684
Assert.Equal(viewWithQueryable.BindingContext, border.BindingContext);
684685
Assert.Equal(viewWithQueryable.Background, border.Background);
685686
Assert.Equal(viewWithQueryable.BackgroundColor, border.BackgroundColor);
686-
Assert.Equal(viewWithQueryable.Padding, border.Padding);
687-
Assert.Equal(viewWithQueryable.Margin, border.Margin);
688687
Assert.Equal(viewWithQueryable.VerticalOptions, border.VerticalOptions);
689688
Assert.Equal(viewWithQueryable.HorizontalOptions, border.HorizontalOptions);
690689

691-
// Verify Border Bindings to Border
690+
// Verify Border Bindings to Popup
692691
Assert.Equal(popup.BindingContext, border.BindingContext);
693692
Assert.Equal(popup.Margin, border.Margin);
694-
Assert.Equal(((IPaddingElement)popup).Padding, border.Padding);
695693
Assert.Equal(popup.Background, border.Background);
696694
Assert.Equal(popup.BackgroundColor, border.BackgroundColor);
697695
Assert.Equal(popup.HorizontalOptions, border.HorizontalOptions);

0 commit comments

Comments
 (0)