Browse Source

Reuse observer instance where possible

pull/9749/head
Max Katz 3 years ago
parent
commit
ccffa6acab
  1. 16
      src/Avalonia.Base/Layout/Layoutable.cs
  2. 8
      src/Avalonia.Base/Media/Brush.cs
  3. 10
      src/Avalonia.Base/Media/DashStyle.cs
  4. 8
      src/Avalonia.Base/Media/ExperimentalAcrylicMaterial.cs
  5. 3
      src/Avalonia.Base/Media/Geometry.cs
  6. 12
      src/Avalonia.Base/Media/ScaleTransform.cs
  7. 12
      src/Avalonia.Base/Media/SkewTransform.cs
  8. 12
      src/Avalonia.Base/Media/TranslateTransform.cs
  9. 8
      src/Avalonia.Controls/DefinitionBase.cs
  10. 8
      src/Avalonia.Controls/Panel.cs

16
src/Avalonia.Base/Layout/Layoutable.cs

@ -470,14 +470,12 @@ namespace Avalonia.Layout
protected static void AffectsMeasure<T>(params AvaloniaProperty[] properties) protected static void AffectsMeasure<T>(params AvaloniaProperty[] properties)
where T : Layoutable where T : Layoutable
{ {
void Invalidate(AvaloniaPropertyChangedEventArgs e) var invalidateObserver = new AnonymousObserver<AvaloniaPropertyChangedEventArgs>(
{ static e => (e.Sender as T)?.InvalidateMeasure());
(e.Sender as T)?.InvalidateMeasure();
}
foreach (var property in properties) foreach (var property in properties)
{ {
property.Changed.Subscribe(Invalidate); property.Changed.Subscribe(invalidateObserver);
} }
} }
@ -493,14 +491,12 @@ namespace Avalonia.Layout
protected static void AffectsArrange<T>(params AvaloniaProperty[] properties) protected static void AffectsArrange<T>(params AvaloniaProperty[] properties)
where T : Layoutable where T : Layoutable
{ {
void Invalidate(AvaloniaPropertyChangedEventArgs e) var invalidate = new AnonymousObserver<AvaloniaPropertyChangedEventArgs>(
{ static e => (e.Sender as T)?.InvalidateArrange());
(e.Sender as T)?.InvalidateArrange();
}
foreach (var property in properties) foreach (var property in properties)
{ {
property.Changed.Subscribe(Invalidate); property.Changed.Subscribe(invalidate);
} }
} }

8
src/Avalonia.Base/Media/Brush.cs

@ -104,14 +104,12 @@ namespace Avalonia.Media
protected static void AffectsRender<T>(params AvaloniaProperty[] properties) protected static void AffectsRender<T>(params AvaloniaProperty[] properties)
where T : Brush where T : Brush
{ {
static void Invalidate(AvaloniaPropertyChangedEventArgs e) var invalidateObserver = new AnonymousObserver<AvaloniaPropertyChangedEventArgs>(
{ static e => (e.Sender as T)?.RaiseInvalidated(EventArgs.Empty));
(e.Sender as T)?.RaiseInvalidated(EventArgs.Empty);
}
foreach (var property in properties) foreach (var property in properties)
{ {
property.Changed.Subscribe(e => Invalidate(e)); property.Changed.Subscribe(invalidateObserver);
} }
} }

10
src/Avalonia.Base/Media/DashStyle.cs

@ -52,13 +52,11 @@ namespace Avalonia.Media
static DashStyle() static DashStyle()
{ {
void RaiseInvalidated(AvaloniaPropertyChangedEventArgs e) var invalidateObserver = new AnonymousObserver<AvaloniaPropertyChangedEventArgs>(
{ static e => ((DashStyle)e.Sender).Invalidated?.Invoke(e.Sender, EventArgs.Empty));
((DashStyle)e.Sender).Invalidated?.Invoke(e.Sender, EventArgs.Empty);
}
DashesProperty.Changed.Subscribe(RaiseInvalidated); DashesProperty.Changed.Subscribe(invalidateObserver);
OffsetProperty.Changed.Subscribe(RaiseInvalidated); OffsetProperty.Changed.Subscribe(invalidateObserver);
} }
/// <summary> /// <summary>

8
src/Avalonia.Base/Media/ExperimentalAcrylicMaterial.cs

@ -275,14 +275,12 @@ namespace Avalonia.Media
protected static void AffectsRender<T>(params AvaloniaProperty[] properties) protected static void AffectsRender<T>(params AvaloniaProperty[] properties)
where T : ExperimentalAcrylicMaterial where T : ExperimentalAcrylicMaterial
{ {
static void Invalidate(AvaloniaPropertyChangedEventArgs e) var invalidateObserver = new AnonymousObserver<AvaloniaPropertyChangedEventArgs>(
{ static e => (e.Sender as T)?.RaiseInvalidated(EventArgs.Empty));
(e.Sender as T)?.RaiseInvalidated(EventArgs.Empty);
}
foreach (var property in properties) foreach (var property in properties)
{ {
property.Changed.Subscribe(e => Invalidate(e)); property.Changed.Subscribe(invalidateObserver);
} }
} }

3
src/Avalonia.Base/Media/Geometry.cs

@ -118,9 +118,10 @@ namespace Avalonia.Media
/// </remarks> /// </remarks>
protected static void AffectsGeometry(params AvaloniaProperty[] properties) protected static void AffectsGeometry(params AvaloniaProperty[] properties)
{ {
var invalidateObserver = new AnonymousObserver<AvaloniaPropertyChangedEventArgs>(AffectsGeometryInvalidate);
foreach (var property in properties) foreach (var property in properties)
{ {
property.Changed.Subscribe(AffectsGeometryInvalidate); property.Changed.Subscribe(invalidateObserver);
} }
} }

