Skip to content

Animations package improvements #3796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private sealed record AnimationFactory<T>(
T? From,
TimeSpan Delay,
TimeSpan Duration,
RepeatOption Repeat,
EasingType EasingType,
EasingMode EasingMode)
: ICompositionAnimationFactory, IXamlAnimationFactory
Expand All @@ -70,6 +71,7 @@ private sealed record AnimationFactory<T>(
public CompositionAnimation GetAnimation(CompositionObject targetHint, out CompositionObject? target)
{
CompositionEasingFunction? easingFunction = targetHint.Compositor.TryCreateEasingFunction(EasingType, EasingMode);
(AnimationIterationBehavior iterationBehavior, int iterationCount) = Repeat.ToBehaviorAndCount();

target = null;

Expand All @@ -80,7 +82,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
GetToAs<bool>(),
GetFromAs<bool>(),
Delay,
Duration);
Duration,
iterationBehavior: iterationBehavior,
iterationCount: iterationCount);
}

if (typeof(T) == typeof(float))
Expand All @@ -91,7 +95,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
GetFromAs<float>(),
Delay,
Duration,
easingFunction);
easingFunction,
iterationBehavior: iterationBehavior,
iterationCount: iterationCount);
}

if (typeof(T) == typeof(double))
Expand All @@ -102,7 +108,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
(float?)GetFromAs<double>(),
Delay,
Duration,
easingFunction);
easingFunction,
iterationBehavior: iterationBehavior,
iterationCount: iterationCount);
}

if (typeof(T) == typeof(Vector2))
Expand All @@ -113,7 +121,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
GetFromAs<Vector2>(),
Delay,
Duration,
easingFunction);
easingFunction,
iterationBehavior: iterationBehavior,
iterationCount: iterationCount);
}

if (typeof(T) == typeof(Vector3))
Expand All @@ -124,7 +134,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
GetFromAs<Vector3>(),
Delay,
Duration,
easingFunction);
easingFunction,
iterationBehavior: iterationBehavior,
iterationCount: iterationCount);
}

if (typeof(T) == typeof(Vector4))
Expand All @@ -135,7 +147,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
GetFromAs<Vector4>(),
Delay,
Duration,
easingFunction);
easingFunction,
iterationBehavior: iterationBehavior,
iterationCount: iterationCount);
}

if (typeof(T) == typeof(Color))
Expand All @@ -146,7 +160,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
GetFromAs<Color>(),
Delay,
Duration,
easingFunction);
easingFunction,
iterationBehavior: iterationBehavior,
iterationCount: iterationCount);
}

if (typeof(T) == typeof(Quaternion))
Expand All @@ -157,7 +173,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
GetFromAs<Quaternion>(),
Delay,
Duration,
easingFunction);
easingFunction,
iterationBehavior: iterationBehavior,
iterationCount: iterationCount);
}

throw new InvalidOperationException("Invalid animation type");
Expand All @@ -177,6 +195,7 @@ public Timeline GetAnimation(DependencyObject targetHint)
Delay,
Duration,
easingFunction,
Repeat.ToRepeatBehavior(),
enableDependecyAnimations: true);
}

Expand All @@ -189,6 +208,7 @@ public Timeline GetAnimation(DependencyObject targetHint)
Delay,
Duration,
easingFunction,
Repeat.ToRepeatBehavior(),
enableDependecyAnimations: true);
}

Expand All @@ -201,6 +221,7 @@ public Timeline GetAnimation(DependencyObject targetHint)
Delay,
Duration,
easingFunction,
Repeat.ToRepeatBehavior(),
enableDependecyAnimations: true);
}

Expand All @@ -212,7 +233,8 @@ public Timeline GetAnimation(DependencyObject targetHint)
GetFromAs<Color>(),
Delay,
Duration,
easingFunction);
easingFunction,
Repeat.ToRepeatBehavior());
}

throw new InvalidOperationException("Invalid animation type");
Expand Down Expand Up @@ -287,6 +309,7 @@ private sealed record CompositionClipScalarAnimation(
float? From,
TimeSpan Delay,
TimeSpan Duration,
RepeatOption Repeat,
EasingType EasingType,
EasingMode EasingMode)
: ICompositionAnimationFactory
Expand All @@ -297,7 +320,16 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
Visual visual = (Visual)targetHint;
InsetClip clip = visual.Clip as InsetClip ?? (InsetClip)(visual.Clip = visual.Compositor.CreateInsetClip());
CompositionEasingFunction? easingFunction = clip.Compositor.TryCreateEasingFunction(EasingType, EasingMode);
ScalarKeyFrameAnimation animation = clip.Compositor.CreateScalarKeyFrameAnimation(Property, To, From, Delay, Duration, easingFunction);
(AnimationIterationBehavior iterationBehavior, int iterationCount) = Repeat.ToBehaviorAndCount();
ScalarKeyFrameAnimation animation = clip.Compositor.CreateScalarKeyFrameAnimation(
Property,
To,
From,
Delay,
Duration,
easingFunction,
iterationBehavior: iterationBehavior,
iterationCount: iterationCount);

target = clip;

Expand All @@ -314,6 +346,7 @@ private sealed record XamlTransformDoubleAnimationFactory(
double? From,
TimeSpan Delay,
TimeSpan Duration,
RepeatOption Repeat,
EasingType EasingType,
EasingMode EasingMode)
: IXamlAnimationFactory
Expand All @@ -328,7 +361,14 @@ public Timeline GetAnimation(DependencyObject targetHint)
element.RenderTransform = transform = new CompositeTransform();
}

