From efc6a4a33ed6396e4830532a4ba9e3c4ce361bcc Mon Sep 17 00:00:00 2001 From: BornToBeRoot <16019165+BornToBeRoot@users.noreply.github.com> Date: Sun, 22 Dec 2024 03:36:32 +0100 Subject: [PATCH 1/2] Feature: Migrate loading indicators to NETworkManager --- Source/NETworkManager/App.xaml | 15 +- Source/NETworkManager/LoadingIndicators.cs | 139 ++++++++++++++++++ Source/NETworkManager/NETworkManager.csproj | 1 - .../Styles/LoadingIndicatorArcsStyle.xaml | 110 ++++++++++++++ .../Styles/LoadingIndicatorPulseStyle.xaml | 55 +++++++ .../Views/DiscoveryProtocolView.xaml | 4 +- .../Views/IPApiDNSResolverWidgetView.xaml | 4 +- .../Views/IPApiIPGeolocationWidgetView.xaml | 4 +- .../Views/WiFiConnectDialog.xaml | 11 +- Source/NETworkManager/Views/WiFiView.xaml | 19 +-- 10 files changed, 326 insertions(+), 36 deletions(-) create mode 100644 Source/NETworkManager/LoadingIndicators.cs create mode 100644 Source/NETworkManager/Resources/Styles/LoadingIndicatorArcsStyle.xaml create mode 100644 Source/NETworkManager/Resources/Styles/LoadingIndicatorPulseStyle.xaml diff --git a/Source/NETworkManager/App.xaml b/Source/NETworkManager/App.xaml index 6c7b9ed511..ea78b717b3 100644 --- a/Source/NETworkManager/App.xaml +++ b/Source/NETworkManager/App.xaml @@ -17,21 +17,8 @@ Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.FlatButton.xaml" /> - - - - - - - - @@ -44,6 +31,8 @@ + + diff --git a/Source/NETworkManager/LoadingIndicators.cs b/Source/NETworkManager/LoadingIndicators.cs new file mode 100644 index 0000000000..1953c304fe --- /dev/null +++ b/Source/NETworkManager/LoadingIndicators.cs @@ -0,0 +1,139 @@ +using System.Windows; +using System.Windows.Controls; + +namespace NETworkManager; + +/// +/// A control featuring a range of loading indicating animations. +/// Source: https://github.com/zeluisping/LoadingIndicators.WPF +/// +[TemplatePart(Name = "Border", Type = typeof(Border))] +public class LoadingIndicator : Control +{ + /// + /// Identifies the dependency property. + /// + 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); + } + } + } + } + })); + + /// + /// Identifies the dependency property. + /// + 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; + + /// + /// Get/set the speed ratio of the animation. + /// + public double SpeedRatio + { + get { return (double)GetValue(SpeedRatioProperty); } + set { SetValue(SpeedRatioProperty, value); } + } + + /// + /// Get/set whether the loading indicator is active. + /// + public bool IsActive + { + get { return (bool)GetValue(IsActiveProperty); } + set { SetValue(IsActiveProperty, value); } + } + + /// + /// When overridden in a derived class, is invoked whenever application code + /// or internal processes call System.Windows.FrameworkElement.ApplyTemplate(). + /// + 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); + } + } + + /// + /// Initializes a new instance of the class. + /// + public LoadingIndicator() + { + + } +} diff --git a/Source/NETworkManager/NETworkManager.csproj b/Source/NETworkManager/NETworkManager.csproj index 980e8c2301..f83b5b7d27 100644 --- a/Source/NETworkManager/NETworkManager.csproj +++ b/Source/NETworkManager/NETworkManager.csproj @@ -66,7 +66,6 @@ - diff --git a/Source/NETworkManager/Resources/Styles/LoadingIndicatorArcsStyle.xaml b/Source/NETworkManager/Resources/Styles/LoadingIndicatorArcsStyle.xaml new file mode 100644 index 0000000000..c86db2d15d --- /dev/null +++ b/Source/NETworkManager/Resources/Styles/LoadingIndicatorArcsStyle.xaml @@ -0,0 +1,110 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +