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

Added IProgress<int> and IProgress<string> to ProgressDialog #13

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Changes from all commits
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
25 changes: 24 additions & 1 deletion src/Ookii.Dialogs.Wpf/ProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Ookii.Dialogs.Wpf
/// </remarks>
/// <threadsafety static="true" instance="false" />
[DefaultEvent("DoWork"), DefaultProperty("Text"), Description("Represents a dialog that can be used to report progress to the user.")]
public partial class ProgressDialog : Component
public partial class ProgressDialog : Component , IProgress<int>, IProgress<string>
{
private class ProgressChangedData
{
Expand All @@ -42,6 +42,7 @@ private class ProgressChangedData
private bool _useCompactPathsForDescription;
private SafeModuleHandle _currentAnimationModuleHandle;
private bool _cancellationPending;
private int _percentProgress;

/// <summary>
/// Event raised when the dialog is displayed.
Expand Down Expand Up @@ -518,6 +519,24 @@ public void ShowDialog(Window owner, object argument)
RunProgressDialog(owner == null ? NativeMethods.GetActiveWindow() : new WindowInteropHelper(owner).Handle, argument);
}

/// <summary>
/// Updates the dialog's progress bar.
/// </summary>
/// <param name="value">The percentage, from 0 to 100, of the operation that is complete.</param>
void IProgress<int>.Report(int value)
{
ReportProgress(value, null, null, null);
}

/// <summary>
/// Updates the dialog's progress bar.
/// </summary>
/// <param name="value">The new value of the progress dialog's primary text message, or <see langword="null"/> to leave the value unchanged.</param>
void IProgress<string>.Report(string value)
{
ReportProgress(_percentProgress, value, null, null);
}

/// <summary>
/// Updates the dialog's progress bar.
/// </summary>
Expand Down Expand Up @@ -568,6 +587,10 @@ public void ReportProgress(int percentProgress, string text, string description,
throw new ArgumentOutOfRangeException("percentProgress");
if( _dialog == null )
throw new InvalidOperationException(Properties.Resources.ProgressDialogNotRunningError);

// we need to cache the latest percentProgress so IProgress<string>.Report(text) can report the percent progress correctly.
_percentProgress = percentProgress;

_backgroundWorker.ReportProgress(percentProgress, new ProgressChangedData() { Text = text, Description = description, UserState = userState });
}

Expand Down