return transform.CreateDoubleAnimation(Property, To, From, Duration, Delay, EasingType.ToEasingFunction(EasingMode));
return transform.CreateDoubleAnimation(
Property,
To,
From,
Duration,
Delay,
EasingType.ToEasingFunction(EasingMode),
Repeat.ToRepeatBehavior());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public IPropertyAnimationBuilder<Vector2> Size()
/// <param name="delay">The optional initial delay for the animation.</param>
/// <param name="duration">The animation duration.</param>
/// <param name="repeatOption">The repeat option for the animation (defaults to one iteration).</param>
/// <param name="delayBehavior">The delay behavior to use (ignored if <paramref name="layer"/> is <see cref="FrameworkLayer.Xaml"/>).</param>
/// <param name="layer">The target framework layer to animate.</param>
/// <returns>The current <see cref="AnimationBuilder"/> instance.</returns>
public AnimationBuilder NormalizedKeyFrames<T>(
Expand All @@ -244,6 +245,7 @@ public AnimationBuilder NormalizedKeyFrames<T>(
TimeSpan? delay = null,
TimeSpan? duration = null,
RepeatOption? repeatOption = null,
AnimationDelayBehavior? delayBehavior = null,
FrameworkLayer layer = FrameworkLayer.Composition)
where T : unmanaged
{
Expand All @@ -253,7 +255,8 @@ public AnimationBuilder NormalizedKeyFrames<T>(
property,
delay,
duration ?? DefaultDuration,
repeatOption ?? RepeatOption.Once);
repeatOption ?? RepeatOption.Once,
delayBehavior ?? DefaultDelayBehavior);

build(builder);

Expand Down Expand Up @@ -286,6 +289,7 @@ public AnimationBuilder NormalizedKeyFrames<T>(
/// <param name="delay">The optional initial delay for the animation.</param>
/// <param name="duration">The animation duration.</param>
/// <param name="repeatOption">The repeat option for the animation (defaults to one iteration).</param>
/// <param name="delayBehavior">The delay behavior to use (ignored if <paramref name="layer"/> is <see cref="FrameworkLayer.Xaml"/>).</param>
/// <param name="layer">The target framework layer to animate.</param>
/// <returns>The current <see cref="AnimationBuilder"/> instance.</returns>
public AnimationBuilder NormalizedKeyFrames<T, TState>(
Expand All @@ -295,6 +299,7 @@ public AnimationBuilder NormalizedKeyFrames<T, TState>(
TimeSpan? delay = null,
TimeSpan? duration = null,
RepeatOption? repeatOption = null,
AnimationDelayBehavior? delayBehavior = null,
FrameworkLayer layer = FrameworkLayer.Composition)
where T : unmanaged
{
Expand All @@ -304,7 +309,8 @@ public AnimationBuilder NormalizedKeyFrames<T, TState>(
property,
delay,
duration ?? DefaultDuration,
repeatOption ?? RepeatOption.Once);
repeatOption ?? RepeatOption.Once,
delayBehavior ?? DefaultDelayBehavior);

build(builder, state);

Expand Down Expand Up @@ -334,19 +340,25 @@ public AnimationBuilder NormalizedKeyFrames<T, TState>(
/// <param name="build">The callback to use to construct the custom animation.</param>
/// <param name="delay">The optional initial delay for the animation.</param>
/// <param name="repeat">The repeat option for the animation (defaults to one iteration).</param>
/// <param name="delayBehavior">The delay behavior to use (ignored if <paramref name="layer"/> is <see cref="FrameworkLayer.Xaml"/>).</param>
/// <param name="layer">The target framework layer to animate.</param>
/// <returns>The current <see cref="AnimationBuilder"/> instance.</returns>
public AnimationBuilder TimedKeyFrames<T>(
string property,
Action<ITimedKeyFrameAnimationBuilder<T>> build,
TimeSpan? delay = null,
RepeatOption? repeat = null,
AnimationDelayBehavior? delayBehavior = null,
FrameworkLayer layer = FrameworkLayer.Composition)
where T : unmanaged
{
if (layer == FrameworkLayer.Composition)
{
TimedKeyFrameAnimationBuilder<T>.Composition builder = new(property, delay, repeat ?? RepeatOption.Once);
TimedKeyFrameAnimationBuilder<T>.Composition builder = new(
property,
delay,
repeat ?? RepeatOption.Once,
delayBehavior ?? DefaultDelayBehavior);

build(builder);

Expand Down Expand Up @@ -374,6 +386,7 @@ public AnimationBuilder TimedKeyFrames<T>(
/// <param name="build">The callback to use to construct the custom animation.</param>
/// <param name="delay">The optional initial delay for the animation.</param>
/// <param name="repeatOption">The repeat option for the animation (defaults to one iteration).</param>
/// <param name="delayBehavior">The delay behavior to use (ignored if <paramref name="layer"/> is <see cref="FrameworkLayer.Xaml"/>).</param>
/// <param name="layer">The target framework layer to animate.</param>
/// <returns>The current <see cref="AnimationBuilder"/> instance.</returns>
public AnimationBuilder TimedKeyFrames<T, TState>(
Expand All @@ -382,12 +395,17 @@ public AnimationBuilder TimedKeyFrames<T, TState>(
Action<ITimedKeyFrameAnimationBuilder<T>, TState> build,
TimeSpan? delay = null,
RepeatOption? repeatOption = null,
AnimationDelayBehavior? delayBehavior = null,
FrameworkLayer layer = FrameworkLayer.Composition)
where T : unmanaged
{
if (layer == FrameworkLayer.Composition)
{
TimedKeyFrameAnimationBuilder<T>.Composition builder = new(property, delay, repeatOption ?? RepeatOption.Once);
TimedKeyFrameAnimationBuilder<T>.Composition builder = new(
property,
delay,
repeatOption ?? RepeatOption.Once,
delayBehavior ?? DefaultDelayBehavior);

build(builder, state);

Expand Down
Loading