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

Progressdialog improvements #2097

Merged
merged 7 commits into from
Aug 28, 2015
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions MahApps.Metro/Controls/Dialogs/ProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal ProgressDialog(MetroWindow parentWindow, MetroDialogSettings settings)
ProgressBarForeground = Brushes.White;
}
}

internal ProgressDialog(MetroWindow parentWindow)
: this(parentWindow, null)
{ }
Expand Down Expand Up @@ -69,5 +70,33 @@ internal CancellationToken CancellationToken
{
get { return DialogSettings.CancellationToken; }
}

internal double Minimum
{
get { return PART_ProgressBar.Minimum; }
set { PART_ProgressBar.Minimum = value; }
}

internal double Maximum
{
get { return PART_ProgressBar.Maximum; }
set { PART_ProgressBar.Maximum = value; }
}

internal double ProgressValue
{
get { return PART_ProgressBar.Value; }
set
{
PART_ProgressBar.IsIndeterminate = false;
PART_ProgressBar.Value = value;
PART_ProgressBar.ApplyTemplate();
}
}

internal void SetIndeterminate()
{
PART_ProgressBar.IsIndeterminate = true;
}
}
}
51 changes: 30 additions & 21 deletions MahApps.Metro/Controls/Dialogs/ProgressDialogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ public class ProgressDialogController
/// <summary>
/// Gets if the wrapped ProgressDialog is open.
/// </summary>
[Obsolete("Use the Closed event instead")]
public bool IsOpen { get; private set; }

/// <summary>
/// This event is raised when the associated <see cref="ProgressDialog"/> was closed.
/// </summary>
public event EventHandler Closed;

