Browse Source

Fix index clamping.

- Before we were clamping indexes too early, meaning that `firstIndexU` was calculated with a non-clamped index
- `_startUUnstable` needs to be set when the remove happens before the realized elements
pull/10285/head
Steven Kirk 4 years ago
parent
commit
10fae098b9
  1. 9
      src/Avalonia.Controls/VirtualizingStackPanel.cs

9
src/Avalonia.Controls/VirtualizingStackPanel.cs

@ -403,7 +403,7 @@ namespace Avalonia.Controls
if (firstIndex == -1) if (firstIndex == -1)
{ {
estimatedElementSize = EstimateElementSizeU(); estimatedElementSize = EstimateElementSizeU();
firstIndex = (int)(viewportStart / estimatedElementSize); firstIndex = Math.Min((int)(viewportStart / estimatedElementSize), maxIndex);
firstIndexU = firstIndex * estimatedElementSize; firstIndexU = firstIndex * estimatedElementSize;
} }
@ -411,13 +411,13 @@ namespace Avalonia.Controls
{ {
if (estimatedElementSize == -1) if (estimatedElementSize == -1)
estimatedElementSize = EstimateElementSizeU(); estimatedElementSize = EstimateElementSizeU();
lastIndex = (int)(viewportEnd / estimatedElementSize); lastIndex = Math.Min((int)(viewportEnd / estimatedElementSize), maxIndex);
} }
return new MeasureViewport return new MeasureViewport
{ {
firstIndex = MathUtilities.Clamp(firstIndex, 0, maxIndex), firstIndex = firstIndex,
lastIndex = MathUtilities.Clamp(lastIndex, 0, maxIndex), lastIndex = lastIndex,
viewportUStart = viewportStart, viewportUStart = viewportStart,
viewportUEnd = viewportEnd, viewportUEnd = viewportEnd,
startU = firstIndexU, startU = firstIndexU,
@ -1131,6 +1131,7 @@ namespace Avalonia.Controls
// The removed range was before the realized elements. Update the first index and // The removed range was before the realized elements. Update the first index and
// the indexes of the realized elements. // the indexes of the realized elements.
_firstIndex -= count; _firstIndex -= count;
_startUUnstable = true;
var newIndex = _firstIndex; var newIndex = _firstIndex;
for (var i = 0; i < _elements.Count; ++i) for (var i = 0; i < _elements.Count; ++i)

Loading…
Cancel
Save