Skip to content

Commit

Permalink
(#4114) Cancel Progress dialog on Escape button
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed Jul 6, 2021
1 parent fd0892c commit bc84bd6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
17 changes: 9 additions & 8 deletions src/MahApps.Metro.Samples/MahApps.Metro.Demo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,25 +299,26 @@ private async void ShowProgressDialog(object sender, RoutedEventArgs e)
ColorScheme = this.MetroDialogOptions!.ColorScheme
};

var controller = await this.ShowProgressAsync("Please wait...", "We are baking some cupcakes!", settings: mySettings);
controller.SetIndeterminate();
var controller = await this.ShowProgressAsync("Please wait...", "We are baking now some cupcakes!", settings: mySettings);

await Task.Delay(5000);
controller.SetIndeterminate();

await Task.Delay(3000);

controller.SetCancelable(true);

double i = 0.0;
while (i < 6.0)
{
double val = (i / 100.0) * 20.0;
controller.SetProgress(val);
controller.SetMessage("Baking cupcake: " + i + "...");

if (controller.IsCanceled)
{
break; //canceled progressdialog auto closes.
break;
}

var val = (i / 100.0) * 20.0;
controller.SetProgress(val);
controller.SetMessage("Baking cupcake: " + i + "...");

i += 1.0;

await Task.Delay(2000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections;
using System.Globalization;
using System.Windows;
Expand Down
42 changes: 31 additions & 11 deletions src/MahApps.Metro/Controls/Dialogs/ProgressDialogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace MahApps.Metro.Controls.Dialogs
Expand All @@ -14,6 +16,8 @@ namespace MahApps.Metro.Controls.Dialogs
/// </summary>
public class ProgressDialogController
{
private CancellationTokenRegistration cancellationTokenRegistration;

private ProgressDialog WrappedDialog { get; }

private Func<Task> CloseCallback { get; }
Expand Down Expand Up @@ -45,21 +49,37 @@ internal ProgressDialogController(ProgressDialog dialog, Func<Task> closeCallBac

this.IsOpen = dialog.IsVisible;

this.WrappedDialog.Invoke(() => { this.WrappedDialog.PART_NegativeButton.Click += this.PART_NegativeButton_Click; });
this.WrappedDialog.Invoke(() =>
{
this.WrappedDialog.KeyDown += this.WrappedDialog_KeyDown;
this.WrappedDialog.PART_NegativeButton.Click += this.PART_NegativeButton_Click;
});

dialog.CancellationToken.Register(() => { this.WrappedDialog.BeginInvoke(this.Abort); });
this.cancellationTokenRegistration = dialog.CancellationToken.Register(() => { this.WrappedDialog.BeginInvoke(this.Abort); });
}

private void WrappedDialog_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape || (e.Key == Key.System && e.SystemKey == Key.F4))
{
this.WrappedDialog.Invoke(this.Abort);
}
}

private void PART_NegativeButton_Click(object sender, RoutedEventArgs e)
{
this.WrappedDialog.Invoke(this.Abort);
e.Handled = true;
}

private void Abort()
{
this.WrappedDialog.PART_NegativeButton.IsEnabled = false;
this.IsCanceled = true;
this.Canceled?.Invoke(this, EventArgs.Empty);
if (this.WrappedDialog.IsCancelable)
{
this.WrappedDialog.PART_NegativeButton.IsEnabled = false;
this.IsCanceled = true;
this.Canceled?.Invoke(this, EventArgs.Empty);
}
}

/// <summary>
Expand All @@ -85,16 +105,15 @@ public void SetCancelable(bool value)
/// <param name="value">The percentage to set as the value.</param>
public void SetProgress(double value)
{
Action action = () =>
this.WrappedDialog.Invoke(() =>
{
if (value < this.WrappedDialog.Minimum || value > this.WrappedDialog.Maximum)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
this.WrappedDialog.ProgressValue = value;
};
this.WrappedDialog.Invoke(action);
});
}

/// <summary>
Expand Down Expand Up @@ -148,18 +167,19 @@ public void SetProgressBarForegroundBrush(Brush brush)
/// <returns>A task representing the operation.</returns>
public Task CloseAsync()
{
Action action = () =>
this.WrappedDialog.Invoke(() =>
{
if (!this.WrappedDialog.IsVisible)
{
throw new InvalidOperationException("Dialog isn't visible to close");
}
this.WrappedDialog.Dispatcher.VerifyAccess();
this.WrappedDialog.KeyDown -= this.WrappedDialog_KeyDown;
this.WrappedDialog.PART_NegativeButton.Click -= this.PART_NegativeButton_Click;
};
this.WrappedDialog.Invoke(action);
this.cancellationTokenRegistration.Dispose();
});

return this.CloseCallback()
.ContinueWith(_ => this.WrappedDialog.Invoke(() =>
Expand Down
1 change: 0 additions & 1 deletion src/MahApps.Metro/Controls/VisualStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using JetBrains.Annotations;

namespace MahApps.Metro.Controls
{
Expand Down

0 comments on commit bc84bd6

Please sign in to comment.