From edeca6a4db9e5e7b1c77c8013a241b85ea6c332f Mon Sep 17 00:00:00 2001 From: Tom Edwards <109803929+TomEdwardsEnscape@users.noreply.github.com> Date: Wed, 28 May 2025 01:29:50 +0200 Subject: [PATCH] Fixed transitions with delay but no duration completing instantly (#18929) --- .../Animation/TransitionInstance.cs | 12 +++++- .../Animation/TransitionsTests.cs | 43 ++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Base/Animation/TransitionInstance.cs b/src/Avalonia.Base/Animation/TransitionInstance.cs index 9c9494ff87..676a30e81e 100644 --- a/src/Avalonia.Base/Animation/TransitionInstance.cs +++ b/src/Avalonia.Base/Animation/TransitionInstance.cs @@ -33,9 +33,17 @@ namespace Avalonia.Animation // ^- normalizedDelayEnd // [<---- normalizedInterpVal --->] - var normalizedInterpVal = 1d; + double normalizedInterpVal; - if (!MathUtilities.AreClose(_duration.TotalSeconds, 0d)) + if (t < _delay) + { + normalizedInterpVal = 0d; + } + else if (MathUtilities.AreClose(_duration.TotalSeconds, 0d)) + { + normalizedInterpVal = 1d; + } + else { var normalizedTotalDur = _delay + _duration; var normalizedDelayEnd = _delay.TotalSeconds / normalizedTotalDur.TotalSeconds; diff --git a/tests/Avalonia.Base.UnitTests/Animation/TransitionsTests.cs b/tests/Avalonia.Base.UnitTests/Animation/TransitionsTests.cs index 2737c2cebf..caaa29d05f 100644 --- a/tests/Avalonia.Base.UnitTests/Animation/TransitionsTests.cs +++ b/tests/Avalonia.Base.UnitTests/Animation/TransitionsTests.cs @@ -84,7 +84,8 @@ namespace Avalonia.Base.UnitTests.Animation var clock = new TestClock(); var i = -1; - + var completed = false; + new TransitionInstance(clock, TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(70)).Subscribe( nextValue => { @@ -124,12 +125,50 @@ namespace Avalonia.Base.UnitTests.Animation Assert.Equal(1d, nextValue); break; } - }); + }, () => completed = true); for (var z = 0; z <= 10; z++) { clock.Pulse(TimeSpan.FromMilliseconds(10)); } + + Assert.True(completed); + } + + [Fact] + public void TransitionInstance_With_Delay_But_Zero_Duration_Is_Completed_After_Delay() + { + var clock = new TestClock(); + + var i = -1; + var completed = false; + + new TransitionInstance(clock, TimeSpan.FromMilliseconds(30), TimeSpan.Zero).Subscribe( + nextValue => + { + switch (i++) + { + case 0: + Assert.Equal(0, nextValue); + break; + case 1: + Assert.Equal(0, nextValue); + break; + case 2: + Assert.Equal(0, nextValue); + break; + case 3: // one iteration sooner than the test above, because the start of the transition is also the end + Assert.Equal(1, nextValue); + break; + } + }, () => completed = true); + + for (var z = 0; z <= 4; z++) + { + clock.Pulse(TimeSpan.FromMilliseconds(10)); + } + + Assert.True(completed); } } }