Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

fix(taskbar): clear the progress bar when the run ends

fatDuckeb77de049e92
diff --git a/optimizerDuck.Test/UI/Dialogs/ProcessingDialogTests.cs b/optimizerDuck.Test/UI/Dialogs/ProcessingDialogTests.cs
index 3124827..ae296ee 100644
--- a/optimizerDuck.Test/UI/Dialogs/ProcessingDialogTests.cs
+++ b/optimizerDuck.Test/UI/Dialogs/ProcessingDialogTests.cs
@@ -41,14 +41,19 @@ public class ProcessingDialogTests
Assert.Equal(10, result.Total);
}
- [Fact]
- public void MapProgress_ValueExceedsTotal_ClampsToTotal()
+ [Theory]
+ [InlineData(1, 1)]
+ [InlineData(10, 10)]
+ [InlineData(15, 10)]
+ public void MapProgress_ValueReachingTotal_ClearsTheBar(int value, int total)
{
- var result = ProcessingDialog.MapProgress(false, 15, 10);
+ // A run that reached its total is over: painting the bar full there left it on the
+ // taskbar after the dialog closed, with nothing left to clear it.
+ var result = ProcessingDialog.MapProgress(false, value, total);
- Assert.Equal(TaskBarProgressState.Normal, result.State);
- Assert.Equal(10, result.Current);
- Assert.Equal(10, result.Total);
+ Assert.Equal(TaskBarProgressState.None, result.State);
+ Assert.Equal(0, result.Current);
+ Assert.Equal(0, result.Total);
}
[Fact]
diff --git a/optimizerDuck/UI/Dialogs/ProcessingDialog.xaml.cs b/optimizerDuck/UI/Dialogs/ProcessingDialog.xaml.cs
index 0f83f99..0f6b692 100644
--- a/optimizerDuck/UI/Dialogs/ProcessingDialog.xaml.cs
+++ b/optimizerDuck/UI/Dialogs/ProcessingDialog.xaml.cs
@@ -2,7 +2,9 @@ using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
+using System.Windows.Media;
using optimizerDuck.UI.ViewModels.Dialogs;
+using Wpf.Ui.Controls;
using Wpf.Ui.TaskBar;
namespace optimizerDuck.UI.Dialogs;
@@ -10,16 +12,71 @@ namespace optimizerDuck.UI.Dialogs;
public partial class ProcessingDialog : UserControl
{
private Window? _trackedWindow;
+ private ContentDialog? _hostDialog;
+
+ /// <summary>Set once the run is over, so a late report cannot put the bar back.</summary>
+ private bool _detached;
public ProcessingDialog()
{
InitializeComponent();
DataContextChanged += OnDataContextChanged;
- Loaded += (_, _) => ApplyToTaskbar();
- Unloaded += (_, _) => ClearTaskbar();
+ Loaded += OnLoaded;
+ Unloaded += OnUnloaded;
+ }
+
+ private void OnLoaded(object sender, RoutedEventArgs e)
+ {
+ // Hiding a ContentDialog does not reliably unload its content, so the dialog's own
+ // Closed event (the run is over) is the dependable place to drop the taskbar bar.
+ _detached = false;
+ _hostDialog = FindHostDialog(this);
+ if (_hostDialog is not null)
+ _hostDialog.Closed += OnHostDialogClosed;
+
+ ApplyToTaskbar();
+ }
+
+ private void OnUnloaded(object sender, RoutedEventArgs e)
+ {
+ Detach();
+ }
+
+ private void OnHostDialogClosed(ContentDialog sender, ContentDialogClosedEventArgs args)
+ {
+ Detach();
+ }
+
+ /// <summary>Ends the run: clears the bar and ignores every later progress report.</summary>
+ private void Detach()
+ {
+ _detached = true;
+ if (_hostDialog is not null)
+ {
+ _hostDialog.Closed -= OnHostDialogClosed;
+ _hostDialog = null;
+ }
+
+ ClearTaskbar();
}
- /// <summary>Maps dialog progress to a taskbar progress indicator state.</summary>
+ private static ContentDialog? FindHostDialog(DependencyObject? node)
+ {
+ while (node is not null)
+ {
+ if (node is ContentDialog dialog)
+ return dialog;
+ node = node is Visual ? VisualTreeHelper.GetParent(node) : null;
+ }
+
+ return null;
+ }
+
+ /// <summary>
+ /// Maps dialog progress to a taskbar indicator state. A finished run (value reaching the
+ /// total) maps to <see cref="TaskBarProgressState.None" />, so the bar cannot outlive the
+ /// operation even when the dialog's own close never reaches this control.
+ /// </summary>
internal static (TaskBarProgressState State, int Current, int Total) MapProgress(
bool isIndeterminate,
int value,
@@ -29,7 +86,10 @@ public partial class ProcessingDialog : UserControl
if (isIndeterminate || total <= 0)
return (TaskBarProgressState.Indeterminate, 0, 0);
- return (TaskBarProgressState.Normal, Math.Clamp(value, 0, total), total);
+ var current = Math.Clamp(value, 0, total);
+ return current >= total
+ ? (TaskBarProgressState.None, 0, 0)
+ : (TaskBarProgressState.Normal, current, total);
}
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
@@ -58,6 +118,11 @@ public partial class ProcessingDialog : UserControl
private void ApplyToTaskbar()
{
+ // The window handle stays cached after the dialog is gone: without this, a report that
+ // lands after the run finished turns the bar back on with nothing left to clear it.
+ if (_detached || !IsLoaded)
+ return;
+
if (DataContext is not ProcessingViewModel viewModel)
return;
@@ -82,6 +147,8 @@ public partial class ProcessingDialog : UserControl
viewModel.Total
);
+ // A finished operation reports value == total and maps to None here, so the bar cannot
+ // outlive the run even if the dialog's own close never reaches us.
if (state == TaskBarProgressState.Normal)
_ = TaskBarProgress.SetValue(handle, state, current, total);
else