From a9f05f22c22565448244b5f7ef61f62b634c611e Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 20 Aug 2020 19:20:09 +0200 Subject: [PATCH] Added #nullable to SelectingItemsControl. --- .../Primitives/SelectingItemsControl.cs | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index 2eb8d62af8..78cad6f274 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -16,6 +16,8 @@ using Avalonia.Interactivity; using Avalonia.Logging; using Avalonia.VisualTree; +#nullable enable + namespace Avalonia.Controls.Primitives { /// @@ -61,8 +63,8 @@ namespace Avalonia.Controls.Primitives /// /// Defines the property. /// - public static readonly DirectProperty SelectedItemProperty = - AvaloniaProperty.RegisterDirect( + public static readonly DirectProperty SelectedItemProperty = + AvaloniaProperty.RegisterDirect( nameof(SelectedItem), o => o.SelectedItem, (o, v) => o.SelectedItem = v, @@ -105,13 +107,13 @@ namespace Avalonia.Controls.Primitives private static readonly IList Empty = Array.Empty(); private readonly Selection _selection = new Selection(); private int _selectedIndex = -1; - private object _selectedItem; - private IList _selectedItems; + private object? _selectedItem; + private IList? _selectedItems; private bool _ignoreContainerSelectionChanged; private bool _syncingSelectedItems; private int _updateCount; private int _updateSelectedIndex; - private object _updateSelectedItem; + private object? _updateSelectedItem; /// /// Initializes static members of the class. @@ -163,7 +165,7 @@ namespace Avalonia.Controls.Primitives /// /// Gets or sets the selected item. /// - public object SelectedItem + public object? SelectedItem { get => _selectedItem; set @@ -261,7 +263,7 @@ namespace Avalonia.Controls.Primitives /// /// The control that raised the event. /// The container or null if the event did not originate in a container. - protected IControl GetContainerFromEventSource(IInteractive eventSource) + protected IControl? GetContainerFromEventSource(IInteractive eventSource) { var parent = (IVisual)eventSource; @@ -290,7 +292,7 @@ namespace Avalonia.Controls.Primitives if (SelectedIndex != -1) { - newIndex = IndexOf((IEnumerable)e.NewValue, SelectedItem); + newIndex = IndexOf((IEnumerable?)e.NewValue, SelectedItem); } if (AlwaysSelected && Items != null && Items.Cast().Any()) @@ -483,7 +485,7 @@ namespace Avalonia.Controls.Primitives /// The direction to move. /// Whether to wrap when the selection reaches the first or last item. /// True if the selection was moved; otherwise false. - protected bool MoveSelection(IControl from, NavigationDirection direction, bool wrap) + protected bool MoveSelection(IControl? from, NavigationDirection direction, bool wrap) { if (Presenter?.Panel is INavigableContainer container && GetNextControl(container, direction, from, wrap) is IControl next) @@ -833,14 +835,14 @@ namespace Avalonia.Controls.Primitives /// /// The event sender. /// The event args. - private void SelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + private void SelectedItemsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { if (_syncingSelectedItems) { return; } - void Add(IList newItems, IList addedItems = null) + void Add(IList newItems, IList? addedItems = null) { foreach (var item in newItems) { @@ -865,8 +867,8 @@ namespace Avalonia.Controls.Primitives } } - IList added = null; - IList removed = null; + IList? added = null; + IList? removed = null; switch (e.Action) { @@ -983,8 +985,8 @@ namespace Avalonia.Controls.Primitives var item = ElementAt(Items, index); var itemChanged = !Equals(item, oldItem); var added = -1; - HashSet removedIndexes = null; - List removedItems = null; + HashSet? removedIndexes = null; + List? removedItems = null; _selectedIndex = index; _selectedItem = item; @@ -1011,7 +1013,7 @@ namespace Avalonia.Controls.Primitives } else { - removedItems ??= new List(); + removedItems ??= new List(); removedItems.Add(oldItem); } @@ -1050,7 +1052,7 @@ namespace Avalonia.Controls.Primitives if (removedIndexes is object) { - removedItems ??= new List(); + removedItems ??= new List(); foreach (var removed in removedIndexes) { @@ -1065,7 +1067,7 @@ namespace Avalonia.Controls.Primitives var e = new SelectionChangedEventArgs( SelectionChangedEvent, - (IList)removedItems ?? Array.Empty(), + (IList?)removedItems ?? Array.Empty(), added != -1 ? new[] { ElementAt(Items, added) } : Array.Empty()); RaiseEvent(e); }