Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Avalonia.Base/Animation/AnimationInstance`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ protected override void Subscribed()
}

// Stop and dispose the animation when detached from the visual tree.
_detachedHandler = (_, _) => DoComplete();
_detachedHandler = (_, _) =>
{
SetFinalValue();
DoComplete();
};
visual.DetachedFromVisualTree += _detachedHandler;
}

Expand All @@ -172,6 +176,12 @@ public void Step(TimeSpan frameTick)
}
}

private void SetFinalValue()
{
var easedTime = _easeFunc!.Ease(_playbackReversed ? 0.0 : 1.0);
_lastInterpValue = _interpolator(easedTime, _neutralValue);
}

private void ApplyFinalFill()
{
if (_animator.Property is null)
Expand Down Expand Up @@ -237,8 +247,7 @@ private void InternalStep(TimeSpan time)
// when the duration is set to zero while animating and snap to the last iterated value.
if (_currentIteration + 1 > _iterationCount || _duration == TimeSpan.Zero)
{
var easedTime = _easeFunc!.Ease(_playbackReversed ? 0.0 : 1.0);
_lastInterpValue = _interpolator(easedTime, _neutralValue);
SetFinalValue();
DoComplete();
return;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Avalonia.Base.UnitTests/Animation/AnimationIterationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,44 @@ public void Auto_Does_Not_Pause_On_Invisible_When_Started_Manually()
Assert.Equal(200d, border.Width);
}

[Fact]
public void FillMode_Applies_Final_Value_When_Visual_Detached_During_Animation()
{
var keyframe1 = new KeyFrame
{
Setters = { new Setter(Layoutable.WidthProperty, 100d) },
Cue = new Cue(0d)
};
var keyframe2 = new KeyFrame
{
Setters = { new Setter(Layoutable.WidthProperty, 300d) },
Cue = new Cue(1d)
};

var animation = new Animation
{
Duration = TimeSpan.FromSeconds(5),
IterationCount = new IterationCount(1),
FillMode = FillMode.Forward,
Children = { keyframe1, keyframe2 }
};

var border = new Border { Height = 100d, Width = 50d };
var root = new TestRoot(border);
var clock = new TestClock();
var animationRun = animation.RunAsync(border, clock, TestContext.Current.CancellationToken);

clock.Step(TimeSpan.Zero);
Assert.Equal(100d, border.Width);

// Detach from visual tree immediately
root.Child = null;

// The final value should be applied
Assert.True(animationRun.IsCompleted);
Assert.Equal(300d, border.Width);
}

private sealed class FakeAnimator : InterpolatingAnimator<double>
{
public double LastProgress { get; set; } = double.NaN;
Expand Down