Skip to content
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
20 changes: 20 additions & 0 deletions src/CommunityToolkit.Maui.UnitTests/Alerts/ToastTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ public ToastTests()
Assert.IsType<IAlert>(toast, exactMatch: false);
}

[Fact]
public void EnsureWindowsToastEnabled_NotificationsDisabled_ThrowsSetupException()
{
var exception = Assert.Throws<InvalidOperationException>(Toast.EnsureWindowsToastEnabled);

Assert.Contains("UseMauiCommunityToolkit", exception.Message);
Assert.Contains(nameof(Options.SetShouldEnableSnackbarOnWindows), exception.Message);
Assert.Contains("Package.appxmanifest", exception.Message);
Assert.Equal("https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/toast", exception.HelpLink);
}

[Fact]
public void EnsureWindowsToastEnabled_NotificationsEnabled_DoesNotThrow()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiCommunityToolkit(options => options.SetShouldEnableSnackbarOnWindows(true));
Comment thread
TheCodeTraveler marked this conversation as resolved.
Resolved

Toast.EnsureWindowsToastEnabled();
}

[Fact(Timeout = (int)TestDuration.Short)]
public async Task ToastShow_CancellationTokenExpires()
{
Expand Down
17 changes: 17 additions & 0 deletions src/CommunityToolkit.Maui/Alerts/Toast/Toast.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public static IToast Make(
/// <summary>
/// Show Toast
/// </summary>
/// <remarks>
/// On Windows, enable notification registration by calling <see cref="AppBuilderExtensions.UseMauiCommunityToolkit"/>
/// with <see cref="Options.SetShouldEnableSnackbarOnWindows"/> set to <see langword="true"/>.
/// Packaged Windows apps also require the manifest setup described in the
/// <see href="https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/toast">Toast documentation</see>.
/// </remarks>
public virtual Task Show(CancellationToken token = default)
{
#if WINDOWS
Expand Down Expand Up @@ -100,6 +106,17 @@ public void Dispose()
GC.SuppressFinalize(this);
}

internal static void EnsureWindowsToastEnabled()
{
if (!Options.ShouldEnableSnackbarOnWindows)
{
throw new InvalidOperationException($"To enable {nameof(Toast)} on Windows, call {nameof(AppBuilderExtensions.UseMauiCommunityToolkit)}(options => options.{nameof(Options.SetShouldEnableSnackbarOnWindows)}(true)). Packaged Windows apps also require additional setup in Package.appxmanifest. See https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/toast")
{
HelpLink = "https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/toast"
};
}
}

static TimeSpan GetDuration(ToastDuration duration) => duration switch
{
ToastDuration.Short => TimeSpan.FromSeconds(2),
Expand Down
3 changes: 3 additions & 0 deletions src/CommunityToolkit.Maui/Alerts/Toast/Toast.windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ static async Task DismissPlatform(CancellationToken token)

async Task ShowPlatform(CancellationToken token)
{
token.ThrowIfCancellationRequested();
EnsureWindowsToastEnabled();

await DismissPlatform(token);
token.ThrowIfCancellationRequested();
PlatformToast = new AppNotificationBuilder()
Expand Down
8 changes: 6 additions & 2 deletions src/CommunityToolkit.Maui/Options.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ internal Options()
public void SetShouldSuppressExceptionsInBehaviors(bool value) => ShouldSuppressExceptionsInBehaviors = value;

/// <summary>
/// Enables <see cref="Alerts.Snackbar"/> for Windows
/// Enables <see cref="Alerts.Snackbar"/> and <see cref="Alerts.Toast"/> for Windows
/// </summary>
/// <remarks>
/// Additional setup is required in the Package.appxmanifest file to enable <see cref="Alerts.Snackbar"/> on Windows. See the <a href="https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/snackbar">Snackbar Platform Specific Initialization Documentation</a> for more information. Default value is false.
/// This option registers the Windows notification manager used by both alerts.
/// Packaged Windows apps require additional setup in Package.appxmanifest. See the
/// <a href="https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/toast">Toast Platform Specific Initialization Documentation</a>
/// and <a href="https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/snackbar">Snackbar Platform Specific Initialization Documentation</a>
/// for more information. Default value is false.
/// </remarks>
public void SetShouldEnableSnackbarOnWindows(bool value)
{
Expand Down
Loading