12
src/Avalonia.Base/Media/ScaleTransform.cs

@ -26,8 +26,6 @@ namespace Avalonia.Media
/// </summary> /// </summary>
public ScaleTransform() public ScaleTransform()
{ {
this.GetObservable(ScaleXProperty).Subscribe(_ => RaiseChanged());
this.GetObservable(ScaleYProperty).Subscribe(_ => RaiseChanged());
} }
/// <summary> /// <summary>
@ -64,5 +62,15 @@ namespace Avalonia.Media
/// Gets the transform's <see cref="Matrix"/>. /// Gets the transform's <see cref="Matrix"/>.
/// </summary> /// </summary>
public override Matrix Value => Matrix.CreateScale(ScaleX, ScaleY); public override Matrix Value => Matrix.CreateScale(ScaleX, ScaleY);
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == ScaleXProperty || change.Property == ScaleYProperty)
{
RaiseChanged();
}
}
} }
} }

12
src/Avalonia.Base/Media/SkewTransform.cs

@ -26,8 +26,6 @@ namespace Avalonia.Media
/// </summary> /// </summary>
public SkewTransform() public SkewTransform()
{ {
this.GetObservable(AngleXProperty).Subscribe(_ => RaiseChanged());
this.GetObservable(AngleYProperty).Subscribe(_ => RaiseChanged());
} }
/// <summary> /// <summary>
@ -63,5 +61,15 @@ namespace Avalonia.Media
/// Gets the transform's <see cref="Matrix"/>. /// Gets the transform's <see cref="Matrix"/>.
/// </summary> /// </summary>
public override Matrix Value => Matrix.CreateSkew(Matrix.ToRadians(AngleX), Matrix.ToRadians(AngleY)); public override Matrix Value => Matrix.CreateSkew(Matrix.ToRadians(AngleX), Matrix.ToRadians(AngleY));
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == AngleXProperty || change.Property == AngleYProperty)
{
RaiseChanged();
}
}
} }
} }

12
src/Avalonia.Base/Media/TranslateTransform.cs

@ -26,8 +26,6 @@ namespace Avalonia.Media
/// </summary> /// </summary>
public TranslateTransform() public TranslateTransform()
{ {
this.GetObservable(XProperty).Subscribe(_ => RaiseChanged());
this.GetObservable(YProperty).Subscribe(_ => RaiseChanged());
} }
/// <summary> /// <summary>
@ -64,5 +62,15 @@ namespace Avalonia.Media
/// Gets the transform's <see cref="Matrix"/>. /// Gets the transform's <see cref="Matrix"/>.
/// </summary> /// </summary>
public override Matrix Value => Matrix.CreateTranslation(X, Y); public override Matrix Value => Matrix.CreateTranslation(X, Y);
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == XProperty || change.Property == YProperty)
{
RaiseChanged();
}
}
} }
} }

8
src/Avalonia.Controls/DefinitionBase.cs

@ -806,14 +806,12 @@ namespace Avalonia.Controls
/// <param name="properties">The properties.</param> /// <param name="properties">The properties.</param>
protected static void AffectsParentMeasure(params AvaloniaProperty[] properties) protected static void AffectsParentMeasure(params AvaloniaProperty[] properties)
{ {
void Invalidate(AvaloniaPropertyChangedEventArgs e) var invalidateObserver = new AnonymousObserver<AvaloniaPropertyChangedEventArgs>(
{ static e => (e.Sender as DefinitionBase)?.Parent?.InvalidateMeasure());
(e.Sender as DefinitionBase)?.Parent?.InvalidateMeasure();
}
foreach (var property in properties) foreach (var property in properties)
{ {
property.Changed.Subscribe(Invalidate); property.Changed.Subscribe(invalidateObserver);
} }
} }
} }

8
src/Avalonia.Controls/Panel.cs

@ -87,9 +87,11 @@ namespace Avalonia.Controls
protected static void AffectsParentArrange<TPanel>(params AvaloniaProperty[] properties) protected static void AffectsParentArrange<TPanel>(params AvaloniaProperty[] properties)
where TPanel : Panel where TPanel : Panel
{ {
var invalidateObserver = new AnonymousObserver<AvaloniaPropertyChangedEventArgs>(
static e => AffectsParentArrangeInvalidate<TPanel>(e));
foreach (var property in properties) foreach (var property in properties)
{ {
property.Changed.Subscribe(AffectsParentArrangeInvalidate<TPanel>); property.Changed.Subscribe(invalidateObserver);
} }
} }
@ -100,9 +102,11 @@ namespace Avalonia.Controls
protected static void AffectsParentMeasure<TPanel>(params AvaloniaProperty[] properties) protected static void AffectsParentMeasure<TPanel>(params AvaloniaProperty[] properties)
where TPanel : Panel where TPanel : Panel
{ {
var invalidateObserver = new AnonymousObserver<AvaloniaPropertyChangedEventArgs>(
static e => AffectsParentMeasureInvalidate<TPanel>(e));
foreach (var property in properties) foreach (var property in properties)
{ {
property.Changed.Subscribe(AffectsParentMeasureInvalidate<TPanel>); property.Changed.Subscribe(invalidateObserver);
} }
} }

Loading…
Cancel
Save