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

Windows Snackbar rewrite #1658

Merged
merged 8 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 0 additions & 41 deletions samples/CommunityToolkit.Maui.Sample/Platforms/Windows/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Diagnostics;
using Microsoft.UI.Xaml;

namespace CommunityToolkit.Maui.Sample.Windows;

/// <summary>
Expand All @@ -18,42 +15,4 @@ public App()
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

static Mutex? mutex;

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
if (!IsSingleInstance())
{
Process.GetCurrentProcess().Kill();
}
else
{
base.OnLaunched(args);
}
}


static bool IsSingleInstance()
{
const string applicationId = "1F9C3A44-059B-4FBC-9D92-476E59FB937A";
mutex = new Mutex(false, applicationId);

// keep the mutex reference alive until the normal
// termination of the program
GC.KeepAlive(mutex);

try
{
return mutex.WaitOne(0, false);
}
catch (AbandonedMutexException)
{
// if one thread acquires a Mutex object
// that another thread has abandoned
// by exiting without releasing it
mutex.ReleaseMutex();
return mutex.WaitOne(0, false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap rescap">
xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
IgnorableNamespaces="uap rescap com desktop">

<Identity Name="maui-package-name-placeholder" Publisher="CN=Microsoft" Version="0.0.0.0" />
<Identity Name="6e919706-2634-4d97-a93c-2213b2acc334" Publisher="CN=Microsoft" Version="0.0.0.0" />
jfversluis marked this conversation as resolved.
Show resolved Hide resolved

<Properties>
<DisplayName>$placeholder$</DisplayName>
Expand Down Expand Up @@ -33,6 +35,23 @@
<uap:DefaultTile Square71x71Logo="$placeholder$.png" Wide310x150Logo="$placeholder$.png" Square310x310Logo="$placeholder$.png" />
<uap:SplashScreen Image="$placeholder$.png" />
</uap:VisualElements>
<Extensions>

<!--Specify which CLSID to activate when notification is clicked-->
<desktop:Extension Category="windows.toastNotificationActivation">
<desktop:ToastNotificationActivation ToastActivatorCLSID="6e919706-2634-4d97-a93c-2213b2acc334" />
</desktop:Extension>

<!--Register COM CLSID-->
<com:Extension Category="windows.comServer">
<com:ComServer>
<com:ExeServer Executable="CommunityToolkit.Maui.Sample\CommunityToolkit.Maui.Sample.exe" DisplayName="$targetnametoken$" Arguments="----AppNotificationActivated:">
<com:Class Id="6e919706-2634-4d97-a93c-2213b2acc334" />
</com:ExeServer>
</com:ComServer>
</com:Extension>

</Extensions>
</Application>
</Applications>

Expand Down
61 changes: 32 additions & 29 deletions src/CommunityToolkit.Maui/Alerts/Snackbar/Snackbar.windows.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
using Microsoft.Maui.Dispatching;
using Windows.UI.Notifications;
using static CommunityToolkit.Maui.Extensions.ToastNotificationExtensions;
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;

namespace CommunityToolkit.Maui.Alerts;

public partial class Snackbar
{
static Windows.UI.Notifications.ToastNotification? PlatformSnackbar { get; set; }
const string snackbarIdentifierArgumentKey = "snackbarIdentifier";

static AppNotification? PlatformSnackbar { get; set; }

static Dictionary<string, Action?> actions = new();

TaskCompletionSource<bool>? dismissedTCS;

internal static void HandleSnackbarAction(AppNotificationActivatedEventArgs args)
{
if (args.Arguments.TryGetValue(snackbarIdentifierArgumentKey, out var id) && actions.TryGetValue(id, out var action) && action is not null)
{
Dispatcher.GetForCurrentThread().DispatchIfRequired(action);
}
}

/// <summary>
/// Dispose Snackbar
/// </summary>
Expand All @@ -35,13 +46,15 @@ async Task DismissPlatform(CancellationToken token)
}

token.ThrowIfCancellationRequested();
ToastNotificationManager.History.Clear();
await AppNotificationManager.Default.RemoveAllAsync();
actions.Clear();

PlatformSnackbar.Activated -= OnActivated;
PlatformSnackbar.Dismissed -= OnDismissed;
PlatformSnackbar.ExpirationTime = DateTimeOffset.Now;

PlatformSnackbar = null;
// Verify PlatformToast is not null again after `await`
if (PlatformSnackbar is not null)
{
PlatformSnackbar.Expiration = DateTimeOffset.Now;
PlatformSnackbar = null;
}

await (dismissedTCS?.Task ?? Task.CompletedTask);
}
Expand All @@ -55,28 +68,18 @@ async Task ShowPlatform(CancellationToken token)
token.ThrowIfCancellationRequested();

dismissedTCS = new();
var id = Guid.NewGuid().ToString();
PlatformSnackbar = new AppNotificationBuilder()
.AddText(Text)
.AddButton(new AppNotificationButton(ActionButtonText)
.AddArgument(snackbarIdentifierArgumentKey, id))
.BuildNotification();
PlatformSnackbar.Expiration = DateTimeOffset.Now.Add(Duration);

PlatformSnackbar = new ToastNotification(BuildToastNotificationContent(Text, ActionButtonText));
PlatformSnackbar.Activated += OnActivated;
PlatformSnackbar.Dismissed += OnDismissed;
PlatformSnackbar.ExpirationTime = DateTimeOffset.Now.Add(Duration);
AppNotificationManager.Default.Show(PlatformSnackbar);

ToastNotificationManager.CreateToastNotifier().Show(PlatformSnackbar);
actions.Add(id, Action);

OnShown();
}

void OnActivated(ToastNotification sender, object args)
{
if (PlatformSnackbar is not null && Action is not null)
{
Dispatcher.GetForCurrentThread().DispatchIfRequired(Action);
}
}

void OnDismissed(ToastNotification sender, ToastDismissedEventArgs args)
{
dismissedTCS?.TrySetResult(true);
Dispatcher.GetForCurrentThread().DispatchIfRequired(OnDismissed);
}
}
29 changes: 28 additions & 1 deletion src/CommunityToolkit.Maui/AppBuilderExtensions.shared.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Core.Handlers;
using CommunityToolkit.Maui.Views;
using Microsoft.Maui.LifecycleEvents;

