Skip to content

Commit

Permalink
fix(skia): support color animations
Browse files Browse the repository at this point in the history
  • Loading branch information
ramezgerges committed Nov 29, 2024
1 parent dabd0a6 commit 6b908aa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<ColorOffset>(targetValue);
=> new DispatcherColorAnimator(startingValue, targetValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

namespace Microsoft.UI.Xaml.Media.Animation
{
internal sealed class DispatcherFloatAnimator : CPUBoundAnimator<float>
internal abstract class DispatcherAnimator<T> : CPUBoundAnimator<T> 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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<ConsoleColor>.DefaultFrameRate)
: DispatcherAnimator<ColorOffset>(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)));
}
}
Original file line number Diff line number Diff line change
@@ -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<double>
internal sealed class DispatcherDoubleAnimator(double from, double to, int frameRate = DispatcherAnimator<double>.DefaultFrameRate)
: DispatcherAnimator<double>(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);
}
}
8 changes: 4 additions & 4 deletions src/Uno.UI/UI/Xaml/Media/Animation/ColorAnimation.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -138,6 +134,10 @@ ColorOffset IAnimation<ColorOffset>.Convert(object value)
{
return (ColorOffset)Colors.Parse(s);
}
else if (value is Color c)
{
return (ColorOffset)c;
}

// TODO: handle int?
return default(ColorOffset);
Expand Down

0 comments on commit 6b908aa

Please sign in to comment.