Skip to content

Commit 25fe106

Browse files
authored
Merge pull request #5203 from mike-ward/5151
Fix #5151 same as Microsoft
2 parents e1a8bb2 + a2ddc83 commit 25fe106

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed

src/Avalonia.Layout/ElementManager.cs

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void ClearRealizedRange(int realizedIndex, int count)
129129
{
130130
for (int i = 0; i < count; i++)
131131
{
132-
// Clear from the edges so that ItemsRepeater can optimize on maintaining
132+
// Clear from the edges so that ItemsRepeater can optimize on maintaining
133133
// realized indices without walking through all the children every time.
134134
int index = realizedIndex == 0 ? realizedIndex + i : (realizedIndex + count - 1) - i;
135135
var elementRef = _realizedElements[index];
@@ -212,7 +212,7 @@ public bool IsDataIndexRealized(int index)
212212
public ILayoutable GetRealizedElement(int dataIndex)
213213
{
214214
return IsVirtualizingContext ?
215-
GetAt(GetRealizedRangeIndexFromDataIndex(dataIndex)) :
215+
GetAt(GetRealizedRangeIndexFromDataIndex(dataIndex)) :
216216
_context.GetOrCreateElementAt(
217217
dataIndex,
218218
ElementRealizationOptions.ForceCreate | ElementRealizationOptions.SuppressAutoRecycle);
@@ -252,7 +252,6 @@ public bool IsWindowConnected(in Rect window, ScrollOrientation orientation, boo
252252
(orientation == ScrollOrientation.Vertical ? ScrollOrientation.Horizontal : ScrollOrientation.Vertical) :
253253
orientation;
254254

255-
256255
var windowStart = effectiveOrientation == ScrollOrientation.Vertical ? window.Y : window.X;
257256
var windowEnd = effectiveOrientation == ScrollOrientation.Vertical ? window.Y + window.Height : window.X + window.Width;
258257
var firstElementStart = effectiveOrientation == ScrollOrientation.Vertical ? firstElementBounds.Y : firstElementBounds.X;
@@ -273,53 +272,53 @@ public void DataSourceChanged(object source, NotifyCollectionChangedEventArgs ar
273272
switch (args.Action)
274273
{
275274
case NotifyCollectionChangedAction.Add:
276-
{
277-
OnItemsAdded(args.NewStartingIndex, args.NewItems.Count);
278-
}
279-
break;
275+
{
276+
OnItemsAdded(args.NewStartingIndex, args.NewItems.Count);
277+
}
278+
break;
280279

281280
case NotifyCollectionChangedAction.Replace:
282-
{
283-
int oldSize = args.OldItems.Count;
284-
int newSize = args.NewItems.Count;
285-
int oldStartIndex = args.OldStartingIndex;
286-
int newStartIndex = args.NewStartingIndex;
287-
288-
if (oldSize == newSize &&
289-
oldStartIndex == newStartIndex &&
290-
IsDataIndexRealized(oldStartIndex) &&
291-
IsDataIndexRealized(oldStartIndex + oldSize -1))
292281
{
293-
// Straight up replace of n items within the realization window.
294-
// Removing and adding might causes us to lose the anchor causing us
295-
// to throw away all containers and start from scratch.
296-
// Instead, we can just clear those items and set the element to
297-
// null (sentinel) and let the next measure get new containers for them.
298-
var startRealizedIndex = GetRealizedRangeIndexFromDataIndex(oldStartIndex);
299-
for (int realizedIndex = startRealizedIndex; realizedIndex < startRealizedIndex + oldSize; realizedIndex++)
282+
int oldSize = args.OldItems.Count;
283+
int newSize = args.NewItems.Count;
284+
int oldStartIndex = args.OldStartingIndex;
285+
int newStartIndex = args.NewStartingIndex;
286+
287+
if (oldSize == newSize &&
288+
oldStartIndex == newStartIndex &&
289+
IsDataIndexRealized(oldStartIndex) &&
290+
IsDataIndexRealized(oldStartIndex + oldSize - 1))
300291
{
301-
var elementRef = _realizedElements[realizedIndex];
302-
303-
if (elementRef != null)
292+
// Straight up replace of n items within the realization window.
293+
// Removing and adding might causes us to lose the anchor causing us
294+
// to throw away all containers and start from scratch.
295+
// Instead, we can just clear those items and set the element to
296+
// null (sentinel) and let the next measure get new containers for them.
297+
var startRealizedIndex = GetRealizedRangeIndexFromDataIndex(oldStartIndex);
298+
for (int realizedIndex = startRealizedIndex; realizedIndex < startRealizedIndex + oldSize; realizedIndex++)
304299
{
305-
_context.RecycleElement(elementRef);
306-
_realizedElements[realizedIndex] = null;
300+
var elementRef = _realizedElements[realizedIndex];
301+
302+
if (elementRef != null)
303+
{
304+
_context.RecycleElement(elementRef);
305+
_realizedElements[realizedIndex] = null;
306+
}
307307
}
308308
}
309+
else
310+
{
311+
OnItemsRemoved(oldStartIndex, oldSize);
312+
OnItemsAdded(newStartIndex, newSize);
313+
}
309314
}
310-
else
311-
{
312-
OnItemsRemoved(oldStartIndex, oldSize);
313-
OnItemsAdded(newStartIndex, newSize);
314-
}
315-
}
316-
break;
315+
break;
317316

318317
case NotifyCollectionChangedAction.Remove:
319-
{
320-
OnItemsRemoved(args.OldStartingIndex, args.OldItems.Count);
321-
}
322-
break;
318+
{
319+
OnItemsRemoved(args.OldStartingIndex, args.OldItems.Count);
320+
}
321+
break;
323322

324323
case NotifyCollectionChangedAction.Reset:
325324
ClearRealizedRange();
@@ -376,7 +375,7 @@ private void DiscardElementsOutsideWindow(in Rect window, ScrollOrientation orie
376375
int backCutoffIndex = realizedRangeSize;
377376

378377
for (int i = 0;
379-
i<realizedRangeSize &&
378+
i < realizedRangeSize &&
380379
!Intersects(window, _realizedElementLayoutBounds[i], orientation);
381380
++i)
382381
{
@@ -391,7 +390,7 @@ private void DiscardElementsOutsideWindow(in Rect window, ScrollOrientation orie
391390
--backCutoffIndex;
392391
}
393392

394-
if (backCutoffIndex<realizedRangeSize - 1)
393+
if (backCutoffIndex < realizedRangeSize - 1)
395394
{
396395
ClearRealizedRange(backCutoffIndex + 1, realizedRangeSize - backCutoffIndex - 1);
397396
}
@@ -419,14 +418,14 @@ private void OnItemsAdded(int index, int count)
419418
// to insert items.
420419
int lastRealizedDataIndex = _firstRealizedDataIndex + GetRealizedElementCount() - 1;
421420
int newStartingIndex = index;
422-
if (newStartingIndex > _firstRealizedDataIndex &&
421+
if (newStartingIndex >= _firstRealizedDataIndex &&
423422
newStartingIndex <= lastRealizedDataIndex)
424423
{
425424
// Inserted within the realized range
426425
int insertRangeStartIndex = newStartingIndex - _firstRealizedDataIndex;
427426
for (int i = 0; i < count; i++)
428427
{
429-
// Insert null (sentinel) here instead of an element, that way we dont
428+
// Insert null (sentinel) here instead of an element, that way we dont
430429
// end up creating a lot of elements only to be thrown out in the next layout.
431430
int insertRangeIndex = insertRangeStartIndex + i;
432431
int dataIndex = newStartingIndex + i;

0 commit comments

Comments
 (0)