|
| 1 | +using System.Windows; |
| 2 | +using System.Windows.Controls; |
| 3 | + |
| 4 | +namespace NETworkManager; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// A control featuring a range of loading indicating animations. |
| 8 | +/// Source: https://github.com/zeluisping/LoadingIndicators.WPF |
| 9 | +/// </summary> |
| 10 | +[TemplatePart(Name = "Border", Type = typeof(Border))] |
| 11 | +public class LoadingIndicator : Control |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Identifies the <see cref="NETworkManager.LoadingIndicator.SpeedRatio"/> dependency property. |
| 15 | + /// </summary> |
| 16 | + public static readonly DependencyProperty SpeedRatioProperty = |
| 17 | + DependencyProperty.Register(nameof(SpeedRatio), typeof(double), typeof(LoadingIndicator), new PropertyMetadata(1d, (o, e) => |
| 18 | + { |
| 19 | + LoadingIndicator li = (LoadingIndicator)o; |
| 20 | + |
| 21 | + if (li.PART_Border == null || li.IsActive == false) |
| 22 | + { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + foreach (VisualStateGroup group in VisualStateManager.GetVisualStateGroups(li.PART_Border)) |
| 27 | + { |
| 28 | + if (group.Name == "ActiveStates") |
| 29 | + { |
| 30 | + foreach (VisualState state in group.States) |
| 31 | + { |
| 32 | + if (state.Name == "Active") |
| 33 | + { |
| 34 | + state.Storyboard.SetSpeedRatio(li.PART_Border, (double)e.NewValue); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + })); |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Identifies the <see cref="NETworkManager.LoadingIndicator.IsActive"/> dependency property. |
| 43 | + /// </summary> |
| 44 | + public static readonly DependencyProperty IsActiveProperty = |
| 45 | + DependencyProperty.Register(nameof(IsActive), typeof(bool), typeof(LoadingIndicator), new PropertyMetadata(true, (o, e) => |
| 46 | + { |
| 47 | + LoadingIndicator li = (LoadingIndicator)o; |
| 48 | + |
| 49 | + if (li.PART_Border == null) |
| 50 | + { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if ((bool)e.NewValue == false) |
| 55 | + { |
| 56 | + VisualStateManager.GoToElementState(li.PART_Border, "Inactive", false); |
| 57 | + li.PART_Border.Visibility = Visibility.Collapsed; |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + VisualStateManager.GoToElementState(li.PART_Border, "Active", false); |
| 62 | + li.PART_Border.Visibility = Visibility.Visible; |
| 63 | + |
| 64 | + foreach (VisualStateGroup group in VisualStateManager.GetVisualStateGroups(li.PART_Border)) |
| 65 | + { |
| 66 | + if (group.Name == "ActiveStates") |
| 67 | + { |
| 68 | + foreach (VisualState state in group.States) |
| 69 | + { |
| 70 | + if (state.Name == "Active") |
| 71 | + { |
| 72 | + state.Storyboard.SetSpeedRatio(li.PART_Border, li.SpeedRatio); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + })); |
| 79 | + |
| 80 | + // Variables |
| 81 | + protected Border PART_Border; |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Get/set the speed ratio of the animation. |
| 85 | + /// </summary> |
| 86 | + public double SpeedRatio |
| 87 | + { |
| 88 | + get { return (double)GetValue(SpeedRatioProperty); } |
| 89 | + set { SetValue(SpeedRatioProperty, value); } |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Get/set whether the loading indicator is active. |
| 94 | + /// </summary> |
| 95 | + public bool IsActive |
| 96 | + { |
| 97 | + get { return (bool)GetValue(IsActiveProperty); } |
| 98 | + set { SetValue(IsActiveProperty, value); } |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// When overridden in a derived class, is invoked whenever application code |
| 103 | + /// or internal processes call System.Windows.FrameworkElement.ApplyTemplate(). |
| 104 | + /// </summary> |
| 105 | + public override void OnApplyTemplate() |
| 106 | + { |
| 107 | + base.OnApplyTemplate(); |
| 108 | + |
| 109 | + PART_Border = (Border)GetTemplateChild("PART_Border"); |
| 110 | + |
| 111 | + if (PART_Border != null) |
| 112 | + { |
| 113 | + VisualStateManager.GoToElementState(PART_Border, (this.IsActive ? "Active" : "Inactive"), false); |
| 114 | + foreach (VisualStateGroup group in VisualStateManager.GetVisualStateGroups(PART_Border)) |
| 115 | + { |
| 116 | + if (group.Name == "ActiveStates") |
| 117 | + { |
| 118 | + foreach (VisualState state in group.States) |
| 119 | + { |
| 120 | + if (state.Name == "Active") |
| 121 | + { |
| 122 | + state.Storyboard.SetSpeedRatio(PART_Border, this.SpeedRatio); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + PART_Border.Visibility = (IsActive ? Visibility.Visible : Visibility.Collapsed); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Initializes a new instance of the <see cref="NETworkManager.LoadingIndicator"/> class. |
| 134 | + /// </summary> |
| 135 | + public LoadingIndicator() |
| 136 | + { |
| 137 | + |
| 138 | + } |
| 139 | +} |
0 commit comments