diff --git a/src/Avalonia.Controls/VirtualizingStackPanel.cs b/src/Avalonia.Controls/VirtualizingStackPanel.cs index 8471595a46..a02d963ff8 100644 --- a/src/Avalonia.Controls/VirtualizingStackPanel.cs +++ b/src/Avalonia.Controls/VirtualizingStackPanel.cs @@ -102,7 +102,7 @@ namespace Avalonia.Controls GenerateElements(availableSize, ref viewport); // Now we know what definitely fits, recycle anything left over. - _realizedElements.RecycleElementsAfter(_measureElements.LastModelIndex, _recycleElement); + _realizedElements.RecycleElementsAfter(_measureElements.LastIndex, _recycleElement); // And swap the measureElements and realizedElements collection. (_measureElements, _realizedElements) = (_realizedElements, _measureElements); @@ -310,8 +310,8 @@ namespace Avalonia.Controls var viewportStart = Orientation == Orientation.Horizontal ? viewport.X : viewport.Y; var viewportEnd = Orientation == Orientation.Horizontal ? viewport.Right : viewport.Bottom; - var (firstIndex, firstIndexU) = _realizedElements.GetModelIndexAt(viewportStart); - var (lastIndex, _) = _realizedElements.GetModelIndexAt(viewportEnd); + var (firstIndex, firstIndexU) = _realizedElements.GetIndexAt(viewportStart); + var (lastIndex, _) = _realizedElements.GetIndexAt(viewportEnd); var estimatedElementSize = -1.0; var itemCount = items?.Count ?? 0; @@ -595,14 +595,14 @@ namespace Avalonia.Controls public int Count => _elements?.Count ?? 0; /// - /// Gets the model index of the first realized element, or -1 if no elements are realized. + /// Gets the index of the first realized element, or -1 if no elements are realized. /// - public int FirstModelIndex => _elements?.Count > 0 ? _firstIndex : -1; + public int FirstIndex => _elements?.Count > 0 ? _firstIndex : -1; /// - /// Gets the model index of the last realized element, or -1 if no elements are realized. + /// Gets the index of the last realized element, or -1 if no elements are realized. /// - public int LastModelIndex => _elements?.Count > 0 ? _firstIndex + _elements.Count - 1 : -1; + public int LastIndex => _elements?.Count > 0 ? _firstIndex + _elements.Count - 1 : -1; /// /// Gets the elements. @@ -622,14 +622,14 @@ namespace Avalonia.Controls /// /// Adds a newly realized element to the collection. /// - /// The model index of the element. + /// The index of the element. /// The element. /// The position of the elemnt on the primary axis. /// The size of the element on the primary axis. - public void Add(int modelIndex, Control element, double u, double sizeU) + public void Add(int index, Control element, double u, double sizeU) { - if (modelIndex < 0) - throw new ArgumentOutOfRangeException(nameof(modelIndex)); + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); _elements ??= new List(); _sizes ??= new List(); @@ -639,14 +639,14 @@ namespace Avalonia.Controls _elements.Add(element); _sizes.Add(sizeU); _startU = u; - _firstIndex = modelIndex; + _firstIndex = index; } - else if (modelIndex == LastModelIndex + 1) + else if (index == LastIndex + 1) { _elements.Add(element); _sizes.Add(sizeU); } - else if (modelIndex == FirstModelIndex - 1) + else if (index == FirstIndex - 1) { --_firstIndex; _elements.Insert(0, element); @@ -660,20 +660,20 @@ namespace Avalonia.Controls } /// - /// Gets the element at the specified model index, if realized. + /// Gets the element at the specified index, if realized. /// - /// The index in the source collection of the element to get. + /// The index in the source collection of the element to get. /// The element if realized; otherwise null. - public Control? GetElement(int modelIndex) + public Control? GetElement(int index) { - var index = modelIndex - FirstModelIndex; - if (index >= 0 && index < _elements?.Count) - return _elements[index]; + var i = index - FirstIndex; + if (i >= 0 && i < _elements?.Count) + return _elements[i]; return null; } /// - /// Gets the model index and start U position of the element at the specified U position. + /// Gets the index and start U position of the element at the specified U position. /// /// The U position. /// @@ -682,7 +682,7 @@ namespace Avalonia.Controls /// determined /// - The U position of the start of the item, if determined /// - public (int index, double position) GetModelIndexAt(double u) + public (int index, double position) GetIndexAt(double u) { if (_elements is null || _sizes is null || _startU > u || _startUUnstable) return (-1, 0); @@ -696,7 +696,7 @@ namespace Avalonia.Controls if (double.IsNaN(size)) break; if (u >= position && u < position + size) - return (index + FirstModelIndex, position); + return (index + FirstIndex, position); position += size; ++index; } @@ -718,7 +718,7 @@ namespace Avalonia.Controls return (-1, 0); var u = StartU; - var i = FirstModelIndex; + var i = FirstIndex; foreach (var size in _sizes) { @@ -746,10 +746,10 @@ namespace Avalonia.Controls var divisor = 0.0; // Start by averaging the size of the elements before the first realized element. - if (FirstModelIndex >= 0 && !_startUUnstable) + if (FirstIndex >= 0 && !_startUUnstable) { total += _startU; - divisor += FirstModelIndex; + divisor += FirstIndex; } // Average the size of the realized elements. @@ -772,38 +772,38 @@ namespace Avalonia.Controls } /// - /// Gets the model index of the specified element. + /// Gets the index of the specified element. /// /// The element. - /// The model index or -1 if the element is not present in the collection. + /// The index or -1 if the element is not present in the collection. public int GetIndex(Control element) { - return _elements?.IndexOf(element) is int index && index >= 0 ? index + FirstModelIndex : -1; + return _elements?.IndexOf(element) is int index && index >= 0 ? index + FirstIndex : -1; } /// /// Updates the elements in response to items being inserted into the source collection. /// - /// The index in the source collection of the insert. + /// The index in the source collection of the insert. /// The number of items inserted. /// A method used to update the element indexes. - public void ItemsInserted(int modelIndex, int count, Action updateElementIndex) + public void ItemsInserted(int index, int count, Action updateElementIndex) { - if (modelIndex < 0) - throw new ArgumentOutOfRangeException(nameof(modelIndex)); + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); if (_elements is null || _elements.Count == 0) return; // Get the index within the realized _elements collection. - var first = FirstModelIndex; - var index = modelIndex - first; + var first = FirstIndex; + var realizedIndex = index - first; - if (index < Count) + if (realizedIndex < Count) { // The insertion point affects the realized elements. Update the index of the // elements after the insertion point. var elementCount = _elements.Count; - var start = Math.Max(index, 0); + var start = Math.Max(realizedIndex, 0); var newIndex = first + count; for (var i = start; i < elementCount; ++i) @@ -813,7 +813,7 @@ namespace Avalonia.Controls ++newIndex; } - if (index <= 0) + if (realizedIndex <= 0) { // The insertion point was before the first element, update the first index. _firstIndex += count; @@ -822,8 +822,8 @@ namespace Avalonia.Controls { // The insertion point was within the realized elements, insert an empty space // in _elements and _sizes. - _elements!.InsertMany(index, null, count); - _sizes!.InsertMany(index, double.NaN, count); + _elements!.InsertMany(realizedIndex, null, count); + _sizes!.InsertMany(realizedIndex, double.NaN, count); } } } @@ -831,26 +831,26 @@ namespace Avalonia.Controls /// /// Updates the elements in response to items being removed from the source collection. /// - /// The index in the source collection of the remove. + /// The index in the source collection of the remove. /// The number of items removed. /// A method used to update the element indexes. /// A method used to recycle elements. public void ItemsRemoved( - int modelIndex, + int index, int count, Action updateElementIndex, Action recycleElement) { - if (modelIndex < 0) - throw new ArgumentOutOfRangeException(nameof(modelIndex)); + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); if (_elements is null || _elements.Count == 0) return; // Get the removal start and end index within the realized _elements collection. - var first = FirstModelIndex; - var last = LastModelIndex; - var startIndex = modelIndex - first; - var endIndex = (modelIndex + count) - first; + var first = FirstIndex; + var last = LastIndex; + var startIndex = index - first; + var endIndex = (index + count) - first; if (endIndex < 0) { @@ -886,7 +886,7 @@ namespace Avalonia.Controls // because we can't rely on it now to estimate element heights. if (startIndex <= 0 && end < last) { - _firstIndex = first = modelIndex; + _firstIndex = first = index; _startUUnstable = true; } @@ -905,20 +905,20 @@ namespace Avalonia.Controls /// /// Recycles elements before a specific index. /// - /// The index in the source collection of new first element. + /// The index in the source collection of new first element. /// A method used to recycle elements. - public void RecycleElementsBefore(int modelIndex, Action recycleElement) + public void RecycleElementsBefore(int index, Action recycleElement) { - if (modelIndex <= FirstModelIndex || _elements is null || _elements.Count == 0) + if (index <= FirstIndex || _elements is null || _elements.Count == 0) return; - if (modelIndex > LastModelIndex) + if (index > LastIndex) { RecycleAllElements(recycleElement); } else { - var endIndex = modelIndex - FirstModelIndex; + var endIndex = index - FirstIndex; for (var i = 0; i < endIndex; ++i) { @@ -928,27 +928,27 @@ namespace Avalonia.Controls _elements.RemoveRange(0, endIndex); _sizes!.RemoveRange(0, endIndex); - _firstIndex = modelIndex; + _firstIndex = index; } } /// /// Recycles elements after a specific index. /// - /// The index in the source collection of new last element. + /// The index in the source collection of new last element. /// A method used to recycle elements. - public void RecycleElementsAfter(int modelIndex, Action recycleElement) + public void RecycleElementsAfter(int index, Action recycleElement) { - if (modelIndex >= LastModelIndex || _elements is null || _elements.Count == 0) + if (index >= LastIndex || _elements is null || _elements.Count == 0) return; - if (modelIndex < FirstModelIndex) + if (index < FirstIndex) { RecycleAllElements(recycleElement); } else { - var startIndex = (modelIndex + 1) - FirstModelIndex; + var startIndex = (index + 1) - FirstIndex; var count = _elements.Count; for (var i = startIndex; i < count; ++i)