Browse Source

Add nullable annotations.

pull/3470/head
Steven Kirk 7 years ago
parent
commit
01a1945201
  1. 47
      src/Avalonia.Controls/SelectionModelChangeSet.cs
  2. 12
      src/Avalonia.Controls/SelectionModelSelectionChangedEventArgs.cs

47
src/Avalonia.Controls/SelectionModelChangeSet.cs

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
#nullable enable
namespace Avalonia.Controls namespace Avalonia.Controls
{ {
internal class SelectionModelChangeSet internal class SelectionModelChangeSet
@ -31,11 +33,11 @@ namespace Avalonia.Controls
_changes, _changes,
selectedCount, selectedCount,
GetSelectedIndexAt); GetSelectedIndexAt);
var deselectedItems = new SelectedItems<object, SelectionNodeOperation>( var deselectedItems = new SelectedItems<object?, SelectionNodeOperation>(
_changes, _changes,
deselectedCount, deselectedCount,
GetDeselectedItemAt); GetDeselectedItemAt);
var selectedItems = new SelectedItems<object, SelectionNodeOperation>( var selectedItems = new SelectedItems<object?, SelectionNodeOperation>(
_changes, _changes,
selectedCount, selectedCount,
GetSelectedItemAt); GetSelectedItemAt);
@ -52,7 +54,7 @@ namespace Avalonia.Controls
int index) int index)
{ {
static int GetCount(SelectionNodeOperation info) => info.DeselectedCount; static int GetCount(SelectionNodeOperation info) => info.DeselectedCount;
static List<IndexRange> GetRanges(SelectionNodeOperation info) => info.DeselectedRanges; static List<IndexRange>? GetRanges(SelectionNodeOperation info) => info.DeselectedRanges;
return GetIndexAt(infos, index, GetCount, GetRanges); return GetIndexAt(infos, index, GetCount, GetRanges);
} }
@ -61,25 +63,25 @@ namespace Avalonia.Controls
int index) int index)
{ {
static int GetCount(SelectionNodeOperation info) => info.SelectedCount; static int GetCount(SelectionNodeOperation info) => info.SelectedCount;
static List<IndexRange> GetRanges(SelectionNodeOperation info) => info.SelectedRanges; static List<IndexRange>? GetRanges(SelectionNodeOperation info) => info.SelectedRanges;
return GetIndexAt(infos, index, GetCount, GetRanges); return GetIndexAt(infos, index, GetCount, GetRanges);
} }
private object GetDeselectedItemAt( private object? GetDeselectedItemAt(
List<SelectionNodeOperation> infos, List<SelectionNodeOperation> infos,
int index) int index)
{ {
static int GetCount(SelectionNodeOperation info) => info.DeselectedCount; static int GetCount(SelectionNodeOperation info) => info.DeselectedCount;
static List<IndexRange> GetRanges(SelectionNodeOperation info) => info.DeselectedRanges; static List<IndexRange>? GetRanges(SelectionNodeOperation info) => info.DeselectedRanges;
return GetItemAt(infos, index, GetCount, GetRanges); return GetItemAt(infos, index, GetCount, GetRanges);
} }
private object GetSelectedItemAt( private object? GetSelectedItemAt(
List<SelectionNodeOperation> infos, List<SelectionNodeOperation> infos,
int index) int index)
{ {
static int GetCount(SelectionNodeOperation info) => info.SelectedCount; static int GetCount(SelectionNodeOperation info) => info.SelectedCount;
static List<IndexRange> GetRanges(SelectionNodeOperation info) => info.SelectedRanges; static List<IndexRange>? GetRanges(SelectionNodeOperation info) => info.SelectedRanges;
return GetItemAt(infos, index, GetCount, GetRanges); return GetItemAt(infos, index, GetCount, GetRanges);
} }
@ -87,7 +89,7 @@ namespace Avalonia.Controls
List<SelectionNodeOperation> infos, List<SelectionNodeOperation> infos,
int index, int index,
Func<SelectionNodeOperation, int> getCount, Func<SelectionNodeOperation, int> getCount,
Func<SelectionNodeOperation, List<IndexRange>> getRanges) Func<SelectionNodeOperation, List<IndexRange>?> getRanges)
{ {
var currentIndex = 0; var currentIndex = 0;
IndexPath path = default; IndexPath path = default;
@ -109,14 +111,14 @@ namespace Avalonia.Controls
return path; return path;
} }
private object GetItemAt( private object? GetItemAt(
List<SelectionNodeOperation> infos, List<SelectionNodeOperation> infos,
int index, int index,
Func<SelectionNodeOperation, int> getCount, Func<SelectionNodeOperation, int> getCount,
Func<SelectionNodeOperation, List<IndexRange>> getRanges) Func<SelectionNodeOperation, List<IndexRange>?> getRanges)
{ {
var currentIndex = 0; var currentIndex = 0;
object item = null; object? item = null;
foreach (var info in infos) foreach (var info in infos)
{ {
@ -125,7 +127,7 @@ namespace Avalonia.Controls
if (index >= currentIndex && index < currentIndex + currentCount) if (index >= currentIndex && index < currentIndex + currentCount)
{ {
int targetIndex = GetIndexAt(getRanges(info), index - currentIndex); int targetIndex = GetIndexAt(getRanges(info), index - currentIndex);
item = info.Items.GetAt(targetIndex); item = info.Items?.GetAt(targetIndex);
break; break;
} }
@ -135,20 +137,23 @@ namespace Avalonia.Controls
return item; return item;
} }
private int GetIndexAt(List<IndexRange> ranges, int index) private int GetIndexAt(List<IndexRange>? ranges, int index)
{ {
var currentIndex = 0; var currentIndex = 0;
foreach (var range in ranges) if (ranges != null)
{ {
var currentCount = (range.End - range.Begin) + 1; foreach (var range in ranges)
if (index >= currentIndex && index < currentIndex + currentCount)
{ {
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(); throw new IndexOutOfRangeException();

12
src/Avalonia.Controls/SelectionModelSelectionChangedEventArgs.cs

@ -15,13 +15,13 @@ namespace Avalonia.Controls
public SelectionModelSelectionChangedEventArgs( public SelectionModelSelectionChangedEventArgs(
IReadOnlyList<IndexPath>? deselectedIndices, IReadOnlyList<IndexPath>? deselectedIndices,
IReadOnlyList<IndexPath>? selectedIndices, IReadOnlyList<IndexPath>? selectedIndices,
IReadOnlyList<object>? deselectedItems, IReadOnlyList<object?>? deselectedItems,
IReadOnlyList<object>? selectedItems) IReadOnlyList<object?>? selectedItems)
{ {
DeselectedIndices = deselectedIndices ?? Array.Empty<IndexPath>(); DeselectedIndices = deselectedIndices ?? Array.Empty<IndexPath>();
SelectedIndices = selectedIndices ?? Array.Empty<IndexPath>(); SelectedIndices = selectedIndices ?? Array.Empty<IndexPath>();
DeselectedItems = deselectedItems ?? Array.Empty<object>(); DeselectedItems = deselectedItems ?? Array.Empty<object?>();
SelectedItems= selectedItems ?? Array.Empty<object>(); SelectedItems= selectedItems ?? Array.Empty<object?>();
} }
/// <summary> /// <summary>
@ -37,11 +37,11 @@ namespace Avalonia.Controls
/// <summary> /// <summary>
/// Gets the items that were removed from the selection. /// Gets the items that were removed from the selection.
/// </summary> /// </summary>
public IReadOnlyList<object> DeselectedItems { get; } public IReadOnlyList<object?> DeselectedItems { get; }
/// <summary> /// <summary>
/// Gets the items that were added to the selection. /// Gets the items that were added to the selection.
/// </summary> /// </summary>
public IReadOnlyList<object> SelectedItems { get; } public IReadOnlyList<object?> SelectedItems { get; }
} }
} }

Loading…
Cancel
Save