From 3e22d5a85209958563229bc2c82cf0d2dd9d8efc Mon Sep 17 00:00:00 2001 From: Kristen Schau <47155823+krschau@users.noreply.github.com> Date: Fri, 9 Feb 2024 10:51:31 -0800 Subject: [PATCH] Deactivate widgets when Dashboard isn't displayed (#2240) --- .../DevHome.Dashboard/ViewModels/WidgetViewModel.cs | 5 +++++ .../DevHome.Dashboard/Views/DashboardView.xaml.cs | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs b/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs index 10b90cafd6..53d769fea0 100644 --- a/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs +++ b/tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs @@ -323,4 +323,9 @@ private async void HandleWidgetUpdated(Widget sender, WidgetUpdatedEventArgs arg Log.Logger()?.ReportDebug("WidgetViewModel", $"HandleWidgetUpdated for widget {sender.Id}"); await RenderWidgetFrameworkElementAsync(); } + + public void UnsubscribeFromWidgetUpdates() + { + Widget.WidgetUpdated -= HandleWidgetUpdated; + } } diff --git a/tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs b/tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs index 9a0b31733c..e53e94d425 100644 --- a/tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs +++ b/tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs @@ -20,6 +20,7 @@ using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Automation; using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Navigation; using Microsoft.Windows.Widgets; using Microsoft.Windows.Widgets.Hosts; using Windows.System; @@ -589,6 +590,17 @@ private async void WidgetControl_Drop(object sender, DragEventArgs e) Log.Logger()?.ReportDebug("DashboardView", $"Drop ended"); } + protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) + { + Log.Logger()?.ReportDebug("DashboardView", $"Leaving Dashboard, deactivating widgets."); + + // Deactivate widgets if we're not on the Dashboard. + foreach (var widget in PinnedWidgets) + { + widget.UnsubscribeFromWidgetUpdates(); + } + } + #if DEBUG private void AddResetButton(object sender, RoutedEventArgs e) {