From c872cc005d37529017f7311d5c180708e73ad9b2 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 30 Apr 2020 15:15:31 +0200 Subject: [PATCH] Only call property notify on effective value change. --- src/Avalonia.Base/AvaloniaObject.cs | 10 ++++++++-- .../AvaloniaPropertyTests.cs | 20 ++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Base/AvaloniaObject.cs b/src/Avalonia.Base/AvaloniaObject.cs index 546ab78e31..f387d7e0b6 100644 --- a/src/Avalonia.Base/AvaloniaObject.cs +++ b/src/Avalonia.Base/AvaloniaObject.cs @@ -716,7 +716,10 @@ namespace Avalonia { VerifyAccess(); - change.Property.Notifying?.Invoke(this, true); + if (change.IsEffectiveValueChange) + { + change.Property.Notifying?.Invoke(this, true); + } try { @@ -747,7 +750,10 @@ namespace Avalonia } finally { - change.Property.Notifying?.Invoke(this, false); + if (change.IsEffectiveValueChange) + { + change.Property.Notifying?.Invoke(this, false); + } } } diff --git a/tests/Avalonia.Base.UnitTests/AvaloniaPropertyTests.cs b/tests/Avalonia.Base.UnitTests/AvaloniaPropertyTests.cs index 3819e715f3..d7f927372e 100644 --- a/tests/Avalonia.Base.UnitTests/AvaloniaPropertyTests.cs +++ b/tests/Avalonia.Base.UnitTests/AvaloniaPropertyTests.cs @@ -102,6 +102,17 @@ namespace Avalonia.Base.UnitTests Assert.Equal(new[] { "animated" }, result); } + [Fact] + public void Notify_Fired_Only_On_Effective_Value_Change() + { + var target = new Class1(); + + target.SetValue(Class1.FooProperty, "animated", BindingPriority.Animation); + target.SetValue(Class1.FooProperty, "local"); + + Assert.Equal(2, target.NotifyCount); + } + [Fact] public void Property_Equals_Should_Handle_Null() { @@ -180,7 +191,14 @@ namespace Avalonia.Base.UnitTests private class Class1 : AvaloniaObject { public static readonly StyledProperty FooProperty = - AvaloniaProperty.Register("Foo", "default"); + AvaloniaProperty.Register("Foo", "default", notifying: FooNotifying); + + public int NotifyCount { get; private set; } + + private static void FooNotifying(IAvaloniaObject o, bool n) + { + ++((Class1)o).NotifyCount; + } } private class Class2 : Class1