From 6b908aa0b78ebaf5baed0391066306c65f313578 Mon Sep 17 00:00:00 2001 From: Ramez Ragaa Date: Fri, 29 Nov 2024 15:07:14 +0200 Subject: [PATCH 1/2] fix(skia): support color animations --- .../Animators/AnimatorFactory.skia.cs | 7 +---- ...FloatAnimator.cs => DispatcherAnimator.cs} | 6 ++--- .../Animators/DispatcherColorAnimator.cs | 16 +++++++++++ .../Animators/DispatcherDoubleAnimator.cs | 27 +++---------------- .../UI/Xaml/Media/Animation/ColorAnimation.cs | 8 +++--- 5 files changed, 27 insertions(+), 37 deletions(-) rename src/Uno.UI/UI/Xaml/Media/Animation/Animators/{DispatcherFloatAnimator.cs => DispatcherAnimator.cs} (70%) create mode 100644 src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherColorAnimator.cs diff --git a/src/Uno.UI/UI/Xaml/Media/Animation/Animators/AnimatorFactory.skia.cs b/src/Uno.UI/UI/Xaml/Media/Animation/Animators/AnimatorFactory.skia.cs index 1da8b5f96de9..4f94c3697d50 100644 --- a/src/Uno.UI/UI/Xaml/Media/Animation/Animators/AnimatorFactory.skia.cs +++ b/src/Uno.UI/UI/Xaml/Media/Animation/Animators/AnimatorFactory.skia.cs @@ -1,9 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Uno.Extensions; -using Uno.Foundation.Logging; using Windows.UI; namespace Microsoft.UI.Xaml.Media.Animation @@ -14,6 +9,6 @@ private static IValueAnimator CreateDouble(Timeline timeline, double startingVal => new DispatcherDoubleAnimator(startingValue, targetValue); private static IValueAnimator CreateColor(Timeline timeline, ColorOffset startingValue, ColorOffset targetValue) - => new ImmediateAnimator(targetValue); + => new DispatcherColorAnimator(startingValue, targetValue); } } diff --git a/src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherFloatAnimator.cs b/src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherAnimator.cs similarity index 70% rename from src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherFloatAnimator.cs rename to src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherAnimator.cs index efef50d52a07..3ca3b1f39daf 100644 --- a/src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherFloatAnimator.cs +++ b/src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherAnimator.cs @@ -3,14 +3,14 @@ namespace Microsoft.UI.Xaml.Media.Animation { - internal sealed class DispatcherFloatAnimator : CPUBoundAnimator + internal abstract class DispatcherAnimator : CPUBoundAnimator where T : struct { public const int DefaultFrameRate = 30; private readonly int _frameRate; private readonly DispatcherTimer _timer; - public DispatcherFloatAnimator(float from, float to, int frameRate = DefaultFrameRate) + public DispatcherAnimator(T from, T to, int frameRate = DefaultFrameRate) : base(from, to) { _frameRate = frameRate; @@ -24,6 +24,6 @@ public DispatcherFloatAnimator(float from, float to, int frameRate = DefaultFram protected override void SetStartFrameDelay(long delayMs) => _timer.Interval = TimeSpan.FromMilliseconds(delayMs); protected override void SetAnimationFramesInterval() => _timer.Interval = TimeSpan.FromSeconds(1d / _frameRate); - protected override float GetUpdatedValue(long frame, float from, float to) => (float)_easing.Ease(frame, from, to, Duration); + protected abstract override T GetUpdatedValue(long frame, T from, T to); } } diff --git a/src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherColorAnimator.cs b/src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherColorAnimator.cs new file mode 100644 index 000000000000..d60fe84cc5bb --- /dev/null +++ b/src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherColorAnimator.cs @@ -0,0 +1,16 @@ +using System; +using Windows.UI; + +namespace Microsoft.UI.Xaml.Media.Animation +{ + internal sealed class DispatcherColorAnimator(ColorOffset from, ColorOffset to, int frameRate = DispatcherAnimator.DefaultFrameRate) + : DispatcherAnimator(from, to, frameRate) + { + protected override ColorOffset GetUpdatedValue(long frame, ColorOffset from, ColorOffset to) => + ColorOffset.FromArgb( + (int)Math.Round(_easing.Ease(frame, from.A, to.A, Duration)), + (int)Math.Round(_easing.Ease(frame, from.R, to.R, Duration)), + (int)Math.Round(_easing.Ease(frame, from.G, to.G, Duration)), + (int)Math.Round(_easing.Ease(frame, from.B, to.B, Duration))); + } +} diff --git a/src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherDoubleAnimator.cs b/src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherDoubleAnimator.cs index 4e1007d6b03a..201934c72516 100644 --- a/src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherDoubleAnimator.cs +++ b/src/Uno.UI/UI/Xaml/Media/Animation/Animators/DispatcherDoubleAnimator.cs @@ -1,29 +1,8 @@ -using System; -using System.Linq; - -namespace Microsoft.UI.Xaml.Media.Animation +namespace Microsoft.UI.Xaml.Media.Animation { - internal sealed class DispatcherDoubleAnimator : CPUBoundAnimator + internal sealed class DispatcherDoubleAnimator(double from, double to, int frameRate = DispatcherAnimator.DefaultFrameRate) + : DispatcherAnimator(from, to, frameRate) { - public const int DefaultFrameRate = 30; - - private readonly int _frameRate; - private readonly DispatcherTimer _timer; - - public DispatcherDoubleAnimator(double from, double to, int frameRate = DefaultFrameRate) - : base(from, to) - { - _frameRate = frameRate; - _timer = new DispatcherTimer(); - _timer.Tick += OnFrame; - } - - protected override void EnableFrameReporting() => _timer.Start(); - protected override void DisableFrameReporting() => _timer.Stop(); - - protected override void SetStartFrameDelay(long delayMs) => _timer.Interval = TimeSpan.FromMilliseconds(delayMs); - protected override void SetAnimationFramesInterval() => _timer.Interval = TimeSpan.FromSeconds(1d / _frameRate); - protected override double GetUpdatedValue(long frame, double from, double to) => (float)_easing.Ease(frame, from, to, Duration); } } diff --git a/src/Uno.UI/UI/Xaml/Media/Animation/ColorAnimation.cs b/src/Uno.UI/UI/Xaml/Media/Animation/ColorAnimation.cs index 34fd1e728767..4e24390a6604 100644 --- a/src/Uno.UI/UI/Xaml/Media/Animation/ColorAnimation.cs +++ b/src/Uno.UI/UI/Xaml/Media/Animation/ColorAnimation.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Uno; using Windows.UI; @@ -138,6 +134,10 @@ ColorOffset IAnimation.Convert(object value) { return (ColorOffset)Colors.Parse(s); } + else if (value is Color c) + { + return (ColorOffset)c; + } // TODO: handle int? return default(ColorOffset); From b6f5318b5dd0ac57213e017448caa2824bcdf637 Mon Sep 17 00:00:00 2001 From: Ramez Ragaa Date: Fri, 29 Nov 2024 15:40:57 +0200 Subject: [PATCH 2/2] chore: fix test and mark as manual --- .../ColorAnimationUsingKeyFrames_Fill.xaml | 54 ++++++++++--------- .../ColorAnimationUsingKeyFrames_Fill.xaml.cs | 2 +- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Media_Animation/ColorAnimationUsingKeyFrames_Fill.xaml b/src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Media_Animation/ColorAnimationUsingKeyFrames_Fill.xaml index baca954e0453..c56f97bde242 100644 --- a/src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Media_Animation/ColorAnimationUsingKeyFrames_Fill.xaml +++ b/src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Media_Animation/ColorAnimationUsingKeyFrames_Fill.xaml @@ -9,12 +9,18 @@ Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> - - - - - - + #FF0000 + #FF8000 + #FFFF00 + #008000 + #0000FF + #A000C0 + + + + + + @@ -162,18 +168,18 @@ Original Colors: (should never change) - - - - - - - - - - - - + + + + + + + + + + + + @@ -186,12 +192,12 @@ Animated block: - - - - - - + + + + + + diff --git a/src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Media_Animation/ColorAnimationUsingKeyFrames_Fill.xaml.cs b/src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Media_Animation/ColorAnimationUsingKeyFrames_Fill.xaml.cs index b411821661ba..c30d9ba2a958 100644 --- a/src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Media_Animation/ColorAnimationUsingKeyFrames_Fill.xaml.cs +++ b/src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Media_Animation/ColorAnimationUsingKeyFrames_Fill.xaml.cs @@ -5,7 +5,7 @@ namespace UITests.Windows_UI_Xaml_Media_Animation { - [Sample("Animations")] + [Sample("Animations", IsManualTest = true)] public sealed partial class ColorAnimationUsingKeyFrames_Fill : Page { public ColorAnimationUsingKeyFrames_Fill()