Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RearrangeableListContainer<> crashing on replacement operations #6424

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -21,9 +19,8 @@ namespace osu.Framework.Tests.Visual.UserInterface
{
public partial class TestSceneRearrangeableListContainer : ManualInputManagerTestScene
{
private TestRearrangeableList list;

private Container listContainer;
private TestRearrangeableList list = null!;
private Container listContainer = null!;

[SetUp]
public void Setup() => Schedule(() =>
Expand Down Expand Up @@ -85,7 +82,7 @@ public void TestRemoveItem()

addItems(item_count);

List<Drawable> items = null;
List<Drawable> items = null!;

AddStep("get item references", () => items = new List<Drawable>(list.ItemMap.Values.ToList()));

Expand Down Expand Up @@ -278,7 +275,7 @@ public void TestNotScrolledToTopOnRemove()
[Test]
public void TestRemoveDuringLoadAndReAdd()
{
TestDelayedLoadRearrangeableList delayedList = null;
TestDelayedLoadRearrangeableList delayedList = null!;

AddStep("create list", () => Child = delayedList = new TestDelayedLoadRearrangeableList());

Expand Down Expand Up @@ -327,6 +324,24 @@ public void TestDragSynchronisation()
});
}

[Test]
public void TestReplaceEntireList()
{
addItems(1);

AddStep("replace list", () => list.Items.ReplaceRange(0, list.Items.Count, [100]));
AddUntilStep("wait for items to load", () => list.ItemMap.Values.All(i => i.IsLoaded));
}

[Test]
public void TestPartialReplace()
{
addItems(5);

AddStep("replace list", () => list.Items.ReplaceRange(2, 2, [100, 101]));
AddUntilStep("wait for items to load", () => list.ItemMap.Values.All(i => i.IsLoaded));
}

private void addDragSteps(int from, int to, int[] expectedSequence)
{
AddStep($"move to {from}", () =>
Expand Down
12 changes: 8 additions & 4 deletions osu.Framework/Graphics/Containers/RearrangeableListContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,15 @@ private void sortItems()
{
for (int i = 0; i < Items.Count; i++)
{
var drawable = itemMap[Items[i]];
// A drawable for the item may not exist yet, for example in a replace-range operation where the removal happens first.
if (!itemMap.TryGetValue(Items[i], out var drawable))
continue;

// The item may not be loaded yet, because add operations are asynchronous.
if (drawable.Parent != ListContainer)
continue;

// If the async load didn't complete, the item wouldn't exist in the container and an exception would be thrown
if (drawable.Parent == ListContainer)
ListContainer!.SetLayoutPosition(drawable, i);
ListContainer!.SetLayoutPosition(drawable, i);
}
}

Expand Down
Loading