Skip to content

Commit 9972cce

Browse files
author
msftbot[bot]
authored
Animations package improvements (#3796)
## Follow up for #3639 <!-- Add a brief overview here of the feature/bug & fix. --> ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> - Feature - Improvements <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## Changelog This PR tracks new changes and tweaks for the `Microsoft.Toolkit.Uwp.UI.Animations` package before the 7.0 release. Changes are done according to feedbacks and requests from developers trying out the Preview 5 release. This PR contains the following changes: - All `AnimationBuilder` and XAML animation types now use `AnimationDelayBehavior.SetValueBeforeDelay` by default - New `DelayBehavior` property available for XAML animation types (ignored when an animation is on the XAML layer) - New `delayBehavior` parameter available for the explicit `AnimationBuilder` keyframe animation APIs (the more advanced ones) - New `RepeatOption` parameter now available for all `AnimationBuilder` APIs (default, keyframe, etc.) Opening as a draft PR until we finalize the list of changes to add. ## PR Checklist Please check if your PR fulfills the following requirements: - [X] Tested code with current [supported SDKs](../readme.md#supported) - [ ] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link --> - [ ] Sample in sample app has been added / updated (for bug fixes / features) - [ ] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets) - [X] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/windows-toolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc... - [ ] Tests for the changes have been added (for bug fixes / features) (if applicable) - [X] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [ ] Contains **NO** breaking changes
2 parents 66c9743 + e27c0d7 commit 9972cce

16 files changed

+302
-91
lines changed

Microsoft.Toolkit.Uwp.UI.Animations/Builders/AnimationBuilder.Default.cs

Lines changed: 97 additions & 42 deletions
Large diffs are not rendered by default.

Microsoft.Toolkit.Uwp.UI.Animations/Builders/AnimationBuilder.Factories.cs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ private sealed record AnimationFactory<T>(
6161
T? From,
6262
TimeSpan Delay,
6363
TimeSpan Duration,
64+
RepeatOption Repeat,
6465
EasingType EasingType,
6566
EasingMode EasingMode)
6667
: ICompositionAnimationFactory, IXamlAnimationFactory
@@ -70,6 +71,7 @@ private sealed record AnimationFactory<T>(
7071
public CompositionAnimation GetAnimation(CompositionObject targetHint, out CompositionObject? target)
7172
{
7273
CompositionEasingFunction? easingFunction = targetHint.Compositor.TryCreateEasingFunction(EasingType, EasingMode);
74+
(AnimationIterationBehavior iterationBehavior, int iterationCount) = Repeat.ToBehaviorAndCount();
7375

7476
target = null;
7577

@@ -80,7 +82,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
8082
GetToAs<bool>(),
8183
GetFromAs<bool>(),
8284
Delay,
83-
Duration);
85+
Duration,
86+
iterationBehavior: iterationBehavior,
87+
iterationCount: iterationCount);
8488
}
8589

8690
if (typeof(T) == typeof(float))
@@ -91,7 +95,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
9195
GetFromAs<float>(),
9296
Delay,
9397
Duration,
94-
easingFunction);
98+
easingFunction,
99+
iterationBehavior: iterationBehavior,
100+
iterationCount: iterationCount);
95101
}
96102

97103
if (typeof(T) == typeof(double))
@@ -102,7 +108,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
102108
(float?)GetFromAs<double>(),
103109
Delay,
104110
Duration,
105-
easingFunction);
111+
easingFunction,
112+
iterationBehavior: iterationBehavior,
113+
iterationCount: iterationCount);
106114
}
107115

108116
if (typeof(T) == typeof(Vector2))
@@ -113,7 +121,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
113121
GetFromAs<Vector2>(),
114122
Delay,
115123
Duration,
116-
easingFunction);
124+
easingFunction,
125+
iterationBehavior: iterationBehavior,
126+
iterationCount: iterationCount);
117127
}
118128

119129
if (typeof(T) == typeof(Vector3))
@@ -124,7 +134,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
124134
GetFromAs<Vector3>(),
125135
Delay,
126136
Duration,
127-
easingFunction);
137+
easingFunction,
138+
iterationBehavior: iterationBehavior,
139+
iterationCount: iterationCount);
128140
}
129141

130142
if (typeof(T) == typeof(Vector4))
@@ -135,7 +147,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
135147
GetFromAs<Vector4>(),
136148
Delay,
137149
Duration,
138-
easingFunction);
150+
easingFunction,
151+
iterationBehavior: iterationBehavior,
152+
iterationCount: iterationCount);
139153
}
140154

141155
if (typeof(T) == typeof(Color))
@@ -146,7 +160,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
146160
GetFromAs<Color>(),
147161
Delay,
148162
Duration,
149-
easingFunction);
163+
easingFunction,
164+
iterationBehavior: iterationBehavior,
165+
iterationCount: iterationCount);
150166
}
151167

152168
if (typeof(T) == typeof(Quaternion))
@@ -157,7 +173,9 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
157173
GetFromAs<Quaternion>(),
158174
Delay,
159175
Duration,
160-
easingFunction);
176+
easingFunction,
177+
iterationBehavior: iterationBehavior,
178+
iterationCount: iterationCount);
161179
}
162180

