diff --git a/src/Avalonia.Controls/SelectionModelChangeSet.cs b/src/Avalonia.Controls/SelectionModelChangeSet.cs index c0228a1cbc..57bf369585 100644 --- a/src/Avalonia.Controls/SelectionModelChangeSet.cs +++ b/src/Avalonia.Controls/SelectionModelChangeSet.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +#nullable enable + namespace Avalonia.Controls { internal class SelectionModelChangeSet @@ -31,11 +33,11 @@ namespace Avalonia.Controls _changes, selectedCount, GetSelectedIndexAt); - var deselectedItems = new SelectedItems( + var deselectedItems = new SelectedItems( _changes, deselectedCount, GetDeselectedItemAt); - var selectedItems = new SelectedItems( + var selectedItems = new SelectedItems( _changes, selectedCount, GetSelectedItemAt); @@ -52,7 +54,7 @@ namespace Avalonia.Controls int index) { static int GetCount(SelectionNodeOperation info) => info.DeselectedCount; - static List GetRanges(SelectionNodeOperation info) => info.DeselectedRanges; + static List? GetRanges(SelectionNodeOperation info) => info.DeselectedRanges; return GetIndexAt(infos, index, GetCount, GetRanges); } @@ -61,25 +63,25 @@ namespace Avalonia.Controls int index) { static int GetCount(SelectionNodeOperation info) => info.SelectedCount; - static List GetRanges(SelectionNodeOperation info) => info.SelectedRanges; + static List? GetRanges(SelectionNodeOperation info) => info.SelectedRanges; return GetIndexAt(infos, index, GetCount, GetRanges); } - private object GetDeselectedItemAt( + private object? GetDeselectedItemAt( List infos, int index) { static int GetCount(SelectionNodeOperation info) => info.DeselectedCount; - static List GetRanges(SelectionNodeOperation info) => info.DeselectedRanges; + static List? GetRanges(SelectionNodeOperation info) => info.DeselectedRanges; return GetItemAt(infos, index, GetCount, GetRanges); } - private object GetSelectedItemAt( + private object? GetSelectedItemAt( List infos, int index) { static int GetCount(SelectionNodeOperation info) => info.SelectedCount; - static List GetRanges(SelectionNodeOperation info) => info.SelectedRanges; + static List? GetRanges(SelectionNodeOperation info) => info.SelectedRanges; return GetItemAt(infos, index, GetCount, GetRanges); } @@ -87,7 +89,7 @@ namespace Avalonia.Controls List infos, int index, Func getCount, - Func> getRanges) + Func?> getRanges) { var currentIndex = 0; IndexPath path = default; @@ -109,14 +111,14 @@ namespace Avalonia.Controls return path; } - private object GetItemAt( + private object? GetItemAt( List infos, int index, Func getCount, - Func> getRanges) + Func?> getRanges) { var currentIndex = 0; - object item = null; + object? item = null; foreach (var info in infos) { @@ -125,7 +127,7 @@ namespace Avalonia.Controls if (index >= currentIndex && index < currentIndex + currentCount) { int targetIndex = GetIndexAt(getRanges(info), index - currentIndex); - item = info.Items.GetAt(targetIndex); + item = info.Items?.GetAt(targetIndex); break; } @@ -135,20 +137,23 @@ namespace Avalonia.Controls return item; } - private int GetIndexAt(List ranges, int index) + private int GetIndexAt(List? ranges, int index) { var currentIndex = 0; - foreach (var range in ranges) + if (ranges != null) { - var currentCount = (range.End - range.Begin) + 1; - - if (index >= currentIndex && index < currentIndex + currentCount) + foreach (var range in ranges) { - return range.Begin + (index - currentIndex); - } + var currentCount = (range.End - range.Begin) + 1; - currentIndex += currentCount; + if (index >= currentIndex && index < currentIndex + currentCount) + { + return range.Begin + (index - currentIndex); + } + + currentIndex += currentCount; + } } throw new IndexOutOfRangeException(); diff --git a/src/Avalonia.Controls/SelectionModelSelectionChangedEventArgs.cs b/src/Avalonia.Controls/SelectionModelSelectionChangedEventArgs.cs index 4e64ee6e6f..5e2efdf331 100644 --- a/src/Avalonia.Controls/SelectionModelSelectionChangedEventArgs.cs +++ b/src/Avalonia.Controls/SelectionModelSelectionChangedEventArgs.cs @@ -15,13 +15,13 @@ namespace Avalonia.Controls public SelectionModelSelectionChangedEventArgs( IReadOnlyList? deselectedIndices, IReadOnlyList? selectedIndices, - IReadOnlyList? deselectedItems, - IReadOnlyList? selectedItems) + IReadOnlyList? deselectedItems, + IReadOnlyList? selectedItems) { DeselectedIndices = deselectedIndices ?? Array.Empty(); SelectedIndices = selectedIndices ?? Array.Empty(); - DeselectedItems = deselectedItems ?? Array.Empty(); - SelectedItems= selectedItems ?? Array.Empty(); + DeselectedItems = deselectedItems ?? Array.Empty(); + SelectedItems= selectedItems ?? Array.Empty(); } /// @@ -37,11 +37,11 @@ namespace Avalonia.Controls /// /// Gets the items that were removed from the selection. /// - public IReadOnlyList DeselectedItems { get; } + public IReadOnlyList DeselectedItems { get; } /// /// Gets the items that were added to the selection. /// - public IReadOnlyList SelectedItems { get; } + public IReadOnlyList SelectedItems { get; } } }