Skip to content

Commit c65c0b1

Browse files
committed
Added notification for property changes to implicit animations
Used registered callbacks to avoid the overhead in explicit animations. This way the callbacks and events are only present and used when an implicit animation is actually used, since explicit ones use a new AnimationBuilder instance every time anyway, so notification is not necessary.
1 parent 14cbe9f commit c65c0b1

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

Microsoft.Toolkit.Uwp.UI.Animations/Xaml/Abstract/Animation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ public EasingMode? EasingMode
9191
/// </summary>
9292
public RepeatOption Repeat
9393
{
94-
get => (RepeatOption)GetValue(RepeatOptionProperty);
95-
set => SetValue(RepeatOptionProperty, value);
94+
get => (RepeatOption)GetValue(RepeatProperty);
95+
set => SetValue(RepeatProperty, value);
9696
}
9797

9898
/// <summary>
9999
/// Identifies the <seealso cref="Repeat"/> dependency property.
100100
/// </summary>
101-
public static readonly DependencyProperty RepeatOptionProperty = DependencyProperty.Register(
101+
public static readonly DependencyProperty RepeatProperty = DependencyProperty.Register(
102102
nameof(Repeat),
103103
typeof(RepeatOption),
104104
typeof(Animation),

Microsoft.Toolkit.Uwp.UI.Animations/Xaml/Abstract/ImplicitAnimation{TValue,TKeyFrame}.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#nullable enable
66

7+
using System;
78
using Windows.UI.Composition;
89
using Windows.UI.Xaml;
910
using static Microsoft.Toolkit.Uwp.UI.Animations.AnimationExtensions;
@@ -14,9 +15,35 @@ namespace Microsoft.Toolkit.Uwp.UI.Animations
1415
/// A base model representing a typed animation that can be used as an implicit composition animation.
1516
/// </summary>
1617
/// <inheritdoc cref="Animation{TValue, TKeyFrame}"/>
17-
public abstract class ImplicitAnimation<TValue, TKeyFrame> : Animation<TValue, TKeyFrame>, IImplicitTimeline
18+
public abstract class ImplicitAnimation<TValue, TKeyFrame> : Animation<TValue, TKeyFrame>, IImplicitTimeline, IInternalImplicitAnimation
1819
where TKeyFrame : unmanaged
1920
{
21+
/// <inheritdoc cref="IInternalImplicitAnimation.AnimationPropertyChanged"/>
22+
private event EventHandler? AnimationPropertyChanged;
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="ImplicitAnimation{TValue, TKeyFrame}"/> class.
26+
/// </summary>
27+
protected ImplicitAnimation()
28+
{
29+
RegisterPropertyChangedCallback(DelayProperty, RaiseAnimationPropertyChanged);
30+
RegisterPropertyChangedCallback(DurationProperty, RaiseAnimationPropertyChanged);
31+
RegisterPropertyChangedCallback(EasingTypeProperty, RaiseAnimationPropertyChanged);
32+
RegisterPropertyChangedCallback(EasingModeProperty, RaiseAnimationPropertyChanged);
33+
RegisterPropertyChangedCallback(RepeatProperty, RaiseAnimationPropertyChanged);
34+
RegisterPropertyChangedCallback(DelayBehaviorProperty, RaiseAnimationPropertyChanged);
35+
RegisterPropertyChangedCallback(ToProperty, RaiseAnimationPropertyChanged);
36+
RegisterPropertyChangedCallback(FromProperty, RaiseAnimationPropertyChanged);
37+
RegisterPropertyChangedCallback(KeyFramesProperty, RaiseAnimationPropertyChanged);
38+
}
39+
40+
/// <inheritdoc/>
41+
event EventHandler? IInternalImplicitAnimation.AnimationPropertyChanged
42+
{
43+
add => AnimationPropertyChanged += value;
44+
remove => AnimationPropertyChanged -= value;
45+
}
46+
2047
/// <summary>
2148
/// Gets or sets the optional implicit target for the animation. This can act as a trigger property for the animation.
2249
/// </summary>
@@ -67,5 +94,15 @@ public CompositionAnimation GetAnimation(UIElement element, out string? target)
6794

6895
return builder.GetAnimation(element.GetVisual(), out _);
6996
}
97+
98+
/// <summary>
99+
/// Raises <see cref="AnimationPropertyChanged"/> event.
100+
/// </summary>
101+
/// <param name="sender">The instance raising the event.</param>
102+
/// <param name="property">The <see cref="DependencyProperty"/> that was changed.</param>
103+
private void RaiseAnimationPropertyChanged(DependencyObject sender, DependencyProperty property)
104+
{
105+
AnimationPropertyChanged?.Invoke(this, EventArgs.Empty);
106+
}
70107
}
71108
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
7+
#nullable enable
8+
9+
namespace Microsoft.Toolkit.Uwp.UI.Animations
10+
{
11+
/// <summary>
12+
/// An interface adding notification support to implicit animations. This is needed to
13+
/// avoid the type parameters when the event is subscribed to from <see cref="ImplicitAnimationSet"/>.
14+
/// </summary>
15+
internal interface IInternalImplicitAnimation
16+
{
17+
/// <summary>
18+
/// Raised whenever a property that influences the animation changes.
19+
/// </summary>
20+
event EventHandler? AnimationPropertyChanged;
21+
}
22+
}

0 commit comments

Comments
 (0)