Skip to content

Commit

Permalink
Add Uno platform targets to Animation package
Browse files Browse the repository at this point in the history
Make the Xaml layer the default animation layer when using Uno
  • Loading branch information
michael-hawker committed Jul 10, 2023
1 parent fb0b2d8 commit bb90817
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 10 deletions.
48 changes: 48 additions & 0 deletions components/Animations/src/Builders/AnimationBuilder.Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ public AnimationBuilder Opacity(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -128,7 +132,11 @@ public AnimationBuilder Translation(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -162,7 +170,11 @@ public AnimationBuilder Translation(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -297,7 +309,11 @@ public AnimationBuilder Scale(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -337,7 +353,11 @@ public AnimationBuilder Scale(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -371,7 +391,11 @@ public AnimationBuilder Scale(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -432,7 +456,11 @@ public AnimationBuilder CenterPoint(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -466,7 +494,11 @@ public AnimationBuilder CenterPoint(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -525,7 +557,11 @@ public AnimationBuilder Rotation(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -562,7 +598,11 @@ public AnimationBuilder RotationInDegrees(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -800,7 +840,11 @@ public AnimationBuilder Size(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -834,7 +878,11 @@ public AnimationBuilder Size(
RepeatOption? repeat = null,
EasingType easingType = DefaultEasingType,
EasingMode easingMode = DefaultEasingMode,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down
51 changes: 46 additions & 5 deletions components/Animations/src/Builders/AnimationBuilder.KeyFrames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ public IPropertyAnimationBuilder<Vector2> AnchorPoint()
/// </summary>
/// <param name="layer">The target framework layer to animate.</param>
/// <returns>An <see cref="IPropertyAnimationBuilder{T}"/> instance to configure the animation.</returns>
public IPropertyAnimationBuilder<double> Opacity(FrameworkLayer layer = FrameworkLayer.Composition)
public IPropertyAnimationBuilder<double> Opacity(
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
return new PropertyAnimationBuilder<double>(this, nameof(Visual.Opacity), layer);
}
Expand All @@ -52,7 +57,12 @@ public IPropertyAnimationBuilder<double> Opacity(FrameworkLayer layer = Framewor
/// <param name="axis">The target translation axis to animate.</param>
/// <param name="layer">The target framework layer to animate.</param>
/// <returns>An <see cref="IPropertyAnimationBuilder{T}"/> instance to configure the animation.</returns>
public IPropertyAnimationBuilder<double> Translation(Axis axis, FrameworkLayer layer = FrameworkLayer.Composition)
public IPropertyAnimationBuilder<double> Translation(Axis axis,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -99,7 +109,12 @@ public IPropertyAnimationBuilder<Vector3> Offset()
/// <param name="axis">The target scale axis to animate.</param>
/// <param name="layer">The target framework layer to animate.</param>
/// <returns>An <see cref="IPropertyAnimationBuilder{T}"/> instance to configure the animation.</returns>
public IPropertyAnimationBuilder<double> Scale(Axis axis, FrameworkLayer layer = FrameworkLayer.Composition)
public IPropertyAnimationBuilder<double> Scale(Axis axis,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand All @@ -125,7 +140,12 @@ public IPropertyAnimationBuilder<Vector3> Scale()
/// <param name="axis">The target scale axis to animate.</param>
/// <param name="layer">The target framework layer to animate.</param>
/// <returns>An <see cref="IPropertyAnimationBuilder{T}"/> instance to configure the animation.</returns>
public IPropertyAnimationBuilder<double> CenterPoint(Axis axis, FrameworkLayer layer = FrameworkLayer.Composition)
public IPropertyAnimationBuilder<double> CenterPoint(Axis axis,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -160,7 +180,12 @@ public IPropertyAnimationBuilder<double> Rotation()
/// </summary>
/// <param name="layer">The target framework layer to animate.</param>
/// <returns>An <see cref="IPropertyAnimationBuilder{T}"/> instance to configure the animation.</returns>
public IPropertyAnimationBuilder<double> RotationInDegrees(FrameworkLayer layer = FrameworkLayer.Composition)
public IPropertyAnimationBuilder<double> RotationInDegrees(
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
{
if (layer == FrameworkLayer.Composition)
{
Expand Down Expand Up @@ -246,7 +271,11 @@ public AnimationBuilder NormalizedKeyFrames<T>(
TimeSpan? duration = null,
RepeatOption? repeatOption = null,
AnimationDelayBehavior? delayBehavior = null,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
where T : unmanaged
{
if (layer == FrameworkLayer.Composition)
Expand Down Expand Up @@ -300,7 +329,11 @@ public AnimationBuilder NormalizedKeyFrames<T, TState>(
TimeSpan? duration = null,
RepeatOption? repeatOption = null,
AnimationDelayBehavior? delayBehavior = null,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
where T : unmanaged
{
if (layer == FrameworkLayer.Composition)
Expand Down Expand Up @@ -349,7 +382,11 @@ public AnimationBuilder TimedKeyFrames<T>(
TimeSpan? delay = null,
RepeatOption? repeat = null,
AnimationDelayBehavior? delayBehavior = null,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
where T : unmanaged
{
if (layer == FrameworkLayer.Composition)
Expand Down Expand Up @@ -396,7 +433,11 @@ public AnimationBuilder TimedKeyFrames<T, TState>(
TimeSpan? delay = null,
RepeatOption? repeatOption = null,
AnimationDelayBehavior? delayBehavior = null,
#if !HAS_UNO
FrameworkLayer layer = FrameworkLayer.Composition)
#else
FrameworkLayer layer = FrameworkLayer.Xaml)
#endif
where T : unmanaged
{
if (layer == FrameworkLayer.Composition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public CompositionAnimation GetAnimation(CompositionObject targetHint, out Compo
// We can retrieve the total duration from the last timed keyframe, and then set
// this as the target duration and use it to normalize the keyframe progresses.
ArraySegment<KeyFrameInfo> keyFrames = this.keyFrames.GetArraySegment();
#if !NETSTANDARD2_0
TimeSpan duration = keyFrames[keyFrames.Count - 1].GetTimedProgress(default);
#else
TimeSpan duration = keyFrames.Array[keyFrames.Count - 1].GetTimedProgress(default);
#endif

return NormalizedKeyFrameAnimationBuilder<T>.GetAnimation(
targetHint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ public bool TryInsertExpressionKeyFrame(KeyFrameAnimation animation, TimeSpan du
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float GetNormalizedProgress(TimeSpan duration)
{
#if !NETSTANDARD2_0
return (float)Math.Clamp(this.progress.TotalMilliseconds / duration.TotalMilliseconds, 0, 1);
#else
var result = this.progress.TotalMilliseconds / duration.TotalMilliseconds;
return (float)Math.Max(Math.Min(result, 1), 0);
#endif
}

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion components/Animations/src/MultiTarget.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
MultiTarget is a custom property that indicates which target a project is designed to be built for / run on.
Used to create project references, generate solution files, enable/disable TargetFrameworks, and build nuget packages.
-->
<MultiTarget>uwp;wasdk;</MultiTarget>
<MultiTarget>uwp;wasdk;wpf;wasm;linuxgtk;macos;ios;android;</MultiTarget>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion components/Animations/src/Xaml/Abstract/Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace CommunityToolkit.WinUI.Animations;
/// <summary>
/// A base model representing an animation that can be used in XAML.
/// </summary>
public abstract class Animation : DependencyObject, ITimeline
public abstract partial class Animation : DependencyObject, ITimeline
{
/// <summary>
/// Gets or sets the optional initial delay for the animation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ public abstract class CustomAnimation<TValue, TKeyFrame> : ImplicitAnimation<TVa
/// Gets or sets the target framework layer for the animation. This is only supported
/// for a set of animation types (see the docs for more on this). Furthermore, this is
/// ignored when the animation is being used as an implicit composition animation.
#if !HAS_UNO
/// The default value is <see cref="FrameworkLayer.Composition"/>.
/// </summary>
public FrameworkLayer Layer { get; set; }
#else
/// The default value is <see cref="FrameworkLayer.Xaml"/>.
/// </summary>
public FrameworkLayer Layer { get; set; } = FrameworkLayer.Xaml;
#endif

/// <inheritdoc/>
protected override string ExplicitTarget => Target!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

namespace CommunityToolkit.WinUI.Animations;

#if HAS_UNO
/// <summary>
/// A base model representing a key frame for Uno Platform compatibility
/// </summary>
public abstract partial class KeyFrame : DependencyObject
{
}
#endif

/// <summary>
/// A base model representing a typed keyframe that can be used in XAML.
/// </summary>
Expand All @@ -14,7 +23,11 @@ namespace CommunityToolkit.WinUI.Animations;
/// This can differ from <typeparamref name="TKeyFrame"/> to facilitate XAML parsing.
/// </typeparam>
/// <typeparam name="TKeyFrame">The actual type of keyframe values in use.</typeparam>
public abstract class KeyFrame<TValue, TKeyFrame> : DependencyObject, IKeyFrame<TKeyFrame>
#if !HAS_UNO
public abstract partial class KeyFrame<TValue, TKeyFrame> : DependencyObject, IKeyFrame<TKeyFrame>
#else
public abstract partial class KeyFrame<TValue, TKeyFrame> : KeyFrame, IKeyFrame<TKeyFrame>
#endif
{
/// <summary>
/// Gets or sets the key time for the current keyframe. This is a normalized
Expand Down
2 changes: 1 addition & 1 deletion components/Animations/src/Xaml/Activities/Activity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CommunityToolkit.WinUI.Animations;
/// <summary>
/// Base class to use when creating activities which accept a <see cref="Delay"/>.
/// </summary>
public abstract class Activity : DependencyObject, IActivity
public abstract partial class Activity : DependencyObject, IActivity
{
/// <summary>
/// Gets or sets the <see cref="TimeSpan"/> to wait before running the activity.
Expand Down
2 changes: 1 addition & 1 deletion components/Animations/src/Xaml/AnimationDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CommunityToolkit.WinUI.Animations;
/// <summary>
/// A collection of animations that can be defined from XAML.
/// </summary>
public sealed class AnimationDictionary : DependencyObject, IList<AnimationSet>
public sealed partial class AnimationDictionary : DependencyObject, IList<AnimationSet>
{
/// <summary>
/// The underlying list of animations.
Expand Down
10 changes: 10 additions & 0 deletions components/Animations/src/Xaml/AnimationSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,17 @@ public Task StartAsync(UIElement element)

CancellationTokenSource cancellationTokenSource = new();

#if !NETSTANDARD2_0
this.cancellationTokenMap.AddOrUpdate(element, cancellationTokenSource);
#else
// If we have a token, remove it first, before adding new one.
if (this.cancellationTokenMap.TryGetValue(element, out _))
{
this.cancellationTokenMap.Remove(element);
}

this.cancellationTokenMap.Add(element, cancellationTokenSource);
#endif

return StartAsync(element, cancellationTokenSource.Token);
}
Expand Down

0 comments on commit bb90817

Please sign in to comment.