From dd728dae5b76aab80f12917b1a862f7da8ac3666 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 21 Jul 2016 14:34:13 -0600 Subject: [PATCH] Fix measuring to infinity when scrolled to end. When a virtualized list was scrolled to the bottom and then the list was measured with a size larger than needed to fit all items (in this case we use infinity) then the virtualizer tries to go backwards to add items at the top of the currently visible items by setting `step = -1`; however it didn't check whether the current index was < 0. Fixes #589. --- .../Presenters/ItemVirtualizerSimple.cs | 2 +- ...emsPresenterTests_Virtualization_Simple.cs | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs b/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs index a54e502033..bea9cb8a1a 100644 --- a/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs +++ b/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs @@ -252,7 +252,7 @@ namespace Avalonia.Controls.Presenters var index = NextIndex; var step = 1; - while (!panel.IsFull) + while (!panel.IsFull && index >= 0) { if (index >= ItemCount) { diff --git a/tests/Avalonia.Controls.UnitTests/Presenters/ItemsPresenterTests_Virtualization_Simple.cs b/tests/Avalonia.Controls.UnitTests/Presenters/ItemsPresenterTests_Virtualization_Simple.cs index 34b074d185..264c2b2e89 100644 --- a/tests/Avalonia.Controls.UnitTests/Presenters/ItemsPresenterTests_Virtualization_Simple.cs +++ b/tests/Avalonia.Controls.UnitTests/Presenters/ItemsPresenterTests_Virtualization_Simple.cs @@ -326,6 +326,27 @@ namespace Avalonia.Controls.UnitTests.Presenters Assert.Equal(expected, actual); } + [Fact] + public void Measuring_To_Infinity_When_Scrolled_To_End_Should_Not_Throw() + { + var target = CreateTarget(useAvaloniaList: true); + + target.ApplyTemplate(); + target.Measure(new Size(100, 100)); + target.Arrange(new Rect(0, 0, 100, 100)); + + ((ILogicalScrollable)target).Offset = new Vector(0, 10); + + // Check for issue #589: this should not throw. + target.Measure(Size.Infinity); + + var expected = Enumerable.Range(0, 20).Select(x => $"Item {x}").ToList(); + var items = (AvaloniaList)target.Items; + var actual = target.Panel.Children.Select(x => x.DataContext).ToList(); + + Assert.Equal(expected, actual); + } + [Fact] public void Replacing_Items_Should_Update_Containers() {