Skip to content

Commit

Permalink
better animation helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kikipoulet committed Feb 17, 2025
1 parent c96a413 commit f05aff4
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 5 deletions.
22 changes: 17 additions & 5 deletions SukiUI/Helpers/ControlAnimations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ public static void Vibrate(this Animatable control, TimeSpan duration)
}.RunAsync(control);
}

public static void Animate<T>(this Animatable control, AvaloniaProperty Property, T from, T to, TimeSpan duration, ulong count = 1)
public static CancellationTokenSource Animate<T>(this Animatable control, AvaloniaProperty Property, T from, T to, TimeSpan duration, ulong count = 1)
{
var tokensource = new CancellationTokenSource();

new Avalonia.Animation.Animation
{
Duration = duration,
Expand All @@ -124,11 +126,15 @@ public static void Animate<T>(this Animatable control, AvaloniaProperty Property
KeyTime = duration
}
}
}.RunAsync(control);
}.RunAsync(control, tokensource.Token);

return tokensource;
}

public static void Animate<T>(this Animatable control, AvaloniaProperty Property, T from, T to)
public static CancellationTokenSource Animate<T>(this Animatable control, AvaloniaProperty Property, T from, T to)
{
var tokensource = new CancellationTokenSource();

new Avalonia.Animation.Animation
{
Duration = TimeSpan.FromMilliseconds(500),
Expand All @@ -149,6 +155,12 @@ public static void Animate<T>(this Animatable control, AvaloniaProperty Property
KeyTime = TimeSpan.FromMilliseconds(500)
}
}
}.RunAsync(control);
}.RunAsync(control, tokensource.Token);

return tokensource;
}
}



}

97 changes: 97 additions & 0 deletions SukiUI/Helpers/FluentAnimator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Avalonia.Styling;
using Avalonia;
using Avalonia.Animation;
using Avalonia.Animation.Easings;
using System;
using System.Threading;

namespace SukiUI.Helpers;


public static class FluentAnimatorExtensions
{
public static FluentAnimator<T> Animate<T>(this Animatable control, AvaloniaProperty<T> property)
{
return new FluentAnimator<T>(control, property);
}
}


/*
* var tokensource = MyButton.Animate(OpacityProperty)
.From(0).To(1)
.WithDuration(TimeSpan.FromMilliseconds(1000))
.WithEasing(new CubicEaseOut())
.Start();
*
*/

public class FluentAnimator<T>
{
private readonly Animatable _control;
private readonly AvaloniaProperty<T> _property;
private T _from;
private T _to;
private TimeSpan _duration = TimeSpan.FromMilliseconds(500);
private Easing _easing = new CubicEaseInOut();

public FluentAnimator(Animatable control, AvaloniaProperty<T> property)
{
_control = control;
_property = property;
}

public FluentAnimator<T> From(T value)
{
_from = value;
return this;
}

public FluentAnimator<T> To(T value)
{
_to = value;
return this;
}

public FluentAnimator<T> WithDuration(TimeSpan duration)
{
_duration = duration;
return this;
}

public FluentAnimator<T> WithEasing(Easing easing)
{
_easing = easing;
return this;
}

public CancellationTokenSource Start()
{
var tokenSource = new CancellationTokenSource();

new Animation
{
Duration = _duration,
FillMode = FillMode.Forward,
Easing = _easing,
IterationCount = new IterationCount(1),
PlaybackDirection = PlaybackDirection.Normal,
Children =
{
new KeyFrame
{
Setters = { new Setter { Property = _property, Value = _from } },
KeyTime = TimeSpan.Zero
},
new KeyFrame
{
Setters = { new Setter { Property = _property, Value = _to } },
KeyTime = _duration
}
}
}.RunAsync(_control, tokenSource.Token);

return tokenSource;
}
}

0 comments on commit f05aff4

Please sign in to comment.