Skip to content
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

Provide the ability to show popups without having to subclass Popup #1581

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1e15307
Initial musings on showing popups without having to create your own p…
bijington Nov 29, 2023
fdd7715
Remove commented code
bijington Nov 29, 2023
1f78fa7
Pass on the BindingContext of a view up to the popup
bijington Dec 4, 2023
71847fe
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
brminnick Feb 2, 2024
68928f1
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
bijington Feb 11, 2024
f7fb581
Expand IPopupService API to enable the ability to supply the configur…
bijington Feb 11, 2024
733a467
Merge branch 'feature/sl/remove-popup-constraint-from-popup-service' …
bijington Feb 11, 2024
3969115
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
brminnick Feb 15, 2024
b365000
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
bijington Feb 18, 2024
3a14894
Name the cancellation token usage
bijington Feb 20, 2024
a3ae853
Merge branch 'feature/sl/remove-popup-constraint-from-popup-service' …
bijington Feb 20, 2024
46f8182
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
bijington Mar 27, 2024
086b161
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
brminnick Mar 27, 2024
e0d6dfc
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
bijington Jun 5, 2024
b2810bb
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
bijington Jun 9, 2024
e249bbc
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
brminnick Jul 24, 2024
64916d3
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
bijington Aug 1, 2024
574b600
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
bijington Aug 28, 2024
7584b48
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
bijington Sep 3, 2024
0a9d436
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
bijington Sep 8, 2024
880bd32
Include DynamicallyAccessedMembers
bijington Sep 10, 2024
b701700
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
bijington Sep 11, 2024
8c993d3
Merge branch 'main' into feature/sl/remove-popup-constraint-from-popu…
bijington Oct 15, 2024
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
2 changes: 2 additions & 0 deletions samples/CommunityToolkit.Maui.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ static void RegisterViewsAndViewModels(in IServiceCollection services)
services.AddTransientPopup<CsharpBindingPopup, CsharpBindingPopupViewModel>();
services.AddTransientPopup<UpdatingPopup, UpdatingPopupViewModel>();
services.AddTransientPopup<XamlBindingPopup, XamlBindingPopupViewModel>();

services.AddTransientPopupContent<PopupContentView, PopupContentViewModel>();
}

static void RegisterEssentials(in IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<Button Text="C# Binding Popup" Command="{Binding CsharpBindingPopupCommand}" />

<Button Text="Updating Popup" Command="{Binding UpdatingPopupCommand}" />

<Button Text="Show Popup content" Command="{Binding ShowPopupContentCommand}" />
</VerticalStackLayout>
</ScrollView>
</ContentPage.Content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ Task OnCsharpBindingPopup(CancellationToken token)
{
return popupService.ShowPopupAsync<CsharpBindingPopupViewModel>(
onPresenting: viewModel => viewModel.Load("This is a platform specific popup with a .NET MAUI View being rendered. The behaviors of the popup will confirm to 100% this platform look and feel, but still allows you to use your .NET MAUI Controls."),
token);
token: token);
}

[RelayCommand]
Task OnUpdatingPopup(CancellationToken token)
{
return popupService.ShowPopupAsync<UpdatingPopupViewModel>(
onPresenting: viewModel => viewModel.PerformUpdates(10),
token);
token: token);
}

