1
0
Эх сурвалжийг харах

Fixed transitions with delay but no duration completing instantly (#18929)

Tom Edwards 4 сар өмнө
parent
commit
edeca6a4db

+ 10 - 2
src/Avalonia.Base/Animation/TransitionInstance.cs

@@ -33,9 +33,17 @@ namespace Avalonia.Animation
             //                   ^- normalizedDelayEnd
             //                   ^- normalizedDelayEnd
             //                    [<----   normalizedInterpVal   --->]
             //                    [<----   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 normalizedTotalDur = _delay + _duration;
                 var normalizedDelayEnd = _delay.TotalSeconds / normalizedTotalDur.TotalSeconds;
                 var normalizedDelayEnd = _delay.TotalSeconds / normalizedTotalDur.TotalSeconds;

+ 41 - 2
tests/Avalonia.Base.UnitTests/Animation/TransitionsTests.cs

@@ -84,7 +84,8 @@ namespace Avalonia.Base.UnitTests.Animation
             var clock = new TestClock();
             var clock = new TestClock();
 
 
             var i = -1;
             var i = -1;
-            
+            var completed = false;
+
             new TransitionInstance(clock, TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(70)).Subscribe(
             new TransitionInstance(clock, TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(70)).Subscribe(
                 nextValue =>
                 nextValue =>
                 {
                 {
@@ -124,12 +125,50 @@ namespace Avalonia.Base.UnitTests.Animation
                             Assert.Equal(1d, nextValue);
                             Assert.Equal(1d, nextValue);
                             break;
                             break;
                     }
                     }
-                });
+                }, () => completed = true);
 
 
             for (var z = 0; z <= 10; z++)
             for (var z = 0; z <= 10; z++)
             {
             {
                 clock.Pulse(TimeSpan.FromMilliseconds(10));
                 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);
         }
         }
     }
     }
 }
 }