From 926fdfc6792378e29171d2291e4e6f9305caa476 Mon Sep 17 00:00:00 2001 From: punker76 Date: Sat, 26 Oct 2024 21:04:52 +0200 Subject: [PATCH] refactor: use OnPropertyChange --- .editorconfig | 14 ++++++ src/AvaloniaIconPacks/Core/PackIconBase.cs | 1 + .../Core/PackIconControlBase.cs | 50 ++++++++++--------- src/AvaloniaIconPacks/PackIconBoxIcons.cs | 11 +++- 4 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..490397a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,14 @@ +; Top-most http://editorconfig.org/ file +root = true + +[*] +end_of_line = CRLF + +; 4-column tab indentation +[*.{cs,csproj,xaml,xml,props,targets}] +indent_style = space +indent_size = 4 + +[*.{md,yml}] +indent_style = space +indent_size = 2 diff --git a/src/AvaloniaIconPacks/Core/PackIconBase.cs b/src/AvaloniaIconPacks/Core/PackIconBase.cs index 509a72a..f44c400 100644 --- a/src/AvaloniaIconPacks/Core/PackIconBase.cs +++ b/src/AvaloniaIconPacks/Core/PackIconBase.cs @@ -2,6 +2,7 @@ using Windows.UI.Xaml.Controls; #elif AVALONIA using Avalonia.Controls; + #else using System.Windows.Controls; #endif diff --git a/src/AvaloniaIconPacks/Core/PackIconControlBase.cs b/src/AvaloniaIconPacks/Core/PackIconControlBase.cs index a8eeda3..dfa9c89 100644 --- a/src/AvaloniaIconPacks/Core/PackIconControlBase.cs +++ b/src/AvaloniaIconPacks/Core/PackIconControlBase.cs @@ -1,5 +1,4 @@ using System; -using System.ComponentModel; #if NETFX_CORE || WINDOWS_UWP using System.Linq; @@ -42,10 +41,8 @@ public PackIconControlBase() { this.Loaded += (sender, args) => { - this._opacityRegisterToken = - this.RegisterPropertyChangedCallback(OpacityProperty, this.CoerceSpinProperty); - this._visibilityRegisterToken = - this.RegisterPropertyChangedCallback(VisibilityProperty, this.CoerceSpinProperty); + this._opacityRegisterToken = this.RegisterPropertyChangedCallback(OpacityProperty, this.CoerceSpinProperty); + this._visibilityRegisterToken = this.RegisterPropertyChangedCallback(VisibilityProperty, this.CoerceSpinProperty); }; this.Unloaded += (sender, args) => { @@ -59,8 +56,7 @@ private void CoerceSpinProperty(DependencyObject sender, DependencyProperty dp) var packIcon = sender as PackIconControlBase; if (packIcon != null && (dp == OpacityProperty || dp == VisibilityProperty)) { - var spin = - this.Spin && packIcon.Visibility == Visibility.Visible && packIcon.SpinDuration > 0 && packIcon.Opacity > 0; + var spin = this.Spin && packIcon.Visibility == Visibility.Visible && packIcon.SpinDuration > 0 && packIcon.Opacity > 0; packIcon.ToggleSpinAnimation(spin); } } @@ -147,6 +143,7 @@ protected override void OnApplyTemplate() } #elif AVALONIA private Grid innerGrid; + private ScaleTransform scaleTransform; private RotateTransform rotateTransform; @@ -179,24 +176,24 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e) } } - protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e) + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { - if (e.Property == FlipProperty) + base.OnPropertyChanged(change); + + if (change.Property == FlipProperty) { - if (e.NewValue != null && e.NewValue != e.OldValue) + if (change.NewValue != null && change.NewValue != change.OldValue) { - this.UpdateScaleTransformation(e.GetNewValue()); + this.UpdateScaleTransformation(change.GetNewValue()); } } - else if (e.Property == RotationAngleProperty) + else if (change.Property == RotationAngleProperty) { - if (e.NewValue != null && e.NewValue != e.OldValue) + if (change.NewValue != null && change.NewValue != change.OldValue) { - this.UpdateRotateTransformation(e.GetNewValue()); + this.UpdateRotateTransformation(change.GetNewValue()); } } - - base.OnPropertyChanged(e); } private void UpdateScaleTransformation(PackIconFlipOrientation flipOrientation) @@ -282,8 +279,12 @@ public static readonly StyledProperty RotationAngleProperty null, (packIcon, value) => { - var val = (double)value; - return val < 0 ? 0d : (val > 360 ? 360d : value); + if (value < 0) + { + return 0d; + } + + return value > 360 ? 360d : value; }); #else public static readonly DependencyProperty RotationAngleProperty @@ -294,7 +295,12 @@ public static readonly DependencyProperty RotationAngleProperty new PropertyMetadata(0d, null, (dependencyObject, value) => { var val = (double)value; - return val < 0 ? 0d : (val > 360 ? 360d : value); + if (val < 0) + { + return 0d; + } + + return val > 360 ? 360d : val; })); #endif @@ -497,11 +503,7 @@ public static readonly StyledProperty SpinDurationProperty false, BindingMode.OneWay, null, - (iconPack, value) => - { - var val = (double)value; - return val < 0 ? 0d : value; - }); + (iconPack, value) => value < 0 ? 0d : value); #else public static readonly DependencyProperty SpinDurationProperty = DependencyProperty.Register( diff --git a/src/AvaloniaIconPacks/PackIconBoxIcons.cs b/src/AvaloniaIconPacks/PackIconBoxIcons.cs index 8cf50e4..8d3f17b 100644 --- a/src/AvaloniaIconPacks/PackIconBoxIcons.cs +++ b/src/AvaloniaIconPacks/PackIconBoxIcons.cs @@ -52,9 +52,16 @@ public PackIconBoxIcons() this.DefaultStyleKey = typeof(PackIconBoxIcons); } #elif AVALONIA - public PackIconBoxIcons() + // We override OnPropertyChanged of the base class. That way we can react on property changes + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { - this.GetObservable(KindProperty).Subscribe(_ => UpdateData()); + base.OnPropertyChanged(change); + + // if the changed property is the NumberOfStarsProperty, we need to update the stars + if (change.Property == KindProperty) + { + UpdateData(); + } } #else static PackIconBoxIcons()