namespace CommunityToolkit.Maui;

Expand All @@ -20,6 +22,23 @@ public static MauiAppBuilder UseMauiCommunityToolkit(this MauiAppBuilder builder
// Pass `null` because `options?.Invoke()` will set options on both `CommunityToolkit.Maui` and `CommunityToolkit.Maui.Core`
builder.UseMauiCommunityToolkitCore(null);

#if WINDOWS
builder.ConfigureLifecycleEvents(events =>
{
VladislavAntonyuk marked this conversation as resolved.
Show resolved Hide resolved
events.AddWindows(windows => windows
.OnLaunched((_, _) =>
{
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked += OnSnackbarNotificationInvoked;
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Register();
})
.OnClosed((_, _) =>
{
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked -= OnSnackbarNotificationInvoked;
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Unregister();
}));
});
#endif

builder.Services.AddSingleton<IPopupService, PopupService>();

// Invokes options for both `CommunityToolkit.Maui` and `CommunityToolkit.Maui.Core`
Expand All @@ -36,4 +55,12 @@ public static MauiAppBuilder UseMauiCommunityToolkit(this MauiAppBuilder builder

return builder;
}
#if WINDOWS
static void OnSnackbarNotificationInvoked(
Microsoft.Windows.AppNotifications.AppNotificationManager sender,
VladislavAntonyuk marked this conversation as resolved.
Show resolved Hide resolved
Microsoft.Windows.AppNotifications.AppNotificationActivatedEventArgs args)
{
Snackbar.HandleSnackbarAction(args);
}
#endif
}

This file was deleted.

Loading