163181
throw new InvalidOperationException("Invalid animation type");
@@ -177,6 +195,7 @@ public Timeline GetAnimation(DependencyObject targetHint)
177195
Delay,
178196
Duration,
179197
easingFunction,
198+
Repeat.ToRepeatBehavior(),
180199
enableDependecyAnimations: true);
181200
}
182201

@@ -189,6 +208,7 @@ public Timeline GetAnimation(DependencyObject targetHint)
189208
Delay,
190209
Duration,
191210
easingFunction,
211+
Repeat.ToRepeatBehavior(),
192212
enableDependecyAnimations: true);
193213
}
194214

@@ -201,6 +221,7 @@ public Timeline GetAnimation(DependencyObject targetHint)
201221
Delay,
202222
Duration,
203223
easingFunction,
224+
Repeat.ToRepeatBehavior(),
204225
enableDependecyAnimations: true);
205226
}
206227

@@ -212,7 +233,8 @@ public Timeline GetAnimation(DependencyObject targetHint)
212233
GetFromAs<Color>(),
213234
Delay,
214235
Duration,
215-
easingFunction);
236+
easingFunction,
237+
Repeat.ToRepeatBehavior());
216238
}
217239

218240
throw new InvalidOperationException("Invalid animation type");
@@ -287,6 +309,7 @@ private sealed record CompositionClipScalarAnimation(
287309
float? From,
288310
TimeSpan Delay,
289311
TimeSpan Duration,
312+
RepeatOption Repeat,
290313
EasingType EasingType,
291314
EasingMode EasingMode)
292315
: ICompositionAnimationFactory
@@ -297,7 +320,16 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
297320
Visual visual = (Visual)targetHint;
298321
InsetClip clip = visual.Clip as InsetClip ?? (InsetClip)(visual.Clip = visual.Compositor.CreateInsetClip());
299322
CompositionEasingFunction? easingFunction = clip.Compositor.TryCreateEasingFunction(EasingType, EasingMode);
300-
ScalarKeyFrameAnimation animation = clip.Compositor.CreateScalarKeyFrameAnimation(Property, To, From, Delay, Duration, easingFunction);
323+
(AnimationIterationBehavior iterationBehavior, int iterationCount) = Repeat.ToBehaviorAndCount();
324+
ScalarKeyFrameAnimation animation = clip.Compositor.CreateScalarKeyFrameAnimation(
325+
Property,
326+
To,
327+
From,
328+
Delay,
329+
Duration,
330+
easingFunction,
331+
iterationBehavior: iterationBehavior,
332+
iterationCount: iterationCount);
301333

302334
target = clip;
303335

@@ -314,6 +346,7 @@ private sealed record XamlTransformDoubleAnimationFactory(
314346
double? From,
315347
TimeSpan Delay,
316348
TimeSpan Duration,
349+
RepeatOption Repeat,
317350
EasingType EasingType,
318351
EasingMode EasingMode)
319352
: IXamlAnimationFactory
@@ -328,7 +361,14 @@ public Timeline GetAnimation(DependencyObject targetHint)
328361
element.RenderTransform = transform = new CompositeTransform();
329362
}
330363

331-
return transform.CreateDoubleAnimation(Property, To, From, Duration, Delay, EasingType.ToEasingFunction(EasingMode));
364+
return transform.CreateDoubleAnimation(
365+
Property,
366+
To,
367+
From,
368+
Duration,
369+
Delay,
370+
EasingType.ToEasingFunction(EasingMode),
371+
Repeat.ToRepeatBehavior());
332372
}
333373
}
334374

