Browse Source

Merge branch 'master' of https://github.com/AvaloniaUI/Avalonia

pull/4069/head
JamRemco 6 years ago
parent
commit
ad7eb43097
  1. 16
      src/Avalonia.Animation/Animatable.cs
  2. 8
      src/Avalonia.Base/Data/Core/Plugins/InpcPropertyAccessorPlugin.cs
  3. 65
      tests/Avalonia.Animation.UnitTests/AnimatableTests.cs
  4. 57
      tests/Avalonia.Benchmarks/Data/PropertyAccessorBenchmarks.cs

16
src/Avalonia.Animation/Animatable.cs

@ -93,17 +93,17 @@ namespace Avalonia.Animation
var oldTransitions = change.OldValue.GetValueOrDefault<Transitions>();
var newTransitions = change.NewValue.GetValueOrDefault<Transitions>();
if (oldTransitions is object)
{
oldTransitions.CollectionChanged -= TransitionsCollectionChanged;
RemoveTransitions(oldTransitions);
}
if (newTransitions is object)
{
newTransitions.CollectionChanged += TransitionsCollectionChanged;
AddTransitions(newTransitions);
}
if (oldTransitions is object)
{
oldTransitions.CollectionChanged -= TransitionsCollectionChanged;
RemoveTransitions(oldTransitions);
}
}
else if (_transitionsEnabled &&
Transitions is object &&
@ -111,8 +111,10 @@ namespace Avalonia.Animation
!change.Property.IsDirect &&
change.Priority > BindingPriority.Animation)
{
foreach (var transition in Transitions)
for (var i = Transitions.Count -1; i >= 0; --i)
{
var transition = Transitions[i];
if (transition.Property == change.Property)
{
var state = _transitionState[transition];

8
src/Avalonia.Base/Data/Core/Plugins/InpcPropertyAccessorPlugin.cs

@ -1,7 +1,5 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Reflection;
using Avalonia.Utilities;
@ -31,7 +29,11 @@ namespace Avalonia.Data.Core.Plugins
Contract.Requires<ArgumentNullException>(propertyName != null);
reference.TryGetTarget(out object instance);
var p = instance.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name == propertyName);
const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance;
var p = instance.GetType().GetProperty(propertyName, bindingFlags);
if (p != null)
{

65
tests/Avalonia.Animation.UnitTests/AnimatableTests.cs

@ -2,6 +2,7 @@
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Moq;
@ -265,6 +266,70 @@ namespace Avalonia.Animation.UnitTests
}
}
[Fact]
public void Replacing_Transitions_During_Animation_Does_Not_Throw_KeyNotFound()
{
// Issue #4059
using (UnitTestApplication.Start(TestServices.RealStyler))
{
Border target;
var clock = new TestClock();
var root = new TestRoot
{
Clock = clock,
Styles =
{
new Style(x => x.OfType<Border>())
{
Setters =
{
new Setter(Border.TransitionsProperty,
new Transitions
{
new DoubleTransition
{
Property = Border.OpacityProperty,
Duration = TimeSpan.FromSeconds(1),
},
}),
},
},
new Style(x => x.OfType<Border>().Class("foo"))
{
Setters =
{
new Setter(Border.TransitionsProperty,
new Transitions
{
new DoubleTransition
{
Property = Border.OpacityProperty,
Duration = TimeSpan.FromSeconds(1),
},
}),
new Setter(Border.OpacityProperty, 0.0),
},
},
},
Child = target = new Border
{
Background = Brushes.Red,
}
};
root.Measure(Size.Infinity);
root.Arrange(new Rect(root.DesiredSize));
target.Classes.Add("foo");
clock.Step(TimeSpan.FromSeconds(0));
clock.Step(TimeSpan.FromSeconds(0.5));
Assert.Equal(0.5, target.Opacity);
target.Classes.Remove("foo");
}
}
private static Mock<ITransition> CreateTarget()
{
return CreateTransition(Visual.OpacityProperty);

57
tests/Avalonia.Benchmarks/Data/PropertyAccessorBenchmarks.cs

@ -0,0 +1,57 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Data.Core.Plugins;
using BenchmarkDotNet.Attributes;
using JetBrains.Annotations;
namespace Avalonia.Benchmarks.Data
{
[MemoryDiagnoser, InProcess]
public class PropertyAccessorBenchmarks
{
private readonly InpcPropertyAccessorPlugin _plugin = new InpcPropertyAccessorPlugin();
private readonly TestObject _targetStrongRef = new TestObject();
private readonly WeakReference<object> _targetWeakRef;
public PropertyAccessorBenchmarks()
{
_targetWeakRef = new WeakReference<object>(_targetStrongRef);
}
[Benchmark]
public void InpcAccessor()
{
_plugin.Start(_targetWeakRef, nameof(TestObject.Test));
}
private class TestObject : INotifyPropertyChanged
{
private string _test;
public string Test
{
get => _test;
set
{
if (_test == value)
{
return;
}
_test = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Loading…
Cancel
Save