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 incorrect cast in TreeSelectionModelBase #282

Merged
merged 1 commit into from
Apr 16, 2024
Merged
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
Expand Up @@ -204,7 +204,7 @@ public bool IsSelected(IndexPath index)

protected virtual bool TryGetItemAt(IndexPath index, out T? result)
{
var items = (IReadOnlyList<T>?)_root.ItemsView;
var items = (IEnumerable<T>?)_root.ItemsView;
var count = index.Count;

for (var i = 0; i < count; ++i)
Expand All @@ -215,17 +215,21 @@ protected virtual bool TryGetItemAt(IndexPath index, out T? result)
return false;
}

var j = index[i];

if (j < items.Count)
if (TryGetElementAt(items, index[i], out var item))
{
if (i == count - 1)
{
result = items[j];
result = item;
return true;
}
else
items = GetChildren(items[j]) as IReadOnlyList<T>;
{
items = GetChildren(item);
}
}
else
{
break;
}
}

Expand Down Expand Up @@ -566,6 +570,40 @@ internal static bool ShiftIndex(IndexPath parentIndex, int shiftIndex, int shift
return false;
}

private static bool TryGetElementAt(IEnumerable<T> items, int index, [MaybeNullWhen(false)] out T result)
{
if (items is IList<T> list)
{
if (index < list.Count)
{
result = list[index];
return true;
}
}
else if (items is IReadOnlyList<T> ro)
{
if (index < ro.Count)
{
result = ro[index];
return true;
}
}
else
{
foreach (var item in items)
{
if (index-- == 0)
{
result = item;
return true;
}
}
}

result = default;
return false;
}

public struct BatchUpdateOperation : IDisposable
{
private readonly TreeSelectionModelBase<T> _owner;
Expand Down
Loading