2024-04-18 01:44:37 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Numerics;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Windows.UI.Composition;
|
|
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
namespace Wino.Extensions;
|
|
|
|
|
|
|
|
|
|
|
|
public static partial class CompositionExtensions
|
2024-04-18 01:44:37 +02:00
|
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
|
public static void StartSizeAnimation(this UIElement element, Vector2? from = null, Vector2? to = null,
|
|
|
|
|
|
double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
|
|
|
|
|
|
AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
|
2024-04-18 01:44:37 +02:00
|
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
|
CompositionScopedBatch batch = null;
|
2025-02-16 11:43:30 +01:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
var visual = element.Visual();
|
|
|
|
|
|
var compositor = visual.Compositor;
|
2025-02-16 11:43:30 +01:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
if (completed != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
batch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
|
|
|
|
|
|
batch.Completed += (s, e) => completed();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
if (to == null)
|
2024-04-18 01:44:37 +02:00
|
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
|
to = Vector2.One;
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
visual.StartAnimation("Size",
|
|
|
|
|
|
compositor.CreateVector2KeyFrameAnimation(from, to.Value, duration, delay, easing, iterationBehavior));
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
batch?.End();
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
public static void StartSizeAnimation(this Visual visual, Vector2? from = null, Vector2? to = null,
|
|
|
|
|
|
double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
|
|
|
|
|
|
AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
|
|
|
|
|
|
{
|
|
|
|
|
|
CompositionScopedBatch batch = null;
|
|
|
|
|
|
var compositor = visual.Compositor;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
if (completed != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
batch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
|
|
|
|
|
|
batch.Completed += (s, e) => completed();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
if (to == null)
|
2024-04-18 01:44:37 +02:00
|
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
|
to = Vector2.One;
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
visual.StartAnimation("Size",
|
|
|
|
|
|
compositor.CreateVector2KeyFrameAnimation(from, to.Value, duration, delay, easing, iterationBehavior));
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
batch?.End();
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
public static Task StartSizeAnimationAsync(this UIElement element, Vector2? from = null, Vector2? to = null,
|
|
|
|
|
|
double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
|
|
|
|
|
|
AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
|
|
|
|
|
|
{
|
|
|
|
|
|
CompositionScopedBatch batch;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
var visual = element.Visual();
|
|
|
|
|
|
var compositor = visual.Compositor;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
var taskSource = new TaskCompletionSource<bool>();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
void Completed(object o, CompositionBatchCompletedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
batch.Completed -= Completed;
|
|
|
|
|
|
taskSource.SetResult(true);
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
batch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
|
|
|
|
|
|
batch.Completed += Completed;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
if (to == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
to = Vector2.One;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
visual.StartAnimation("Size",
|
|
|
|
|
|
compositor.CreateVector2KeyFrameAnimation(from, to.Value, duration, delay, easing, iterationBehavior));
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
batch.End();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
return taskSource.Task;
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
public static Task StartSizeAnimationAsync(this Visual visual, Vector2? from = null, Vector2? to = null,
|
|
|
|
|
|
double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
|
|
|
|
|
|
AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
|
|
|
|
|
|
{
|
|
|
|
|
|
CompositionScopedBatch batch;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
var compositor = visual.Compositor;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
var taskSource = new TaskCompletionSource<bool>();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
void Completed(object o, CompositionBatchCompletedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
batch.Completed -= Completed;
|
|
|
|
|
|
taskSource.SetResult(true);
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
batch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
|
|
|
|
|
|
batch.Completed += Completed;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
if (to == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
to = Vector2.One;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
|
visual.StartAnimation("Size",
|
|
|
|
|
|
compositor.CreateVector2KeyFrameAnimation(from, to.Value, duration, delay, easing, iterationBehavior));
|
|
|
|
|
|
|
|
|
|
|
|
batch.End();
|
|
|
|
|
|
|
|
|
|
|
|
return taskSource.Task;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
|
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|