aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Firmware/Tango.FirmwareUpdateLib.WPF/ProgressAnimator.cs
blob: 755df4dcbd8840482f056ddb61bbda9da0f3f4e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
{
    /// <summary>
    /// Represents a progress animation component capable of exposing a current and total progress with animations.
    /// </summary>
    /// <seealso cref="System.Windows.FrameworkElement" />
    public class ProgressAnimator : FrameworkElement
    {
        /// <summary>
        /// Gets or sets the animation duration between each update (Default 2 seconds).
        /// </summary>
        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)));

        /// <summary>
        /// Gets or sets the current progress. (Use the ApplyProgress method instead of updating this directly).
        /// </summary>
        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));

        /// <summary>
        /// Gets or sets the total expected progress.  (Use the ApplyProgress method instead of updating this directly).
        /// </summary>
        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));

        /// <summary>
        /// Gets or sets a value indicating whether this instance is intermediate.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is intermediate; otherwise, <c>false</c>.
        /// </value>
        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));

        /// <summary>
        /// Applies the progress properties animation.
        /// </summary>
        /// <param name="current">The current.</param>
        /// <param name="total">The total.</param>
        /// <param name="duration">Will override the default duration for the next progress animation.</param>
        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);
        }

        /// <summary>
        /// Resets the progress properties.
        /// </summary>
        public void ResetProgress()
        {
            this.BeginAnimation(CurrentProperty, null, HandoffBehavior.SnapshotAndReplace);
            Current = 0;
            IsIntermediate = true;
        }
    }
}