Browse Source

Merge pull request #3642 from MarchingCube/optimize-affects-render

Avoid IAffectsRender subscribe attempts on values that can't implement it
pull/3646/head
Jumar Macato 7 years ago
committed by GitHub
parent
commit
40e853d6d8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      src/Avalonia.Visuals/AvaloniaPropertyExtensions.cs
  2. 4
      src/Avalonia.Visuals/Media/Brush.cs
  3. 20
      src/Avalonia.Visuals/Media/Pen.cs
  4. 22
      src/Avalonia.Visuals/Visual.cs

27
src/Avalonia.Visuals/AvaloniaPropertyExtensions.cs

@ -0,0 +1,27 @@
using Avalonia.Media;
#nullable enable
namespace Avalonia
{
/// <summary>
/// Extensions for <see cref="AvaloniaProperty"/>.
/// </summary>
public static class AvaloniaPropertyExtensions
{
/// <summary>
/// Checks if values of given property can affect rendering (via <see cref="IAffectsRender"/>).
/// </summary>
/// <param name="property">Property to check.</param>
public static bool CanValueAffectRender(this AvaloniaProperty property)
{
var propertyType = property.PropertyType;
// Only case that we are sure that property value CAN'T affect render are sealed types that don't implement
// the interface.
var cannotAffectRender = propertyType.IsSealed && !typeof(IAffectsRender).IsAssignableFrom(propertyType);
return !cannotAffectRender;
}
}
}

4
src/Avalonia.Visuals/Media/Brush.cs

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

20
src/Avalonia.Visuals/Media/Pen.cs

@ -2,7 +2,6 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Collections.Generic;
using Avalonia.Media.Immutable;
using Avalonia.Utilities;
@ -197,7 +196,15 @@ namespace Avalonia.Media
protected static void AffectsRender<T>(params AvaloniaProperty[] properties)
where T : Pen
{
void Invalidate(AvaloniaPropertyChangedEventArgs e)
static void Invalidate(AvaloniaPropertyChangedEventArgs e)
{
if (e.Sender is T sender)
{
sender.RaiseInvalidated(EventArgs.Empty);
}
}
static void InvalidateAndSubscribe(AvaloniaPropertyChangedEventArgs e)
{
if (e.Sender is T sender)
{
@ -223,7 +230,14 @@ namespace Avalonia.Media
foreach (var property in properties)
{
property.Changed.Subscribe(Invalidate);
if (property.CanValueAffectRender())
{
property.Changed.Subscribe(e => InvalidateAndSubscribe(e));
}
else
{
property.Changed.Subscribe(e => Invalidate(e));
}
}
}

22
src/Avalonia.Visuals/Visual.cs

@ -3,7 +3,6 @@
using System;
using System.Collections.Specialized;
using System.Linq;
using Avalonia.Collections;
using Avalonia.Data;
using Avalonia.Logging;
@ -336,7 +335,15 @@ namespace Avalonia
protected static void AffectsRender<T>(params AvaloniaProperty[] properties)
where T : Visual
{
void Invalidate(AvaloniaPropertyChangedEventArgs e)
static void Invalidate(AvaloniaPropertyChangedEventArgs e)
{
if (e.Sender is T sender)
{
sender.InvalidateVisual();
}
}
static void InvalidateAndSubscribe(AvaloniaPropertyChangedEventArgs e)
{
if (e.Sender is T sender)
{
@ -347,7 +354,7 @@ namespace Avalonia
if (e.NewValue is IAffectsRender newValue)
{
WeakEventHandlerManager.Subscribe<IAffectsRender, EventArgs, T>(newValue, nameof(newValue.Invalidated), sender.AffectsRenderInvalidated);
WeakEventHandlerManager.Subscribe<IAffectsRender, EventArgs, T>(newValue, nameof(newValue.Invalidated), sender.AffectsRenderInvalidated);
}
sender.InvalidateVisual();
@ -356,7 +363,14 @@ namespace Avalonia
foreach (var property in properties)
{
property.Changed.Subscribe(Invalidate);
if (property.CanValueAffectRender())
{
property.Changed.Subscribe(e => InvalidateAndSubscribe(e));
}
else
{
property.Changed.Subscribe(e => Invalidate(e));
}
}
}

Loading…
Cancel
Save