Skip to content

Commit

Permalink
refactor: use OnPropertyChange
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed Oct 26, 2024
1 parent 5d1edaa commit 926fdfc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/AvaloniaIconPacks/Core/PackIconBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Windows.UI.Xaml.Controls;
#elif AVALONIA
using Avalonia.Controls;

#else
using System.Windows.Controls;
#endif
Expand Down
50 changes: 26 additions & 24 deletions src/AvaloniaIconPacks/Core/PackIconControlBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.ComponentModel;

#if NETFX_CORE || WINDOWS_UWP
using System.Linq;
Expand Down Expand Up @@ -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) =>
{
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -147,6 +143,7 @@ protected override void OnApplyTemplate()
}
#elif AVALONIA
private Grid innerGrid;

private ScaleTransform scaleTransform;
private RotateTransform rotateTransform;

Expand Down Expand Up @@ -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<PackIconFlipOrientation>());
this.UpdateScaleTransformation(change.GetNewValue<PackIconFlipOrientation>());
}
}
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<double>());
this.UpdateRotateTransformation(change.GetNewValue<double>());
}
}

base.OnPropertyChanged(e);
}

private void UpdateScaleTransformation(PackIconFlipOrientation flipOrientation)
Expand Down Expand Up @@ -282,8 +279,12 @@ public static readonly StyledProperty<double> 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
Expand All @@ -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

Expand Down Expand Up @@ -497,11 +503,7 @@ public static readonly StyledProperty<double> 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(
Expand Down
11 changes: 9 additions & 2 deletions src/AvaloniaIconPacks/PackIconBoxIcons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 926fdfc

Please sign in to comment.