using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Animation; namespace Tango.FirmwareUpdateLib.WPF { /// /// Represents a progress animation component capable of exposing a current and total progress with animations. /// /// public class ProgressAnimator : FrameworkElement { /// /// Gets or sets the animation duration between each update (Default 2 seconds). /// public TimeSpan Duration { get { return (TimeSpan)GetValue(DurationProperty); } set { SetValue(DurationProperty, value); } } public static readonly DependencyProperty DurationProperty = DependencyProperty.Register("Duration", typeof(TimeSpan), typeof(ProgressAnimator), new PropertyMetadata(TimeSpan.FromSeconds(1))); /// /// Gets or sets the current progress. (Use the ApplyProgress method instead of updating this directly). /// public double Current { get { return (double)GetValue(CurrentProperty); } set { SetValue(CurrentProperty, value); } } public static readonly DependencyProperty CurrentProperty = DependencyProperty.Register("Current", typeof(double), typeof(ProgressAnimator), new PropertyMetadata(0.0)); /// /// Gets or sets the total expected progress. (Use the ApplyProgress method instead of updating this directly). /// public double Total { get { return (double)GetValue(TotalProperty); } set { SetValue(TotalProperty, value); } } public static readonly DependencyProperty TotalProperty = DependencyProperty.Register("Total", typeof(double), typeof(ProgressAnimator), new PropertyMetadata(100.0)); /// /// Gets or sets a value indicating whether this instance is intermediate. /// /// /// true if this instance is intermediate; otherwise, false. /// public bool IsIntermediate { get { return (bool)GetValue(IsIntermediateProperty); } set { SetValue(IsIntermediateProperty, value); } } public static readonly DependencyProperty IsIntermediateProperty = DependencyProperty.Register("IsIntermediate", typeof(bool), typeof(ProgressAnimator), new PropertyMetadata(true)); /// /// Applies the progress properties animation. /// /// The current. /// The total. /// Will override the default duration for the next progress animation. public void ApplyProgress(double current, double total, TimeSpan? duration = null) { Total = total; IsIntermediate = false; DoubleAnimation ani = new DoubleAnimation(); ani.To = current; ani.Duration = duration != null ? duration.Value : Duration; this.BeginAnimation(CurrentProperty, ani, HandoffBehavior.SnapshotAndReplace); } /// /// Resets the progress properties. /// public void ResetProgress() { this.BeginAnimation(CurrentProperty, null, HandoffBehavior.SnapshotAndReplace); Current = 0; IsIntermediate = true; } } }