forked from BornToBeRoot/NETworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadingIndicators.cs
More file actions
139 lines (122 loc) · 4.63 KB
/
Copy pathLoadingIndicators.cs
File metadata and controls
139 lines (122 loc) · 4.63 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System.Windows;
using System.Windows.Controls;
namespace NETworkManager;
/// <summary>
/// A control featuring a range of loading indicating animations.
/// Source: https://github.com/zeluisping/LoadingIndicators.WPF
/// </summary>
[TemplatePart(Name = "Border", Type = typeof(Border))]
public class LoadingIndicator : Control
{
/// <summary>
/// Identifies the <see cref="NETworkManager.LoadingIndicator.SpeedRatio"/> dependency property.
/// </summary>
public static readonly DependencyProperty SpeedRatioProperty =
DependencyProperty.Register(nameof(SpeedRatio), typeof(double), typeof(LoadingIndicator), new PropertyMetadata(1d, (o, e) =>
{
LoadingIndicator li = (LoadingIndicator)o;
if (li.PART_Border == null || li.IsActive == false)
{
return;
}
foreach (VisualStateGroup group in VisualStateManager.GetVisualStateGroups(li.PART_Border))
{
if (group.Name == "ActiveStates")
{
foreach (VisualState state in group.States)
{
if (state.Name == "Active")
{
state.Storyboard.SetSpeedRatio(li.PART_Border, (double)e.NewValue);
}
}
}
}
}));
/// <summary>
/// Identifies the <see cref="NETworkManager.LoadingIndicator.IsActive"/> dependency property.
/// </summary>
public static readonly DependencyProperty IsActiveProperty =
DependencyProperty.Register(nameof(IsActive), typeof(bool), typeof(LoadingIndicator), new PropertyMetadata(true, (o, e) =>
{
LoadingIndicator li = (LoadingIndicator)o;
if (li.PART_Border == null)
{
return;
}
if ((bool)e.NewValue == false)
{
VisualStateManager.GoToElementState(li.PART_Border, "Inactive", false);
li.PART_Border.Visibility = Visibility.Collapsed;
}
else
{
VisualStateManager.GoToElementState(li.PART_Border, "Active", false);
li.PART_Border.Visibility = Visibility.Visible;
foreach (VisualStateGroup group in VisualStateManager.GetVisualStateGroups(li.PART_Border))
{
if (group.Name == "ActiveStates")
{
foreach (VisualState state in group.States)
{
if (state.Name == "Active")
{
state.Storyboard.SetSpeedRatio(li.PART_Border, li.SpeedRatio);
}
}
}
}
}
}));
// Variables
protected Border PART_Border;
/// <summary>
/// Get/set the speed ratio of the animation.
/// </summary>
public double SpeedRatio
{
get { return (double)GetValue(SpeedRatioProperty); }
set { SetValue(SpeedRatioProperty, value); }
}
/// <summary>
/// Get/set whether the loading indicator is active.
/// </summary>
public bool IsActive
{
get { return (bool)GetValue(IsActiveProperty); }
set { SetValue(IsActiveProperty, value); }
}
/// <summary>
/// When overridden in a derived class, is invoked whenever application code
/// or internal processes call System.Windows.FrameworkElement.ApplyTemplate().
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
PART_Border = (Border)GetTemplateChild("PART_Border");
if (PART_Border != null)
{
VisualStateManager.GoToElementState(PART_Border, (this.IsActive ? "Active" : "Inactive"), false);
foreach (VisualStateGroup group in VisualStateManager.GetVisualStateGroups(PART_Border))
{
if (group.Name == "ActiveStates")
{
foreach (VisualState state in group.States)
{
if (state.Name == "Active")
{
state.Storyboard.SetSpeedRatio(PART_Border, this.SpeedRatio);
}
}
}
}
PART_Border.Visibility = (IsActive ? Visibility.Visible : Visibility.Collapsed);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="NETworkManager.LoadingIndicator"/> class.
/// </summary>
public LoadingIndicator()
{
}
}