Browse Source

Make Animation.RunAsync cancellable

pull/6018/head
Max Katz 5 years ago
parent
commit
52e6b9a6d1
  1. 14
      src/Avalonia.Animation/Animation.cs
  2. 6
      src/Avalonia.Animation/ApiCompatBaseline.txt
  3. 3
      src/Avalonia.Animation/IAnimation.cs

14
src/Avalonia.Animation/Animation.cs

@ -3,10 +3,11 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Animation.Animators; using Avalonia.Animation.Animators;
using Avalonia.Animation.Easings; using Avalonia.Animation.Easings;
using Avalonia.Collections;
using Avalonia.Data; using Avalonia.Data;
using Avalonia.Metadata; using Avalonia.Metadata;
@ -292,7 +293,7 @@ namespace Avalonia.Animation
return (newAnimatorInstances, subscriptions); return (newAnimatorInstances, subscriptions);
} }
/// <inheritdocs/> /// <inheritdoc/>
public IDisposable Apply(Animatable control, IClock clock, IObservable<bool> match, Action onComplete) public IDisposable Apply(Animatable control, IClock clock, IObservable<bool> match, Action onComplete)
{ {
var (animators, subscriptions) = InterpretKeyframes(control); var (animators, subscriptions) = InterpretKeyframes(control);
@ -323,21 +324,24 @@ namespace Avalonia.Animation
return new CompositeDisposable(subscriptions); return new CompositeDisposable(subscriptions);
} }
/// <inheritdocs/> /// <inheritdoc/>
public Task RunAsync(Animatable control, IClock clock = null) public Task RunAsync(Animatable control, IClock clock = null, CancellationToken cancellationToken = default)
{ {
var run = new TaskCompletionSource<object>(); var run = new TaskCompletionSource<object>();
if (this.IterationCount == IterationCount.Infinite) if (this.IterationCount == IterationCount.Infinite)
run.SetException(new InvalidOperationException("Looping animations must not use the Run method.")); run.SetException(new InvalidOperationException("Looping animations must not use the Run method."));
IDisposable subscriptions = null; IDisposable subscriptions = null, cancellation = null;
subscriptions = this.Apply(control, clock, Observable.Return(true), () => subscriptions = this.Apply(control, clock, Observable.Return(true), () =>
{ {
run.SetResult(null); run.SetResult(null);
subscriptions?.Dispose(); subscriptions?.Dispose();
cancellation?.Dispose();
}); });
cancellation = cancellationToken.Register(state => ((IDisposable)state).Dispose(), subscriptions);
return run.Task; return run.Task;
} }
} }

6
src/Avalonia.Animation/ApiCompatBaseline.txt

@ -0,0 +1,6 @@
Compat issues with assembly Avalonia.Animation:
MembersMustExist : Member 'public System.Threading.Tasks.Task Avalonia.Animation.Animation.RunAsync(Avalonia.Animation.Animatable, Avalonia.Animation.IClock)' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Threading.Tasks.Task Avalonia.Animation.IAnimation.RunAsync(Avalonia.Animation.Animatable, Avalonia.Animation.IClock)' is present in the contract but not in the implementation.
MembersMustExist : Member 'public System.Threading.Tasks.Task Avalonia.Animation.IAnimation.RunAsync(Avalonia.Animation.Animatable, Avalonia.Animation.IClock)' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Threading.Tasks.Task Avalonia.Animation.IAnimation.RunAsync(Avalonia.Animation.Animatable, Avalonia.Animation.IClock, System.Threading.CancellationToken)' is present in the implementation but not in the contract.
Total Issues: 4

3
src/Avalonia.Animation/IAnimation.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Avalonia.Animation namespace Avalonia.Animation
@ -16,6 +17,6 @@ namespace Avalonia.Animation
/// <summary> /// <summary>
/// Run the animation on the specified control. /// Run the animation on the specified control.
/// </summary> /// </summary>
Task RunAsync(Animatable control, IClock clock); Task RunAsync(Animatable control, IClock clock, CancellationToken cancellationToken);
} }
} }

Loading…
Cancel
Save