Skip to content

Commit 9bf2b70

Browse files
grokysnil4
authored andcommitted
Port fixes from Avalonia.
Port fixes to `RealizedStackElements` from AvaloniaUI/Avalonia#13795.
1 parent 4da5a88 commit 9bf2b70

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/Avalonia.Controls.TreeDataGrid/Primitives/RealizedStackElements.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,13 @@ public void ItemsInserted(int index, int count, Action<Control, int, int> update
281281
// elements after the insertion point.
282282
var elementCount = _elements.Count;
283283
var start = Math.Max(realizedIndex, 0);
284-
var newIndex = realizedIndex + count;
285284

286285
for (var i = start; i < elementCount; ++i)
287286
{
288-
if (_elements[i] is Control element)
289-
updateElementIndex(element, (newIndex - count) + first, newIndex + first);
290-
++newIndex;
287+
if (_elements[i] is not Control element)
288+
continue;
289+
var oldIndex = i + first;
290+
updateElementIndex(element, oldIndex, oldIndex + count);
291291
}
292292

293293
if (realizedIndex < 0)
@@ -341,7 +341,7 @@ public void ItemsRemoved(
341341
for (var i = 0; i < _elements.Count; ++i)
342342
{
343343
if (_elements[i] is Control element)
344-
updateElementIndex(element, newIndex - count, newIndex);
344+
updateElementIndex(element, newIndex + count, newIndex);
345345
++newIndex;
346346
}
347347
}
@@ -384,6 +384,37 @@ public void ItemsRemoved(
384384
}
385385
}
386386

387+
/// <summary>
388+
/// Updates the elements in response to items being replaced in the source collection.
389+
/// </summary>
390+
/// <param name="index">The index in the source collection of the remove.</param>
391+
/// <param name="count">The number of items removed.</param>
392+
/// <param name="recycleElement">A method used to recycle elements.</param>
393+
public void ItemsReplaced(int index, int count, Action<Control> recycleElement)
394+
{
395+
if (index < 0)
396+
throw new ArgumentOutOfRangeException(nameof(index));
397+
if (_elements is null || _elements.Count == 0)
398+
return;
399+
400+
// Get the index within the realized _elements collection.
401+
var startIndex = index - FirstIndex;
402+
var endIndex = Math.Min(startIndex + count, Count);
403+
404+
if (startIndex >= 0 && endIndex > startIndex)
405+
{
406+
for (var i = startIndex; i < endIndex; ++i)
407+
{
408+
if (_elements[i] is { } element)
409+
{
410+
recycleElement(element);
411+
_elements[i] = null;
412+
_sizes![i] = double.NaN;
413+
}
414+
}
415+
}
416+
}
417+
387418
/// <summary>
388419
/// Recycles all elements in response to the source collection being reset.
389420
/// </summary>

src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridPresenterBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,8 @@ private void OnItemsCollectionChanged(object? sender, NotifyCollectionChangedEve
705705
_realizedElements.ItemsRemoved(e.OldStartingIndex, e.OldItems!.Count, _updateElementIndex, _recycleElementOnItemRemoved);
706706
break;
707707
case NotifyCollectionChangedAction.Replace:
708+
_realizedElements.ItemsReplaced(e.OldStartingIndex, e.OldItems!.Count, _recycleElementOnItemRemoved);
709+
break;
708710
case NotifyCollectionChangedAction.Move:
709711
_realizedElements.ItemsRemoved(e.OldStartingIndex, e.OldItems!.Count, _updateElementIndex, _recycleElementOnItemRemoved);
710712
_realizedElements.ItemsInserted(e.NewStartingIndex, e.NewItems!.Count, _updateElementIndex);

0 commit comments

Comments
 (0)