Skip to content

Commit

Permalink
Merge pull request #7210 from AvaloniaUI/fixes/avalonia-layout-nullab…
Browse files Browse the repository at this point in the history
…ility

Added nullable annotations to Avalonia.Layout.
  • Loading branch information
grokys authored and danwalmsley committed Dec 20, 2021
1 parent 65a29ec commit f8b3992
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 111 deletions.
6 changes: 3 additions & 3 deletions src/Avalonia.Layout/AttachedLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ namespace Avalonia.Layout
/// </summary>
public abstract class AttachedLayout : AvaloniaObject
{
public string LayoutId { get; set; }
public string? LayoutId { get; set; }

/// <summary>
/// Occurs when the measurement state (layout) has been invalidated.
/// </summary>
public event EventHandler MeasureInvalidated;
public event EventHandler? MeasureInvalidated;

/// <summary>
/// Occurs when the arrange state (layout) has been invalidated.
/// </summary>
public event EventHandler ArrangeInvalidated;
public event EventHandler? ArrangeInvalidated;

/// <summary>
/// Initializes any per-container state the layout requires when it is attached to an
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Layout/Avalonia.Layout.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
</ItemGroup>
<Import Project="..\..\build\Rx.props" />
<Import Project="..\..\build\ApiDiff.props" />
<Import Project="..\..\build\NullableEnable.props" />
</Project>
46 changes: 22 additions & 24 deletions src/Avalonia.Layout/ElementManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ namespace Avalonia.Layout
{
internal class ElementManager
{
private readonly List<ILayoutable> _realizedElements = new List<ILayoutable>();
private readonly List<ILayoutable?> _realizedElements = new List<ILayoutable?>();
private readonly List<Rect> _realizedElementLayoutBounds = new List<Rect>();
private int _firstRealizedDataIndex;
private VirtualizingLayoutContext _context;
private VirtualizingLayoutContext? _context;

private bool IsVirtualizingContext
{
Expand Down Expand Up @@ -58,42 +58,40 @@ public void OnBeginMeasure(ScrollOrientation orientation)
// Make sure there is enough space for the bounds.
// Note: We could optimize when the count becomes smaller, but keeping
// it always up to date is the simplest option for now.
_realizedElementLayoutBounds.Resize(count);
_realizedElementLayoutBounds.Resize(count, default);
}
}
}
}

public int GetRealizedElementCount()
{
return IsVirtualizingContext ? _realizedElements.Count : _context.ItemCount;
return IsVirtualizingContext ? _realizedElements.Count : _context!.ItemCount;
}

public ILayoutable GetAt(int realizedIndex)
{
ILayoutable element;
ILayoutable? element;

if (IsVirtualizingContext)
{
if (_realizedElements[realizedIndex] == null)
element = _realizedElements[realizedIndex];

if (element == null)
{
// Sentinel. Create the element now since we need it.
int dataIndex = GetDataIndexFromRealizedRangeIndex(realizedIndex);
Logger.TryGet(LogEventLevel.Verbose, "Repeater")?.Log(this, "Creating element for sentinal with data index {Index}", dataIndex);
element = _context.GetOrCreateElementAt(
element = _context!.GetOrCreateElementAt(
dataIndex,
ElementRealizationOptions.ForceCreate | ElementRealizationOptions.SuppressAutoRecycle);
_realizedElements[realizedIndex] = element;
}
else
{
element = _realizedElements[realizedIndex];
}
}
else
{
// realizedIndex and dataIndex are the same (everything is realized)
element = _context.GetOrCreateElementAt(
element = _context!.GetOrCreateElementAt(
realizedIndex,
ElementRealizationOptions.ForceCreate | ElementRealizationOptions.SuppressAutoRecycle);
}
Expand All @@ -112,7 +110,7 @@ public void Add(ILayoutable element, int dataIndex)
_realizedElementLayoutBounds.Add(default);
}

public void Insert(int realizedIndex, int dataIndex, ILayoutable element)
public void Insert(int realizedIndex, int dataIndex, ILayoutable? element)
{
if (realizedIndex == 0)
{
Expand All @@ -136,7 +134,7 @@ public void ClearRealizedRange(int realizedIndex, int count)

if (elementRef != null)
{
_context.RecycleElement(elementRef);
_context!.RecycleElement(elementRef);
}
}

Expand Down Expand Up @@ -203,26 +201,26 @@ public bool IsDataIndexRealized(int index)
else
{
// Non virtualized - everything is realized
return index >= 0 && index < _context.ItemCount;
return index >= 0 && index < _context!.ItemCount;
}
}

public bool IsIndexValidInData(int currentIndex) => (uint)currentIndex < _context.ItemCount;
public bool IsIndexValidInData(int currentIndex) => (uint)currentIndex < _context!.ItemCount;

public ILayoutable GetRealizedElement(int dataIndex)
public ILayoutable? GetRealizedElement(int dataIndex)
{
return IsVirtualizingContext ?
GetAt(GetRealizedRangeIndexFromDataIndex(dataIndex)) :
_context.GetOrCreateElementAt(
_context!.GetOrCreateElementAt(
dataIndex,
ElementRealizationOptions.ForceCreate | ElementRealizationOptions.SuppressAutoRecycle);
}

public void EnsureElementRealized(bool forward, int dataIndex, string layoutId)
public void EnsureElementRealized(bool forward, int dataIndex, string? layoutId)
{
if (IsDataIndexRealized(dataIndex) == false)
{
var element = _context.GetOrCreateElementAt(
var element = _context!.GetOrCreateElementAt(
dataIndex,
ElementRealizationOptions.ForceCreate | ElementRealizationOptions.SuppressAutoRecycle);

Expand Down Expand Up @@ -273,14 +271,14 @@ public void DataSourceChanged(object source, NotifyCollectionChangedEventArgs ar
{
case NotifyCollectionChangedAction.Add:
{
OnItemsAdded(args.NewStartingIndex, args.NewItems.Count);
OnItemsAdded(args.NewStartingIndex, args.NewItems!.Count);
}
break;

case NotifyCollectionChangedAction.Replace:
{
int oldSize = args.OldItems.Count;
int newSize = args.NewItems.Count;
int oldSize = args.OldItems!.Count;
int newSize = args.NewItems!.Count;
int oldStartIndex = args.OldStartingIndex;
int newStartIndex = args.NewStartingIndex;

Expand All @@ -301,7 +299,7 @@ public void DataSourceChanged(object source, NotifyCollectionChangedEventArgs ar

if (elementRef != null)
{
_context.RecycleElement(elementRef);
_context!.RecycleElement(elementRef);
_realizedElements[realizedIndex] = null;
}
}
Expand Down
Loading

0 comments on commit f8b3992

Please sign in to comment.