Microsoft.Toolkit.Uwp.UI.Animations/Builders/AnimationBuilder.KeyFrames.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ public IPropertyAnimationBuilder<Vector2> Size()
236236
/// <param name="delay">The optional initial delay for the animation.</param>
237237
/// <param name="duration">The animation duration.</param>
238238
/// <param name="repeatOption">The repeat option for the animation (defaults to one iteration).</param>
239+
/// <param name="delayBehavior">The delay behavior to use (ignored if <paramref name="layer"/> is <see cref="FrameworkLayer.Xaml"/>).</param>
239240
/// <param name="layer">The target framework layer to animate.</param>
240241
/// <returns>The current <see cref="AnimationBuilder"/> instance.</returns>
241242
public AnimationBuilder NormalizedKeyFrames<T>(
@@ -244,6 +245,7 @@ public AnimationBuilder NormalizedKeyFrames<T>(
244245
TimeSpan? delay = null,
245246
TimeSpan? duration = null,
246247
RepeatOption? repeatOption = null,
248+
AnimationDelayBehavior? delayBehavior = null,
247249
FrameworkLayer layer = FrameworkLayer.Composition)
248250
where T : unmanaged
249251
{
@@ -253,7 +255,8 @@ public AnimationBuilder NormalizedKeyFrames<T>(
253255
property,
254256
delay,
255257
duration ?? DefaultDuration,
256-
repeatOption ?? RepeatOption.Once);
258+
repeatOption ?? RepeatOption.Once,
259+
delayBehavior ?? DefaultDelayBehavior);
257260

258261
build(builder);
259262

@@ -286,6 +289,7 @@ public AnimationBuilder NormalizedKeyFrames<T>(
286289
/// <param name="delay">The optional initial delay for the animation.</param>
287290
/// <param name="duration">The animation duration.</param>
288291
/// <param name="repeatOption">The repeat option for the animation (defaults to one iteration).</param>
292+
/// <param name="delayBehavior">The delay behavior to use (ignored if <paramref name="layer"/> is <see cref="FrameworkLayer.Xaml"/>).</param>
289293
/// <param name="layer">The target framework layer to animate.</param>
290294
/// <returns>The current <see cref="AnimationBuilder"/> instance.</returns>
291295
public AnimationBuilder NormalizedKeyFrames<T, TState>(
@@ -295,6 +299,7 @@ public AnimationBuilder NormalizedKeyFrames<T, TState>(
295299
TimeSpan? delay = null,
296300
TimeSpan? duration = null,
297301
RepeatOption? repeatOption = null,
302+
AnimationDelayBehavior? delayBehavior = null,
298303
FrameworkLayer layer = FrameworkLayer.Composition)
299304
where T : unmanaged
300305
{
@@ -304,7 +309,8 @@ public AnimationBuilder NormalizedKeyFrames<T, TState>(
304309
property,
305310
delay,
306311
duration ?? DefaultDuration,
307-
repeatOption ?? RepeatOption.Once);
312+
repeatOption ?? RepeatOption.Once,
313+
delayBehavior ?? DefaultDelayBehavior);
308314

309315
build(builder, state);
310316

@@ -334,19 +340,25 @@ public AnimationBuilder NormalizedKeyFrames<T, TState>(
334340
/// <param name="build">The callback to use to construct the custom animation.</param>
335341
/// <param name="delay">The optional initial delay for the animation.</param>
336342
/// <param name="repeat">The repeat option for the animation (defaults to one iteration).</param>
343+
/// <param name="delayBehavior">The delay behavior to use (ignored if <paramref name="layer"/> is <see cref="FrameworkLayer.Xaml"/>).</param>
337344
/// <param name="layer">The target framework layer to animate.</param>
338345
/// <returns>The current <see cref="AnimationBuilder"/> instance.</returns>
339346
public AnimationBuilder TimedKeyFrames<T>(
340347
string property,
341348
Action<ITimedKeyFrameAnimationBuilder<T>> build,
342349
TimeSpan? delay = null,
343350
RepeatOption? repeat = null,
351+
AnimationDelayBehavior? delayBehavior = null,
344352
FrameworkLayer layer = FrameworkLayer.Composition)
345353
where T : unmanaged
346354
{
347355
if (layer == FrameworkLayer.Composition)
348356
{
349-
TimedKeyFrameAnimationBuilder<T>.Composition builder = new(property, delay, repeat ?? RepeatOption.Once);
357+
TimedKeyFrameAnimationBuilder<T>.Composition builder = new(
358+
property,
359+
delay,
360+
repeat ?? RepeatOption.Once,
361+
delayBehavior ?? DefaultDelayBehavior);
350362

351363
build(builder);
352364

@@ -374,6 +386,7 @@ public AnimationBuilder TimedKeyFrames<T>(
374386
/// <param name="build">The callback to use to construct the custom animation.</param>
375387
/// <param name="delay">The optional initial delay for the animation.</param>
376388
/// <param name="repeatOption">The repeat option for the animation (defaults to one iteration).</param>
389+
/// <param name="delayBehavior">The delay behavior to use (ignored if <paramref name="layer"/> is <see cref="FrameworkLayer.Xaml"/>).</param>
377390
/// <param name="layer">The target framework layer to animate.</param>
378391
/// <returns>The current <see cref="AnimationBuilder"/> instance.</returns>
379392
public AnimationBuilder TimedKeyFrames<T, TState>(
@@ -382,12 +395,17 @@ public AnimationBuilder TimedKeyFrames<T, TState>(
382395
Action<ITimedKeyFrameAnimationBuilder<T>, TState> build,
383396
TimeSpan? delay = null,
384397
RepeatOption? repeatOption = null,
398+
AnimationDelayBehavior? delayBehavior = null,
385399
FrameworkLayer layer = FrameworkLayer.Composition)
386400
where T : unmanaged
387401
{
388402
if (layer == FrameworkLayer.Composition)
389403
{
390-
TimedKeyFrameAnimationBuilder<T>.Composition builder = new(property, delay, repeatOption ?? RepeatOption.Once);
404+
TimedKeyFrameAnimationBuilder<T>.Composition builder = new(
405+
property,
406+
delay,
407+
repeatOption ?? RepeatOption.Once,
408+
delayBehavior ?? DefaultDelayBehavior);
391409

392410
build(builder, state);
393411

0 commit comments

Comments
 (0)