From a6572c029cbb784b7034ab3f9ca152778f0d2d29 Mon Sep 17 00:00:00 2001 From: James Crutchley Date: Sat, 5 Oct 2024 08:31:46 -0700 Subject: [PATCH 1/3] Track window count and manage snackbar notifications Added a static `NumberOfWindows` property to the `Options` class to track the number of windows. Updated `SetShouldEnableSnackbarOnWindows` to increment/decrement `NumberOfWindows` on window creation/closure. Conditional registration/unregistration of `AppNotificationManager` for snackbar notifications based on `NumberOfWindows` count. --- src/CommunityToolkit.Maui/Options.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/CommunityToolkit.Maui/Options.cs b/src/CommunityToolkit.Maui/Options.cs index 6199182544..a688e566f4 100644 --- a/src/CommunityToolkit.Maui/Options.cs +++ b/src/CommunityToolkit.Maui/Options.cs @@ -15,7 +15,7 @@ internal Options(in MauiAppBuilder builder) : this() { this.builder = builder; } - + internal static int NumberOfWindows { get; private set; } = 0; internal static bool ShouldSuppressExceptionsInAnimations { get; private set; } internal static bool ShouldSuppressExceptionsInConverters { get; private set; } internal static bool ShouldSuppressExceptionsInBehaviors { get; private set; } @@ -66,13 +66,25 @@ public void SetShouldEnableSnackbarOnWindows(bool value) builder.ConfigureLifecycleEvents(events => { events.AddWindows(windows => windows + .OnWindowCreated((_) => + { + NumberOfWindows++; + }) .OnLaunched((_, _) => { - Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked += OnSnackbarNotificationInvoked; - Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Register(); + if(NumberOfWindows == 1) + { + Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked += OnSnackbarNotificationInvoked; + Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Register(); + } }) .OnClosed((_, _) => { + if (NumberOfWindows > 1) + { + NumberOfWindows--; + return; + } Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked -= OnSnackbarNotificationInvoked; Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Unregister(); })); From f359899807963552d6175d18ecb3b89839a0fc30 Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Date: Mon, 11 Nov 2024 10:06:29 -0800 Subject: [PATCH 2/3] Remove `NumberOfWindows ` --- src/CommunityToolkit.Maui/Options.cs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/CommunityToolkit.Maui/Options.cs b/src/CommunityToolkit.Maui/Options.cs index a688e566f4..f25321eb04 100644 --- a/src/CommunityToolkit.Maui/Options.cs +++ b/src/CommunityToolkit.Maui/Options.cs @@ -15,7 +15,7 @@ internal Options(in MauiAppBuilder builder) : this() { this.builder = builder; } - internal static int NumberOfWindows { get; private set; } = 0; + internal static bool ShouldSuppressExceptionsInAnimations { get; private set; } internal static bool ShouldSuppressExceptionsInConverters { get; private set; } internal static bool ShouldSuppressExceptionsInBehaviors { get; private set; } @@ -58,21 +58,17 @@ public void SetShouldEnableSnackbarOnWindows(bool value) { throw new InvalidOperationException($"{nameof(SetShouldEnableSnackbarOnWindows)} must be called using the {nameof(AppBuilderExtensions.UseMauiCommunityToolkit)} extension method. See the Platform Specific Initialization section of the {nameof(Alerts.Snackbar)} documentaion for more inforamtion: https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/snackbar)") { - HelpLink = "https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/snackbar" - }; + HelpLink = "https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/snackbar" + }; } else if (value is true && builder is not null) { builder.ConfigureLifecycleEvents(events => { events.AddWindows(windows => windows - .OnWindowCreated((_) => - { - NumberOfWindows++; - }) .OnLaunched((_, _) => { - if(NumberOfWindows == 1) + if (Application.Current?.Windows.Count is 1) { Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked += OnSnackbarNotificationInvoked; Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Register(); @@ -80,13 +76,11 @@ public void SetShouldEnableSnackbarOnWindows(bool value) }) .OnClosed((_, _) => { - if (NumberOfWindows > 1) + if (Application.Current?.Windows.Count is 1) { - NumberOfWindows--; - return; + Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked -= OnSnackbarNotificationInvoked; + Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Unregister(); } - Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked -= OnSnackbarNotificationInvoked; - Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Unregister(); })); }); From ea42125dbe4c4c8df12581bb924647bdae0121f0 Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Date: Mon, 11 Nov 2024 10:09:23 -0800 Subject: [PATCH 3/3] Add null check --- src/CommunityToolkit.Maui/Options.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/CommunityToolkit.Maui/Options.cs b/src/CommunityToolkit.Maui/Options.cs index f25321eb04..b2eff9fb7b 100644 --- a/src/CommunityToolkit.Maui/Options.cs +++ b/src/CommunityToolkit.Maui/Options.cs @@ -68,7 +68,12 @@ public void SetShouldEnableSnackbarOnWindows(bool value) events.AddWindows(windows => windows .OnLaunched((_, _) => { - if (Application.Current?.Windows.Count is 1) + if (Application.Current is null) + { + throw new InvalidOperationException($"{nameof(Application)}.{nameof(Application.Current)} cannot be null when Windows are launched"); + } + + else if (Application.Current.Windows.Count is 1) { Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked += OnSnackbarNotificationInvoked; Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Register(); @@ -76,7 +81,11 @@ public void SetShouldEnableSnackbarOnWindows(bool value) }) .OnClosed((_, _) => { - if (Application.Current?.Windows.Count is 1) + if (Application.Current is null) + { + throw new InvalidOperationException($"{nameof(Application)}.{nameof(Application.Current)} cannot be null when Windows are closed"); + } + else if (Application.Current.Windows.Count is 1) { Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked -= OnSnackbarNotificationInvoked; Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Unregister();