Browse Source

Remove references to "model index".

It's a TreeDataGrid concept (which is where this code was adapted from).
pull/9677/head
Steven Kirk 4 years ago
parent
commit
9610c80bd3
  1. 120
      src/Avalonia.Controls/VirtualizingStackPanel.cs

120
src/Avalonia.Controls/VirtualizingStackPanel.cs

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

Loading…
Cancel
Save