[RelayCommand]
Task OnShowPopupContent(CancellationToken token)
{
return popupService.ShowPopupAsync<PopupContentViewModel>(
onPresenting: viewModel => viewModel.SetMessage("This is a dynamically set message, shown in a popup without the need to create your own Popup subclass."),
token: token);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using CommunityToolkit.Mvvm.ComponentModel;

namespace CommunityToolkit.Maui.Sample.ViewModels.Views;

public sealed partial class PopupContentViewModel : BaseViewModel
{
[ObservableProperty]
string message = "";

internal void SetMessage(string text) => this.Message = text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<Grid
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CommunityToolkit.Maui.Sample.Views.Popups.PopupContentView"
xmlns:viewModels="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Views"
x:DataType="viewModels:PopupContentViewModel">

<Label
Text="{Binding Message}"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
Margin="20" />

</Grid>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CommunityToolkit.Maui.Sample.ViewModels.Views;

namespace CommunityToolkit.Maui.Sample.Views.Popups;

public partial class PopupContentView : Grid
{
public PopupContentView(PopupContentViewModel popupContentViewModel)
{
InitializeComponent();

BindingContext = popupContentViewModel;
}
}
79 changes: 57 additions & 22 deletions src/CommunityToolkit.Maui.Core/IPopupService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using Microsoft.Maui.Primitives;

namespace CommunityToolkit.Maui.Core;

Expand All @@ -11,31 +12,61 @@ public interface IPopupService
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>.
/// </summary>
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam>
void ShowPopup<TViewModel>() where TViewModel : INotifyPropertyChanged;
/// <param name="canBeDismissedByTappingOutsideOfPopup">A <see cref="bool"/> indicating whether the <see cref="CommunityToolkit.Maui.Core.IPopup"/> can be dismissed by tapping outside of it.</param>
/// <param name="resultWhenUserTapsOutsideOfPopup">An <see cref="object"/> that will be returned when the user taps outside of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="verticalOptions">The vertical alignment of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="horizontalOptions">The horizontal alignment of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="size">The size of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="color">The background color of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
void ShowPopup<TViewModel>(
bool canBeDismissedByTappingOutsideOfPopup = true,
object? resultWhenUserTapsOutsideOfPopup = default,
LayoutAlignment verticalOptions = LayoutAlignment.Center,
LayoutAlignment horizontalOptions = LayoutAlignment.Center,
Size size = default,
Color? color = default) where TViewModel : INotifyPropertyChanged;

/// <summary>
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>.
/// The supplied <paramref name="onPresenting"/> provides a mechanism to invoke any methods on your view model in order to load or pass data to it.
/// </summary>
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam>
/// <param name="onPresenting">An <see cref="Action{TViewModel}"/> that will be performed before the popup is presented.</param>
void ShowPopup<TViewModel>(Action<TViewModel> onPresenting) where TViewModel : INotifyPropertyChanged;

/// <summary>
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>.
/// </summary>
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam>
/// <param name="viewModel">The view model to use as the <c>BindingContext</c> for the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
[Obsolete("This method will be removed in an upcoming version. Use ShowPopup<TViewModel> instead.")]
void ShowPopup<TViewModel>(TViewModel viewModel) where TViewModel : INotifyPropertyChanged;
/// <param name="canBeDismissedByTappingOutsideOfPopup">A <see cref="bool"/> indicating whether the <see cref="CommunityToolkit.Maui.Core.IPopup"/> can be dismissed by tapping outside of it.</param>
/// <param name="resultWhenUserTapsOutsideOfPopup">An <see cref="object"/> that will be returned when the user taps outside of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="verticalOptions">The vertical alignment of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="horizontalOptions">The horizontal alignment of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="size">The size of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="color">The background color of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
void ShowPopup<TViewModel>(
Action<TViewModel> onPresenting,
bool canBeDismissedByTappingOutsideOfPopup = true,
object? resultWhenUserTapsOutsideOfPopup = default,
LayoutAlignment verticalOptions = LayoutAlignment.Center,
LayoutAlignment horizontalOptions = LayoutAlignment.Center,
Size size = default,
Color? color = default) where TViewModel : INotifyPropertyChanged;

/// <summary>
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>.
/// </summary>
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam>
/// <param name="canBeDismissedByTappingOutsideOfPopup">A <see cref="bool"/> indicating whether the <see cref="CommunityToolkit.Maui.Core.IPopup"/> can be dismissed by tapping outside of it.</param>
/// <param name="resultWhenUserTapsOutsideOfPopup">An <see cref="object"/> that will be returned when the user taps outside of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="verticalOptions">The vertical alignment of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="horizontalOptions">The horizontal alignment of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="size">The size of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="color">The background color of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="token"><see cref="CancellationToken"/> </param>
/// <returns>A <see cref="Task"/> that can be awaited to return the result of the <see cref="CommunityToolkit.Maui.Core.IPopup"/> once it has been dismissed.</returns>
Task<object?> ShowPopupAsync<TViewModel>(CancellationToken token = default) where TViewModel : INotifyPropertyChanged;
Task<object?> ShowPopupAsync<TViewModel>(
bool canBeDismissedByTappingOutsideOfPopup = true,
object? resultWhenUserTapsOutsideOfPopup = default,
LayoutAlignment verticalOptions = LayoutAlignment.Center,
LayoutAlignment horizontalOptions = LayoutAlignment.Center,
Size size = default,
Color? color = default,
CancellationToken token = default) where TViewModel : INotifyPropertyChanged;

/// <summary>
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>.
Expand All @@ -44,16 +75,20 @@ public interface IPopupService
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam>
/// <returns>A <see cref="Task"/> that can be awaited to return the result of the <see cref="CommunityToolkit.Maui.Core.IPopup"/> once it has been dismissed.</returns>
/// <param name="onPresenting">An <see cref="Action{TViewModel}"/> that will be performed before the popup is presented.</param>
/// <param name="canBeDismissedByTappingOutsideOfPopup">A <see cref="bool"/> indicating whether the <see cref="CommunityToolkit.Maui.Core.IPopup"/> can be dismissed by tapping outside of it.</param>
/// <param name="resultWhenUserTapsOutsideOfPopup">An <see cref="object"/> that will be returned when the user taps outside of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="verticalOptions">The vertical alignment of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="horizontalOptions">The horizontal alignment of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="size">The size of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="color">The background color of the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="token"><see cref="CancellationToken"/> </param>
Task<object?> ShowPopupAsync<TViewModel>(Action<TViewModel> onPresenting, CancellationToken token = default) where TViewModel : INotifyPropertyChanged;

/// <summary>
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>.
/// </summary>
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam>
/// <param name="viewModel">The view model to use as the <c>BindingContext</c> for the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param>
/// <param name="token"><see cref="CancellationToken"/> </param>
/// <returns>A <see cref="Task"/> that can be awaited to return the result of the <see cref="CommunityToolkit.Maui.Core.IPopup"/> once it has been dismissed.</returns>
[Obsolete("This method will be removed in an upcoming version. Use ShowPopupAsync<TViewModel> instead.")]
Task<object?> ShowPopupAsync<TViewModel>(TViewModel viewModel, CancellationToken token = default) where TViewModel : INotifyPropertyChanged;
Task<object?> ShowPopupAsync<TViewModel>(
Action<TViewModel> onPresenting,
bool canBeDismissedByTappingOutsideOfPopup = true,
object? resultWhenUserTapsOutsideOfPopup = default,
LayoutAlignment verticalOptions = LayoutAlignment.Center,
LayoutAlignment horizontalOptions = LayoutAlignment.Center,
Size size = default,
Color? color = default,
CancellationToken token = default) where TViewModel : INotifyPropertyChanged;
}
Loading