internal ProgressDialogController(ProgressDialog dialog, Func<Task> closeCallBack)
{
WrappedDialog = dialog;
CloseCallback = closeCallBack;

IsOpen = dialog.IsVisible;

WrappedDialog.PART_NegativeButton.Dispatcher.Invoke(new Action(() => {
InvokeAction(() => {
WrappedDialog.PART_NegativeButton.Click += PART_NegativeButton_Click;
}));
});

dialog.CancellationToken.Register(() =>
{
Expand All @@ -41,15 +47,15 @@ private void PART_NegativeButton_Click(object sender, RoutedEventArgs e)
WrappedDialog.PART_NegativeButton.IsEnabled = false;
};

this.InvokeAction(action);
InvokeAction(action);
}

/// <summary>
/// Sets the ProgressBar's IsIndeterminate to true. To set it to false, call SetProgress.
/// </summary>
public void SetIndeterminate()
{
this.InvokeAction(() => WrappedDialog.PART_ProgressBar.IsIndeterminate = true);
InvokeAction(() => WrappedDialog.SetIndeterminate());
}

/// <summary>
Expand All @@ -58,7 +64,7 @@ public void SetIndeterminate()
/// <param name="value"></param>
public void SetCancelable(bool value)
{
this.InvokeAction(() => WrappedDialog.IsCancelable = value);
InvokeAction(() => WrappedDialog.IsCancelable = value);
}

/// <summary>
Expand All @@ -68,35 +74,33 @@ public void SetCancelable(bool value)
public void SetProgress(double value)
{
Action action = () => {
if (value < WrappedDialog.PART_ProgressBar.Minimum || value > WrappedDialog.PART_ProgressBar.Maximum)
if (value < WrappedDialog.Minimum || value > WrappedDialog.Maximum)
{
throw new ArgumentOutOfRangeException("value");
}

WrappedDialog.PART_ProgressBar.IsIndeterminate = false;
WrappedDialog.PART_ProgressBar.Value = value;
WrappedDialog.PART_ProgressBar.ApplyTemplate();
WrappedDialog.ProgressValue = value;
};

this.InvokeAction(action);
InvokeAction(action);
}

/// <summary>
/// Gets/Sets the minimum restriction of the progress Value property
/// </summary>
public double Minimum
{
get { return this.InvokeFunc(() => WrappedDialog.PART_ProgressBar.Minimum); }
set { this.InvokeAction(() => WrappedDialog.PART_ProgressBar.Minimum = value); }
get { return InvokeFunc(() => WrappedDialog.Minimum); }
set { InvokeAction(() => WrappedDialog.Minimum = value); }
}

/// <summary>
/// Gets/Sets the maximum restriction of the progress Value property
/// </summary>
public double Maximum
{
get { return this.InvokeFunc(() => WrappedDialog.PART_ProgressBar.Maximum); }
set { this.InvokeAction(() => WrappedDialog.PART_ProgressBar.Maximum = value); }
get { return InvokeFunc(() => WrappedDialog.Maximum); }
set { InvokeAction(() => WrappedDialog.Maximum = value); }
}

/// <summary>
Expand All @@ -105,7 +109,7 @@ public double Maximum
/// <param name="message">The message to be set.</param>
public void SetMessage(string message)
{
this.InvokeAction(() => WrappedDialog.Message = message);
InvokeAction(() => WrappedDialog.Message = message);
}

/// <summary>
Expand All @@ -114,7 +118,7 @@ public void SetMessage(string message)
/// <param name="title">The title to be set.</param>
public void SetTitle(string title)
{
this.InvokeAction(() => WrappedDialog.Title = title);
InvokeAction(() => WrappedDialog.Title = title);
}

/// <summary>
Expand All @@ -129,18 +133,23 @@ public void SetTitle(string title)
public Task CloseAsync()
{
Action action = () => {
if (!IsOpen)
if (!WrappedDialog.IsVisible)
{
throw new InvalidOperationException();
}
WrappedDialog.Dispatcher.VerifyAccess();
WrappedDialog.PART_NegativeButton.Click -= PART_NegativeButton_Click;
};

this.InvokeAction(action);
InvokeAction(action);

return CloseCallback().ContinueWith(x => WrappedDialog.Dispatcher.Invoke(new Action(() => {
return CloseCallback().ContinueWith(_ => InvokeAction(new Action(() => {
IsOpen = false;

var handler = Closed;
if (handler != null) {
handler(this, EventArgs.Empty);
}
})));
}

Expand All @@ -152,7 +161,7 @@ private double InvokeFunc(Func<double> getValueFunc)
}
else
{
return (double)this.WrappedDialog.Dispatcher.Invoke(new Func<double>(getValueFunc));
return (double)WrappedDialog.Dispatcher.Invoke(new Func<double>(getValueFunc));
}
}

Expand All @@ -164,7 +173,7 @@ private void InvokeAction(Action setValueAction)
}
else
{
WrappedDialog.Dispatcher.Invoke(new Action(setValueAction));
WrappedDialog.Dispatcher.Invoke(setValueAction);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion MahApps.Metro/Themes/Dialogs/ProgressDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
Height="6"
EllipseDiameter="5"
Panel.ZIndex="5"
IsIndeterminate="True"
Minimum="0.0"
Maximum="1.0"
Foreground="{Binding ProgressBarForeground, RelativeSource={RelativeSource AncestorType=Dialogs:ProgressDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Expand Down
2 changes: 2 additions & 0 deletions docs/release-notes/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ This is a bug fix and feature release of MahApps.Metro v1.2.0.
+ introduce `VirtualisedMetroTreeView`
- SelectAllOnFocus for input and login dialogs #1750
- New `MetroValidationPopup` style and new `CloseOnMouseLeftButtonDown` dependency property #2058 #1469
- Progress bar in `ProgressDialog` is not set to Indetermined by default anymore, must be set explicitly by calling `ProgressDialogController.SetIndeterminate()` #2097
- Added `Closed` event to `ProgressDialogController` #2097

# Bugfixes

Expand Down
1 change: 1 addition & 0 deletions samples/MetroDemo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ private async void ShowLoginDialogPasswordPreview(object sender, RoutedEventArgs
private async void ShowProgressDialog(object sender, RoutedEventArgs e)
{
var controller = await this.ShowProgressAsync("Please wait...", "We are baking some cupcakes!");
controller.SetIndeterminate();

await TaskEx.Delay(5000);

Expand Down