diff --git a/src/Avalonia.Base/Collections/Pooled/ClearMode.cs b/src/Avalonia.Base/Collections/Pooled/ClearMode.cs
new file mode 100644
index 0000000000..d78ac8feab
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/ClearMode.cs
@@ -0,0 +1,40 @@
+// This source file is adapted from the Collections.Pooled.
+// (https://github.com/jtmueller/Collections.Pooled/tree/master/Collections.Pooled/)
+
+namespace Avalonia.Collections.Pooled
+{
+ ///
+ /// This enum allows control over how data is treated when internal
+ /// arrays are returned to the ArrayPool. Be careful to understand
+ /// what each option does before using anything other than the default
+ /// of Auto.
+ ///
+ public enum ClearMode
+ {
+ ///
+ /// Auto has different behavior depending on the host project's target framework.
+ /// .NET Core 2.1: Reference types and value types that contain reference types are cleared
+ /// when the internal arrays are returned to the pool. Value types that do not contain reference
+ /// types are not cleared when returned to the pool.
+ /// .NET Standard 2.0: All user types are cleared before returning to the pool, in case they
+ /// contain reference types.
+ /// For .NET Standard, Auto and Always have the same behavior.
+ ///
+ Auto = 0,
+ ///
+ /// The Always setting has the effect of always clearing user types before returning to the pool.
+ /// This is the default behavior on .NET Standard.You might want to turn this on in a .NET Core project
+ /// if you were concerned about sensitive data stored in value types leaking to other pars of your application.
+ ///
+ Always = 1,
+ ///
+ /// Never will cause pooled collections to never clear user types before returning them to the pool.
+ /// You might want to use this setting in a .NET Standard project when you know that a particular collection stores
+ /// only value types and you want the performance benefit of not taking time to reset array items to their default value.
+ /// Be careful with this setting: if used for a collection that contains reference types, or value types that contain
+ /// reference types, this setting could cause memory issues by making the garbage collector unable to clean up instances
+ /// that are still being referenced by arrays sitting in the ArrayPool.
+ ///
+ Never = 2
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/ICollectionDebugView.cs b/src/Avalonia.Base/Collections/Pooled/ICollectionDebugView.cs
new file mode 100644
index 0000000000..2b15388a13
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/ICollectionDebugView.cs
@@ -0,0 +1,31 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+
+namespace Avalonia.Collections.Pooled
+{
+ internal sealed class ICollectionDebugView
+ {
+ private readonly ICollection _collection;
+
+ public ICollectionDebugView(ICollection collection)
+ {
+ _collection = collection ?? throw new ArgumentNullException(nameof(collection));
+ }
+
+ [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
+ public T[] Items
+ {
+ get
+ {
+ T[] items = new T[_collection.Count];
+ _collection.CopyTo(items, 0);
+ return items;
+ }
+ }
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/IReadOnlyPooledList.cs b/src/Avalonia.Base/Collections/Pooled/IReadOnlyPooledList.cs
new file mode 100644
index 0000000000..9bc3609dc5
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/IReadOnlyPooledList.cs
@@ -0,0 +1,21 @@
+// This source file is adapted from the Collections.Pooled.
+// (https://github.com/jtmueller/Collections.Pooled/tree/master/Collections.Pooled/)
+
+using System;
+using System.Collections.Generic;
+
+namespace Avalonia.Collections.Pooled
+{
+ ///
+ /// Represents a read-only collection of pooled elements that can be accessed by index
+ ///
+ /// The type of elements in the read-only pooled list.
+
+ public interface IReadOnlyPooledList : IReadOnlyList
+ {
+ ///
+ /// Gets a for the items currently in the collection.
+ ///
+ ReadOnlySpan Span { get; }
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/PooledList.cs b/src/Avalonia.Base/Collections/Pooled/PooledList.cs
new file mode 100644
index 0000000000..f0d6b292cc
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/PooledList.cs
@@ -0,0 +1,1531 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Buffers;
+using System.Collections;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
+using System.Runtime.Serialization;
+using System.Threading;
+
+namespace Avalonia.Collections.Pooled
+{
+ ///
+ /// Implements a variable-size list that uses a pooled array to store the
+ /// elements. A PooledList has a capacity, which is the allocated length
+ /// of the internal array. As elements are added to a PooledList, the capacity
+ /// of the PooledList is automatically increased as required by reallocating the
+ /// internal array.
+ ///
+ ///
+ /// This class is based on the code for but it supports
+ /// and uses when allocating internal arrays.
+ ///
+ [DebuggerDisplay("Count = {Count}")]
+ [DebuggerTypeProxy(typeof(ICollectionDebugView<>))]
+ [Serializable]
+ public class PooledList : IList, IReadOnlyPooledList, IList, IDisposable, IDeserializationCallback
+ {
+ // internal constant copied from Array.MaxArrayLength
+ private const int MaxArrayLength = 0x7FEFFFFF;
+ private const int DefaultCapacity = 4;
+ private static readonly T[] s_emptyArray = Array.Empty();
+
+ [NonSerialized]
+ private ArrayPool _pool;
+ [NonSerialized]
+ private object _syncRoot;
+
+ private T[] _items; // Do not rename (binary serialization)
+ private int _size; // Do not rename (binary serialization)
+ private int _version; // Do not rename (binary serialization)
+ private readonly bool _clearOnFree;
+
+ #region Constructors
+
+ ///
+ /// Constructs a PooledList. The list is initially empty and has a capacity
+ /// of zero. Upon adding the first element to the list the capacity is
+ /// increased to DefaultCapacity, and then increased in multiples of two
+ /// as required.
+ ///
+ public PooledList() : this(ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList. The list is initially empty and has a capacity
+ /// of zero. Upon adding the first element to the list the capacity is
+ /// increased to DefaultCapacity, and then increased in multiples of two
+ /// as required.
+ ///
+ public PooledList(ClearMode clearMode) : this(clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList. The list is initially empty and has a capacity
+ /// of zero. Upon adding the first element to the list the capacity is
+ /// increased to DefaultCapacity, and then increased in multiples of two
+ /// as required.
+ ///
+ public PooledList(ArrayPool customPool) : this(ClearMode.Auto, customPool) { }
+
+ ///
+ /// Constructs a PooledList. The list is initially empty and has a capacity
+ /// of zero. Upon adding the first element to the list the capacity is
+ /// increased to DefaultCapacity, and then increased in multiples of two
+ /// as required.
+ ///
+ public PooledList(ClearMode clearMode, ArrayPool customPool)
+ {
+ _items = s_emptyArray;
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+ }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity) : this(capacity, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, bool sizeToCapacity) : this(capacity, ClearMode.Auto, ArrayPool.Shared, sizeToCapacity) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, ClearMode clearMode) : this(capacity, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, ClearMode clearMode, bool sizeToCapacity) : this(capacity, clearMode, ArrayPool.Shared, sizeToCapacity) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, ArrayPool customPool) : this(capacity, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, ArrayPool customPool, bool sizeToCapacity) : this(capacity, ClearMode.Auto, customPool, sizeToCapacity) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, ClearMode clearMode, ArrayPool customPool) : this(capacity, clearMode, customPool, false) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ /// If true, Count of list equals capacity. Depending on ClearMode, rented items may or may not hold dirty values.
+ public PooledList(int capacity, ClearMode clearMode, ArrayPool customPool, bool sizeToCapacity)
+ {
+ if (capacity < 0)
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+
+ if (capacity == 0)
+ {
+ _items = s_emptyArray;
+ }
+ else
+ {
+ _items = _pool.Rent(capacity);
+ }
+
+ if (sizeToCapacity)
+ {
+ _size = capacity;
+ if (clearMode != ClearMode.Never)
+ {
+ Array.Clear(_items, 0, _size);
+ }
+ }
+ }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(T[] array) : this(array.AsSpan(), ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(T[] array, ClearMode clearMode) : this(array.AsSpan(), clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(T[] array, ArrayPool customPool) : this(array.AsSpan(), ClearMode.Auto, customPool) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(T[] array, ClearMode clearMode, ArrayPool customPool) : this(array.AsSpan(), clearMode, customPool) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(ReadOnlySpan span) : this(span, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(ReadOnlySpan span, ClearMode clearMode) : this(span, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(ReadOnlySpan span, ArrayPool customPool) : this(span, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(ReadOnlySpan span, ClearMode clearMode, ArrayPool customPool)
+ {
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+
+ int count = span.Length;
+ if (count == 0)
+ {
+ _items = s_emptyArray;
+ }
+ else
+ {
+ _items = _pool.Rent(count);
+ span.CopyTo(_items);
+ _size = count;
+ }
+ }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(IEnumerable collection) : this(collection, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(IEnumerable collection, ClearMode clearMode) : this(collection, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(IEnumerable collection, ArrayPool customPool) : this(collection, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(IEnumerable collection, ClearMode clearMode, ArrayPool customPool)
+ {
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+
+ switch (collection)
+ {
+ case null:
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
+ break;
+
+ case ICollection c:
+ int count = c.Count;
+ if (count == 0)
+ {
+ _items = s_emptyArray;
+ }
+ else
+ {
+ _items = _pool.Rent(count);
+ c.CopyTo(_items, 0);
+ _size = count;
+ }
+ break;
+
+ default:
+ _size = 0;
+ _items = s_emptyArray;
+ using (var en = collection.GetEnumerator())
+ {
+ while (en.MoveNext())
+ Add(en.Current);
+ }
+ break;
+ }
+ }
+
+ #endregion
+
+ ///
+ /// Gets a for the items currently in the collection.
+ ///
+ public Span Span => _items.AsSpan(0, _size);
+
+ ///
+ ReadOnlySpan IReadOnlyPooledList.Span => Span;
+
+ ///
+ /// Gets and sets the capacity of this list. The capacity is the size of
+ /// the internal array used to hold items. When set, the internal
+ /// Memory of the list is reallocated to the given capacity.
+ /// Note that the return value for this property may be larger than the property was set to.
+ ///
+ public int Capacity
+ {
+ get => _items.Length;
+ set
+ {
+ if (value < _size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
+ }
+
+ if (value != _items.Length)
+ {
+ if (value > 0)
+ {
+ var newItems = _pool.Rent(value);
+ if (_size > 0)
+ {
+ Array.Copy(_items, newItems, _size);
+ }
+ ReturnArray();
+ _items = newItems;
+ }
+ else
+ {
+ ReturnArray();
+ _size = 0;
+ }
+ }
+ }
+ }
+
+ ///
+ /// Read-only property describing how many elements are in the List.
+ ///
+ public int Count => _size;
+
+ ///
+ /// Returns the ClearMode behavior for the collection, denoting whether values are
+ /// cleared from internal arrays before returning them to the pool.
+ ///
+ public ClearMode ClearMode => _clearOnFree ? ClearMode.Always : ClearMode.Never;
+
+ bool IList.IsFixedSize => false;
+
+ bool ICollection.IsReadOnly => false;
+
+ bool IList.IsReadOnly => false;
+
+ int ICollection.Count => _size;
+
+ bool ICollection.IsSynchronized => false;
+
+ // Synchronization root for this object.
+ object ICollection.SyncRoot
+ {
+ get
+ {
+ if (_syncRoot == null)
+ {
+ Interlocked.CompareExchange(ref _syncRoot, new object(), null);
+ }
+ return _syncRoot;
+ }
+ }
+
+ ///
+ /// Gets or sets the element at the given index.
+ ///
+ public T this[int index]
+ {
+ get
+ {
+ // Following trick can reduce the range check by one
+ if ((uint)index >= (uint)_size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+ }
+ return _items[index];
+ }
+
+ set
+ {
+ if ((uint)index >= (uint)_size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+ }
+ _items[index] = value;
+ _version++;
+ }
+ }
+
+ private static bool IsCompatibleObject(object value)
+ {
+ // Non-null values are fine. Only accept nulls if T is a class or Nullable.
+ // Note that default(T) is not equal to null for value types except when T is Nullable.
+ return ((value is T) || (value == null && default(T) == null));
+ }
+
+ object IList.this[int index]
+ {
+ get
+ {
+ return this[index];
+ }
+ set
+ {
+ ThrowHelper.IfNullAndNullsAreIllegalThenThrow(value, ExceptionArgument.value);
+
+ try
+ {
+ this[index] = (T)value;
+ }
+ catch (InvalidCastException)
+ {
+ ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(T));
+ }
+ }
+ }
+
+ ///
+ /// Adds the given object to the end of this list. The size of the list is
+ /// increased by one. If required, the capacity of the list is doubled
+ /// before adding the new element.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public void Add(T item)
+ {
+ _version++;
+ int size = _size;
+ if ((uint)size < (uint)_items.Length)
+ {
+ _size = size + 1;
+ _items[size] = item;
+ }
+ else
+ {
+ AddWithResize(item);
+ }
+ }
+
+ // Non-inline from List.Add to improve its code quality as uncommon path
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private void AddWithResize(T item)
+ {
+ int size = _size;
+ EnsureCapacity(size + 1);
+ _size = size + 1;
+ _items[size] = item;
+ }
+
+ int IList.Add(object item)
+ {
+ ThrowHelper.IfNullAndNullsAreIllegalThenThrow(item, ExceptionArgument.item);
+
+ try
+ {
+ Add((T)item);
+ }
+ catch (InvalidCastException)
+ {
+ ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
+ }
+
+ return Count - 1;
+ }
+
+ ///
+ /// Adds the elements of the given collection to the end of this list. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger.
+ ///
+ public void AddRange(IEnumerable collection)
+ => InsertRange(_size, collection);
+
+ ///
+ /// Adds the elements of the given array to the end of this list. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger.
+ ///
+ public void AddRange(T[] array)
+ => AddRange(array.AsSpan());
+
+ ///
+ /// Adds the elements of the given to the end of this list. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger.
+ ///
+ public void AddRange(ReadOnlySpan span)
+ {
+ var newSpan = InsertSpan(_size, span.Length, false);
+ span.CopyTo(newSpan);
+ }
+
+ ///
+ /// Advances the by the number of items specified,
+ /// increasing the capacity if required, then returns a Span representing
+ /// the set of items to be added, allowing direct writes to that section
+ /// of the collection.
+ ///
+ /// The number of items to add.
+ public Span AddSpan(int count)
+ => InsertSpan(_size, count);
+
+ public ReadOnlyCollection AsReadOnly()
+ => new ReadOnlyCollection(this);
+
+ ///
+ /// Searches a section of the list for a given element using a binary search
+ /// algorithm.
+ ///
+ ///
+ /// Elements of the list are compared to the search value using
+ /// the given IComparer interface. If comparer is null, elements of
+ /// the list are compared to the search value using the IComparable
+ /// interface, which in that case must be implemented by all elements of the
+ /// list and the given search value. This method assumes that the given
+ /// section of the list is already sorted; if this is not the case, the
+ /// result will be incorrect.
+ ///
+ /// The method returns the index of the given value in the list. If the
+ /// list does not contain the given value, the method returns a negative
+ /// integer. The bitwise complement operator (~) can be applied to a
+ /// negative result to produce the index of the first element (if any) that
+ /// is larger than the given search value. This is also the index at which
+ /// the search value should be inserted into the list in order for the list
+ /// to remain sorted.
+ ///
+ public int BinarySearch(int index, int count, T item, IComparer comparer)
+ {
+ if (index < 0)
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+ if (count < 0)
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ if (_size - index < count)
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+
+ return Array.BinarySearch(_items, index, count, item, comparer);
+ }
+
+ ///
+ /// Searches the list for a given element using a binary search
+ /// algorithm. If the item implements
+ /// then that is used for comparison, otherwise is used.
+ ///
+ public int BinarySearch(T item)
+ => BinarySearch(0, Count, item, null);
+
+ ///
+ /// Searches the list for a given element using a binary search
+ /// algorithm. If the item implements
+ /// then that is used for comparison, otherwise is used.
+ ///
+ public int BinarySearch(T item, IComparer comparer)
+ => BinarySearch(0, Count, item, comparer);
+
+ ///
+ /// Clears the contents of the PooledList.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public void Clear()
+ {
+ _version++;
+ int size = _size;
+ _size = 0;
+
+ if (size > 0 && _clearOnFree)
+ {
+ // Clear the elements so that the gc can reclaim the references.
+ Array.Clear(_items, 0, _size);
+ }
+ }
+
+ ///
+ /// Contains returns true if the specified element is in the List.
+ /// It does a linear, O(n) search. Equality is determined by calling
+ /// EqualityComparer{T}.Default.Equals.
+ ///
+ public bool Contains(T item)
+ {
+ // PERF: IndexOf calls Array.IndexOf, which internally
+ // calls EqualityComparer.Default.IndexOf, which
+ // is specialized for different types. This
+ // boosts performance since instead of making a
+ // virtual method call each iteration of the loop,
+ // via EqualityComparer.Default.Equals, we
+ // only make one virtual call to EqualityComparer.IndexOf.
+
+ return _size != 0 && IndexOf(item) != -1;
+ }
+
+ bool IList.Contains(object item)
+ {
+ if (IsCompatibleObject(item))
+ {
+ return Contains((T)item);
+ }
+ return false;
+ }
+
+ public PooledList ConvertAll(Func converter)
+ {
+ if (converter == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.converter);
+ }
+
+ var list = new PooledList(_size);
+ for (int i = 0; i < _size; i++)
+ {
+ list._items[i] = converter(_items[i]);
+ }
+ list._size = _size;
+ return list;
+ }
+
+ ///
+ /// Copies this list to the given span.
+ ///
+ public void CopyTo(Span span)
+ {
+ if (span.Length < Count)
+ throw new ArgumentException("Destination span is shorter than the list to be copied.");
+
+ Span.CopyTo(span);
+ }
+
+ void ICollection.CopyTo(T[] array, int arrayIndex)
+ {
+ Array.Copy(_items, 0, array, arrayIndex, _size);
+ }
+
+ // Copies this List into array, which must be of a
+ // compatible array type.
+ void ICollection.CopyTo(Array array, int arrayIndex)
+ {
+ if ((array != null) && (array.Rank != 1))
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
+ }
+
+ try
+ {
+ // Array.Copy will check for NULL.
+ Array.Copy(_items, 0, array, arrayIndex, _size);
+ }
+ catch (ArrayTypeMismatchException)
+ {
+ ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType();
+ }
+ }
+
+ ///
+ /// Ensures that the capacity of this list is at least the given minimum
+ /// value. If the current capacity of the list is less than min, the
+ /// capacity is increased to twice the current capacity or to min,
+ /// whichever is larger.
+ ///
+ private void EnsureCapacity(int min)
+ {
+ if (_items.Length < min)
+ {
+ int newCapacity = _items.Length == 0 ? DefaultCapacity : _items.Length * 2;
+ // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
+ // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
+ if ((uint)newCapacity > MaxArrayLength)
+ newCapacity = MaxArrayLength;
+ if (newCapacity < min)
+ newCapacity = min;
+ Capacity = newCapacity;
+ }
+ }
+
+ public bool Exists(Func match)
+ => FindIndex(match) != -1;
+
+ public bool TryFind(Func match, out T result)
+ {
+ if (match == null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+
+ for (int i = 0; i < _size; i++)
+ {
+ if (match(_items[i]))
+ {
+ result = _items[i];
+ return true;
+ }
+ }
+
+ result = default;
+ return false;
+ }
+
+ public PooledList FindAll(Func match)
+ {
+ if (match == null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+
+ var list = new PooledList();
+ for (int i = 0; i < _size; i++)
+ {
+ if (match(_items[i]))
+ {
+ list.Add(_items[i]);
+ }
+ }
+ return list;
+ }
+
+ public int FindIndex(Func match)
+ => FindIndex(0, _size, match);
+
+ public int FindIndex(int startIndex, Func match)
+ => FindIndex(startIndex, _size - startIndex, match);
+
+ public int FindIndex(int startIndex, int count, Func match)
+ {
+ if ((uint)startIndex > (uint)_size)
+ ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
+
+ if (count < 0 || startIndex > _size - count)
+ ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
+
+ if (match is null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+
+ int endIndex = startIndex + count;
+ for (int i = startIndex; i < endIndex; i++)
+ {
+ if (match(_items[i]))
+ return i;
+ }
+ return -1;
+ }
+
+ public bool TryFindLast(Func match, out T result)
+ {
+ if (match is null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+ }
+
+ for (int i = _size - 1; i >= 0; i--)
+ {
+ if (match(_items[i]))
+ {
+ result = _items[i];
+ return true;
+ }
+ }
+
+ result = default;
+ return false;
+ }
+
+ public int FindLastIndex(Func match)
+ => FindLastIndex(_size - 1, _size, match);
+
+ public int FindLastIndex(int startIndex, Func match)
+ => FindLastIndex(startIndex, startIndex + 1, match);
+
+ public int FindLastIndex(int startIndex, int count, Func match)
+ {
+ if (match == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+ }
+
+ if (_size == 0)
+ {
+ // Special case for 0 length List
+ if (startIndex != -1)
+ {
+ ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
+ }
+ }
+ else
+ {
+ // Make sure we're not out of range
+ if ((uint)startIndex >= (uint)_size)
+ {
+ ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
+ }
+ }
+
+ // 2nd half of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0.
+ if (count < 0 || startIndex - count + 1 < 0)
+ {
+ ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
+ }
+
+ int endIndex = startIndex - count;
+ for (int i = startIndex; i > endIndex; i--)
+ {
+ if (match(_items[i]))
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public void ForEach(Action action)
+ {
+ if (action == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.action);
+ }
+
+ int version = _version;
+ for (int i = 0; i < _size; i++)
+ {
+ if (version != _version)
+ {
+ break;
+ }
+ action(_items[i]);
+ }
+
+ if (version != _version)
+ ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
+ }
+
+ ///
+ /// Returns an enumerator for this list with the given
+ /// permission for removal of elements. If modifications made to the list
+ /// while an enumeration is in progress, the MoveNext and
+ /// GetObject methods of the enumerator will throw an exception.
+ ///
+ public Enumerator GetEnumerator()
+ => new Enumerator(this);
+
+ IEnumerator IEnumerable.GetEnumerator()
+ => new Enumerator(this);
+
+ IEnumerator IEnumerable.GetEnumerator()
+ => new Enumerator(this);
+
+ ///
+ /// Equivalent to PooledList.Span.Slice(index, count).
+ ///
+ public Span GetRange(int index, int count)
+ {
+ if (index < 0)
+ {
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+ }
+
+ if (count < 0)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+
+ if (_size - index < count)
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+ }
+
+ return Span.Slice(index, count);
+ }
+
+ ///
+ /// Returns the index of the first occurrence of a given value in
+ /// this list. The list is searched forwards from beginning to end.
+ ///
+ public int IndexOf(T item)
+ => Array.IndexOf(_items, item, 0, _size);
+
+ int IList.IndexOf(object item)
+ {
+ if (IsCompatibleObject(item))
+ {
+ return IndexOf((T)item);
+ }
+ return -1;
+ }
+
+ ///
+ /// Returns the index of the first occurrence of a given value in a range of
+ /// this list. The list is searched forwards, starting at index
+ /// index and ending at count number of elements.
+ ///
+ public int IndexOf(T item, int index)
+ {
+ if (index > _size)
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+ return Array.IndexOf(_items, item, index, _size - index);
+ }
+
+ ///
+ /// Returns the index of the first occurrence of a given value in a range of
+ /// this list. The list is searched forwards, starting at index
+ /// index and upto count number of elements.
+ ///
+ public int IndexOf(T item, int index, int count)
+ {
+ if (index > _size)
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+
+ if (count < 0 || index > _size - count)
+ ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
+
+ return Array.IndexOf(_items, item, index, count);
+ }
+
+ ///
+ /// Inserts an element into this list at a given index. The size of the list
+ /// is increased by one. If required, the capacity of the list is doubled
+ /// before inserting the new element.
+ ///
+ public void Insert(int index, T item)
+ {
+ // Note that insertions at the end are legal.
+ if ((uint)index > (uint)_size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
+ }
+
+ if (_size == _items.Length)
+ EnsureCapacity(_size + 1);
+ if (index < _size)
+ {
+ Array.Copy(_items, index, _items, index + 1, _size - index);
+ }
+ _items[index] = item;
+ _size++;
+ _version++;
+ }
+
+ void IList.Insert(int index, object item)
+ {
+ ThrowHelper.IfNullAndNullsAreIllegalThenThrow(item, ExceptionArgument.item);
+
+ try
+ {
+ Insert(index, (T)item);
+ }
+ catch (InvalidCastException)
+ {
+ ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
+ }
+ }
+
+ ///
+ /// Inserts the elements of the given collection at a given index. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger. Ranges may be added
+ /// to the end of the list by setting index to the List's size.
+ ///
+ public void InsertRange(int index, IEnumerable collection)
+ {
+ if ((uint)index > (uint)_size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+ }
+
+ switch (collection)
+ {
+ case null:
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
+ break;
+
+ case ICollection c:
+ int count = c.Count;
+ if (count > 0)
+ {
+ EnsureCapacity(_size + count);
+ if (index < _size)
+ {
+ Array.Copy(_items, index, _items, index + count, _size - index);
+ }
+
+ // If we're inserting a List into itself, we want to be able to deal with that.
+ if (this == c)
+ {
+ // Copy first part of _items to insert location
+ Array.Copy(_items, 0, _items, index, index);
+ // Copy last part of _items back to inserted location
+ Array.Copy(_items, index + count, _items, index * 2, _size - index);
+ }
+ else
+ {
+ c.CopyTo(_items, index);
+ }
+ _size += count;
+ }
+ break;
+
+ default:
+ using (var en = collection.GetEnumerator())
+ {
+ while (en.MoveNext())
+ {
+ Insert(index++, en.Current);
+ }
+ }
+ break;
+ }
+
+ _version++;
+ }
+
+ ///
+ /// Inserts the elements of the given collection at a given index. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger. Ranges may be added
+ /// to the end of the list by setting index to the List's size.
+ ///
+ public void InsertRange(int index, ReadOnlySpan span)
+ {
+ var newSpan = InsertSpan(index, span.Length, false);
+ span.CopyTo(newSpan);
+ }
+
+ ///
+ /// Inserts the elements of the given collection at a given index. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger. Ranges may be added
+ /// to the end of the list by setting index to the List's size.
+ ///
+ public void InsertRange(int index, T[] array)
+ {
+ if (array is null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
+ InsertRange(index, array.AsSpan());
+ }
+
+ ///
+ /// Advances the by the number of items specified,
+ /// increasing the capacity if required, then returns a Span representing
+ /// the set of items to be added, allowing direct writes to that section
+ /// of the collection.
+ ///
+ public Span InsertSpan(int index, int count)
+ => InsertSpan(index, count, true);
+
+ private Span InsertSpan(int index, int count, bool clearOutput)
+ {
+ EnsureCapacity(_size + count);
+
+ if (index < _size)
+ {
+ Array.Copy(_items, index, _items, index + count, _size - index);
+ }
+
+ _size += count;
+ _version++;
+
+ var output = _items.AsSpan(index, count);
+
+ if (clearOutput && _clearOnFree)
+ {
+ output.Clear();
+ }
+
+ return output;
+ }
+
+ ///
+ /// Returns the index of the last occurrence of a given value in a range of
+ /// this list. The list is searched backwards, starting at the end
+ /// and ending at the first element in the list.
+ ///
+ public int LastIndexOf(T item)
+ {
+ if (_size == 0)
+ { // Special case for empty list
+ return -1;
+ }
+ else
+ {
+ return LastIndexOf(item, _size - 1, _size);
+ }
+ }
+
+ ///
+ /// Returns the index of the last occurrence of a given value in a range of
+ /// this list. The list is searched backwards, starting at index
+ /// index and ending at the first element in the list.
+ ///
+ public int LastIndexOf(T item, int index)
+ {
+ if (index >= _size)
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+ return LastIndexOf(item, index, index + 1);
+ }
+
+ ///
+ /// Returns the index of the last occurrence of a given value in a range of
+ /// this list. The list is searched backwards, starting at index
+ /// index and upto count elements
+ ///
+ public int LastIndexOf(T item, int index, int count)
+ {
+ if (Count != 0 && index < 0)
+ {
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+ }
+
+ if (Count != 0 && count < 0)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+
+ if (_size == 0)
+ {
+ // Special case for empty list
+ return -1;
+ }
+
+ if (index >= _size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
+ }
+
+ if (count > index + 1)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
+ }
+
+ return Array.LastIndexOf(_items, item, index, count);
+ }
+
+ // Removes the element at the given index. The size of the list is
+ // decreased by one.
+ public bool Remove(T item)
+ {
+ int index = IndexOf(item);
+ if (index >= 0)
+ {
+ RemoveAt(index);
+ return true;
+ }
+
+ return false;
+ }
+
+ void IList.Remove(object item)
+ {
+ if (IsCompatibleObject(item))
+ {
+ Remove((T)item);
+ }
+ }
+
+ ///
+ /// This method removes all items which match the predicate.
+ /// The complexity is O(n).
+ ///
+ public int RemoveAll(Func match)
+ {
+ if (match == null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+
+ int freeIndex = 0; // the first free slot in items array
+
+ // Find the first item which needs to be removed.
+ while (freeIndex < _size && !match(_items[freeIndex]))
+ freeIndex++;
+ if (freeIndex >= _size)
+ return 0;
+
+ int current = freeIndex + 1;
+ while (current < _size)
+ {
+ // Find the first item which needs to be kept.
+ while (current < _size && match(_items[current]))
+ current++;
+
+ if (current < _size)
+ {
+ // copy item to the free slot.
+ _items[freeIndex++] = _items[current++];
+ }
+ }
+
+ if (_clearOnFree)
+ {
+ // Clear the removed elements so that the gc can reclaim the references.
+ Array.Clear(_items, freeIndex, _size - freeIndex);
+ }
+
+ int result = _size - freeIndex;
+ _size = freeIndex;
+ _version++;
+ return result;
+ }
+
+ ///
+ /// Removes the element at the given index. The size of the list is
+ /// decreased by one.
+ ///
+ public void RemoveAt(int index)
+ {
+ if ((uint)index >= (uint)_size)
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+
+ _size--;
+ if (index < _size)
+ {
+ Array.Copy(_items, index + 1, _items, index, _size - index);
+ }
+ _version++;
+
+ if (_clearOnFree)
+ {
+ // Clear the removed element so that the gc can reclaim the reference.
+ _items[_size] = default;
+ }
+ }
+
+ ///
+ /// Removes a range of elements from this list.
+ ///
+ public void RemoveRange(int index, int count)
+ {
+ if (index < 0)
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+
+ if (count < 0)
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+
+ if (_size - index < count)
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+
+ if (count > 0)
+ {
+ _size -= count;
+ if (index < _size)
+ {
+ Array.Copy(_items, index + count, _items, index, _size - index);
+ }
+
+ _version++;
+
+ if (_clearOnFree)
+ {
+ // Clear the removed elements so that the gc can reclaim the references.
+ Array.Clear(_items, _size, count);
+ }
+ }
+ }
+
+ ///
+ /// Reverses the elements in this list.
+ ///
+ public void Reverse()
+ => Reverse(0, _size);
+
+ ///
+ /// Reverses the elements in a range of this list. Following a call to this
+ /// method, an element in the range given by index and count
+ /// which was previously located at index i will now be located at
+ /// index index + (index + count - i - 1).
+ ///
+ public void Reverse(int index, int count)
+ {
+ if (index < 0)
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+
+ if (count < 0)
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+
+ if (_size - index < count)
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+
+ if (count > 1)
+ {
+ Array.Reverse(_items, index, count);
+ }
+ _version++;
+ }
+
+ ///
+ /// Sorts the elements in this list. Uses the default comparer and
+ /// Array.Sort.
+ ///
+ public void Sort()
+ => Sort(0, Count, null);
+
+ ///
+ /// Sorts the elements in this list. Uses Array.Sort with the
+ /// provided comparer.
+ ///
+ ///
+ public void Sort(IComparer comparer)
+ => Sort(0, Count, comparer);
+
+ ///
+ /// Sorts the elements in a section of this list. The sort compares the
+ /// elements to each other using the given IComparer interface. If
+ /// comparer is null, the elements are compared to each other using
+ /// the IComparable interface, which in that case must be implemented by all
+ /// elements of the list.
+ ///
+ /// This method uses the Array.Sort method to sort the elements.
+ ///
+ public void Sort(int index, int count, IComparer comparer)
+ {
+ if (index < 0)
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+
+ if (count < 0)
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+
+ if (_size - index < count)
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+
+ if (count > 1)
+ {
+ Array.Sort(_items, index, count, comparer);
+ }
+ _version++;
+ }
+
+ public void Sort(Func comparison)
+ {
+ if (comparison == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparison);
+ }
+
+ if (_size > 1)
+ {
+ // List uses ArraySortHelper here but since it's an internal class,
+ // we're creating an IComparer using the comparison function to avoid
+ // duplicating all that code.
+ Array.Sort(_items, 0, _size, new Comparer(comparison));
+ }
+ _version++;
+ }
+
+ ///
+ /// ToArray returns an array containing the contents of the List.
+ /// This requires copying the List, which is an O(n) operation.
+ ///
+ public T[] ToArray()
+ {
+ if (_size == 0)
+ {
+ return s_emptyArray;
+ }
+
+ return Span.ToArray();
+ }
+
+ ///
+ /// Sets the capacity of this list to the size of the list. This method can
+ /// be used to minimize a list's memory overhead once it is known that no
+ /// new elements will be added to the list. To completely clear a list and
+ /// release all memory referenced by the list, execute the following
+ /// statements:
+ ///
+ /// list.Clear();
+ /// list.TrimExcess();
+ ///
+ ///
+ public void TrimExcess()
+ {
+ int threshold = (int)(_items.Length * 0.9);
+ if (_size < threshold)
+ {
+ Capacity = _size;
+ }
+ }
+
+ public bool TrueForAll(Func match)
+ {
+ if (match == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+ }
+
+ for (int i = 0; i < _size; i++)
+ {
+ if (!match(_items[i]))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private void ReturnArray()
+ {
+ if (_items.Length == 0)
+ return;
+
+ try
+ {
+ // Clear the elements so that the gc can reclaim the references.
+ _pool.Return(_items, clearArray: _clearOnFree);
+ }
+ catch (ArgumentException)
+ {
+ // oh well, the array pool didn't like our array
+ }
+
+ _items = s_emptyArray;
+ }
+
+ private static bool ShouldClear(ClearMode mode)
+ {
+#if NETCOREAPP2_1
+ return mode == ClearMode.Always
+ || (mode == ClearMode.Auto && RuntimeHelpers.IsReferenceOrContainsReferences());
+#else
+ return mode != ClearMode.Never;
+#endif
+ }
+
+ ///
+ /// Returns the internal buffers to the ArrayPool.
+ ///
+ public void Dispose()
+ {
+ ReturnArray();
+ _size = 0;
+ _version++;
+ }
+
+ void IDeserializationCallback.OnDeserialization(object sender)
+ {
+ // We can't serialize array pools, so deserialized PooledLists will
+ // have to use the shared pool, even if they were using a custom pool
+ // before serialization.
+ _pool = ArrayPool.Shared;
+ }
+
+ public struct Enumerator : IEnumerator, IEnumerator
+ {
+ private readonly PooledList _list;
+ private int _index;
+ private readonly int _version;
+ private T _current;
+
+ internal Enumerator(PooledList list)
+ {
+ _list = list;
+ _index = 0;
+ _version = list._version;
+ _current = default;
+ }
+
+ public void Dispose()
+ {
+ }
+
+ public bool MoveNext()
+ {
+ var localList = _list;
+
+ if (_version == localList._version && ((uint)_index < (uint)localList._size))
+ {
+ _current = localList._items[_index];
+ _index++;
+ return true;
+ }
+ return MoveNextRare();
+ }
+
+ private bool MoveNextRare()
+ {
+ if (_version != _list._version)
+ {
+ ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
+ }
+
+ _index = _list._size + 1;
+ _current = default;
+ return false;
+ }
+
+ public T Current => _current;
+
+ object IEnumerator.Current
+ {
+ get
+ {
+ if (_index == 0 || _index == _list._size + 1)
+ {
+ ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen();
+ }
+ return Current;
+ }
+ }
+
+ void IEnumerator.Reset()
+ {
+ if (_version != _list._version)
+ {
+ ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
+ }
+
+ _index = 0;
+ _current = default;
+ }
+ }
+
+ private readonly struct Comparer : IComparer
+ {
+ private readonly Func _comparison;
+
+ public Comparer(Func comparison)
+ {
+ _comparison = comparison;
+ }
+
+ public int Compare(T x, T y) => _comparison(x, y);
+ }
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/PooledStack.cs b/src/Avalonia.Base/Collections/Pooled/PooledStack.cs
new file mode 100644
index 0000000000..104a7f97e9
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/PooledStack.cs
@@ -0,0 +1,699 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/*=============================================================================
+**
+**
+** Purpose: An array implementation of a generic stack.
+**
+**
+=============================================================================*/
+
+using System;
+using System.Buffers;
+using System.Collections;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
+using System.Runtime.CompilerServices;
+using System.Runtime.Serialization;
+using System.Threading;
+
+namespace Avalonia.Collections.Pooled
+{
+ ///
+ /// A simple stack of objects. Internally it is implemented as an array,
+ /// so Push can be O(n). Pop is O(1).
+ ///
+ [DebuggerTypeProxy(typeof(StackDebugView<>))]
+ [DebuggerDisplay("Count = {Count}")]
+ [Serializable]
+ public class PooledStack : IEnumerable, ICollection, IReadOnlyCollection, IDisposable, IDeserializationCallback
+ {
+ [NonSerialized]
+ private ArrayPool _pool;
+ [NonSerialized]
+ private object _syncRoot;
+
+ private T[] _array; // Storage for stack elements. Do not rename (binary serialization)
+ private int _size; // Number of items in the stack. Do not rename (binary serialization)
+ private int _version; // Used to keep enumerator in sync w/ collection. Do not rename (binary serialization)
+ private readonly bool _clearOnFree;
+
+ private const int DefaultCapacity = 4;
+
+ #region Constructors
+
+ ///
+ /// Create a stack with the default initial capacity.
+ ///
+ public PooledStack() : this(ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Create a stack with the default initial capacity.
+ ///
+ public PooledStack(ClearMode clearMode) : this(clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Create a stack with the default initial capacity.
+ ///
+ public PooledStack(ArrayPool customPool) : this(ClearMode.Auto, customPool) { }
+
+ ///
+ /// Create a stack with the default initial capacity and a custom ArrayPool.
+ ///
+ public PooledStack(ClearMode clearMode, ArrayPool customPool)
+ {
+ _pool = customPool ?? ArrayPool.Shared;
+ _array = Array.Empty();
+ _clearOnFree = ShouldClear(clearMode);
+ }
+
+ ///
+ /// Create a stack with a specific initial capacity. The initial capacity
+ /// must be a non-negative number.
+ ///
+ public PooledStack(int capacity) : this(capacity, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Create a stack with a specific initial capacity. The initial capacity
+ /// must be a non-negative number.
+ ///
+ public PooledStack(int capacity, ClearMode clearMode) : this(capacity, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Create a stack with a specific initial capacity. The initial capacity
+ /// must be a non-negative number.
+ ///
+ public PooledStack(int capacity, ArrayPool customPool) : this(capacity, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Create a stack with a specific initial capacity. The initial capacity
+ /// must be a non-negative number.
+ ///
+ public PooledStack(int capacity, ClearMode clearMode, ArrayPool customPool)
+ {
+ if (capacity < 0)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity,
+ ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+ _pool = customPool ?? ArrayPool.Shared;
+ _array = _pool.Rent(capacity);
+ _clearOnFree = ShouldClear(clearMode);
+ }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(IEnumerable enumerable) : this(enumerable, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(IEnumerable enumerable, ClearMode clearMode) : this(enumerable, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(IEnumerable enumerable, ArrayPool customPool) : this(enumerable, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(IEnumerable enumerable, ClearMode clearMode, ArrayPool customPool)
+ {
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+
+ switch (enumerable)
+ {
+ case null:
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.enumerable);
+ break;
+
+ case ICollection collection:
+ if (collection.Count == 0)
+ {
+ _array = Array.Empty();
+ }
+ else
+ {
+ _array = _pool.Rent(collection.Count);
+ collection.CopyTo(_array, 0);
+ _size = collection.Count;
+ }
+ break;
+
+ default:
+ using (var list = new PooledList(enumerable))
+ {
+ _array = _pool.Rent(list.Count);
+ list.Span.CopyTo(_array);
+ _size = list.Count;
+ }
+ break;
+ }
+ }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(T[] array) : this(array.AsSpan(), ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(T[] array, ClearMode clearMode) : this(array.AsSpan(), clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(T[] array, ArrayPool customPool) : this(array.AsSpan(), ClearMode.Auto, customPool) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(T[] array, ClearMode clearMode, ArrayPool customPool) : this(array.AsSpan(), clearMode, customPool) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(ReadOnlySpan span) : this(span, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(ReadOnlySpan span, ClearMode clearMode) : this(span, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(ReadOnlySpan span, ArrayPool customPool) : this(span, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(ReadOnlySpan span, ClearMode clearMode, ArrayPool customPool)
+ {
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+ _array = _pool.Rent(span.Length);
+ span.CopyTo(_array);
+ _size = span.Length;
+ }
+
+ #endregion
+
+ ///
+ /// The number of items in the stack.
+ ///
+ public int Count => _size;
+
+ ///
+ /// Returns the ClearMode behavior for the collection, denoting whether values are
+ /// cleared from internal arrays before returning them to the pool.
+ ///
+ public ClearMode ClearMode => _clearOnFree ? ClearMode.Always : ClearMode.Never;
+
+ bool ICollection.IsSynchronized => false;
+
+ object ICollection.SyncRoot
+ {
+ get
+ {
+ if (_syncRoot == null)
+ {
+ Interlocked.CompareExchange(ref _syncRoot, new object(), null);
+ }
+ return _syncRoot;
+ }
+ }
+
+ ///
+ /// Removes all Objects from the Stack.
+ ///
+ public void Clear()
+ {
+ if (_clearOnFree)
+ {
+ Array.Clear(_array, 0, _size); // clear the elements so that the gc can reclaim the references.
+ }
+ _size = 0;
+ _version++;
+ }
+
+ ///
+ /// Compares items using the default equality comparer
+ ///
+ public bool Contains(T item)
+ {
+ // PERF: Internally Array.LastIndexOf calls
+ // EqualityComparer.Default.LastIndexOf, which
+ // is specialized for different types. This
+ // boosts performance since instead of making a
+ // virtual method call each iteration of the loop,
+ // via EqualityComparer.Default.Equals, we
+ // only make one virtual call to EqualityComparer.LastIndexOf.
+
+ return _size != 0 && Array.LastIndexOf(_array, item, _size - 1) != -1;
+ }
+
+ ///
+ /// This method removes all items which match the predicate.
+ /// The complexity is O(n).
+ ///
+ public int RemoveWhere(Func match)
+ {
+ if (match == null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+
+ int freeIndex = 0; // the first free slot in items array
+
+ // Find the first item which needs to be removed.
+ while (freeIndex < _size && !match(_array[freeIndex]))
+ freeIndex++;
+ if (freeIndex >= _size)
+ return 0;
+
+ int current = freeIndex + 1;
+ while (current < _size)
+ {
+ // Find the first item which needs to be kept.
+ while (current < _size && match(_array[current]))
+ current++;
+
+ if (current < _size)
+ {
+ // copy item to the free slot.
+ _array[freeIndex++] = _array[current++];
+ }
+ }
+
+ if (_clearOnFree)
+ {
+ // Clear the removed elements so that the gc can reclaim the references.
+ Array.Clear(_array, freeIndex, _size - freeIndex);
+ }
+
+ int result = _size - freeIndex;
+ _size = freeIndex;
+ _version++;
+ return result;
+ }
+
+ // Copies the stack into an array.
+ public void CopyTo(T[] array, int arrayIndex)
+ {
+ if (array == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
+ }
+
+ if (arrayIndex < 0 || arrayIndex > array.Length)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex);
+ }
+
+ if (array.Length - arrayIndex < _size)
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
+ }
+
+ Debug.Assert(array != _array);
+ int srcIndex = 0;
+ int dstIndex = arrayIndex + _size;
+ while (srcIndex < _size)
+ {
+ array[--dstIndex] = _array[srcIndex++];
+ }
+ }
+
+ public void CopyTo(Span span)
+ {
+ if (span.Length < _size)
+ {
+ ThrowHelper.ThrowArgumentException_DestinationTooShort();
+ }
+
+ int srcIndex = 0;
+ int dstIndex = _size;
+ while (srcIndex < _size)
+ {
+ span[--dstIndex] = _array[srcIndex++];
+ }
+ }
+
+ void ICollection.CopyTo(Array array, int arrayIndex)
+ {
+ if (array == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
+ }
+
+ if (array.Rank != 1)
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
+ }
+
+ if (array.GetLowerBound(0) != 0)
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound, ExceptionArgument.array);
+ }
+
+ if (arrayIndex < 0 || arrayIndex > array.Length)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex);
+ }
+
+ if (array.Length - arrayIndex < _size)
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+ }
+
+ try
+ {
+ Array.Copy(_array, 0, array, arrayIndex, _size);
+ Array.Reverse(array, arrayIndex, _size);
+ }
+ catch (ArrayTypeMismatchException)
+ {
+ ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType();
+ }
+ }
+
+ ///
+ /// Returns an IEnumerator for this PooledStack.
+ ///
+ ///
+ public Enumerator GetEnumerator()
+ => new Enumerator(this);
+
+ ///
+ IEnumerator IEnumerable.GetEnumerator()
+ => new Enumerator(this);
+
+ IEnumerator IEnumerable.GetEnumerator()
+ => new Enumerator(this);
+
+ public void TrimExcess()
+ {
+ if (_size == 0)
+ {
+ ReturnArray(replaceWith: Array.Empty());
+ _version++;
+ return;
+ }
+
+ int threshold = (int)(_array.Length * 0.9);
+ if (_size < threshold)
+ {
+ var newArray = _pool.Rent(_size);
+ if (newArray.Length < _array.Length)
+ {
+ Array.Copy(_array, newArray, _size);
+ ReturnArray(replaceWith: newArray);
+ _version++;
+ }
+ else
+ {
+ // The array from the pool wasn't any smaller than the one we already had,
+ // (we can only control minimum size) so return it and do nothing.
+ // If we create an exact-sized array not from the pool, we'll
+ // get an exception when returning it to the pool.
+ _pool.Return(newArray);
+ }
+ }
+ }
+
+ ///
+ /// Returns the top object on the stack without removing it. If the stack
+ /// is empty, Peek throws an InvalidOperationException.
+ ///
+ public T Peek()
+ {
+ int size = _size - 1;
+ T[] array = _array;
+
+ if ((uint)size >= (uint)array.Length)
+ {
+ ThrowForEmptyStack();
+ }
+
+ return array[size];
+ }
+
+ public bool TryPeek(out T result)
+ {
+ int size = _size - 1;
+ T[] array = _array;
+
+ if ((uint)size >= (uint)array.Length)
+ {
+ result = default;
+ return false;
+ }
+ result = array[size];
+ return true;
+ }
+
+ ///
+ /// Pops an item from the top of the stack. If the stack is empty, Pop
+ /// throws an InvalidOperationException.
+ ///
+ public T Pop()
+ {
+ int size = _size - 1;
+ T[] array = _array;
+
+ // if (_size == 0) is equivalent to if (size == -1), and this case
+ // is covered with (uint)size, thus allowing bounds check elimination
+ // https://github.com/dotnet/coreclr/pull/9773
+ if ((uint)size >= (uint)array.Length)
+ {
+ ThrowForEmptyStack();
+ }
+
+ _version++;
+ _size = size;
+ T item = array[size];
+ if (_clearOnFree)
+ {
+ array[size] = default; // Free memory quicker.
+ }
+ return item;
+ }
+
+ public bool TryPop(out T result)
+ {
+ int size = _size - 1;
+ T[] array = _array;
+
+ if ((uint)size >= (uint)array.Length)
+ {
+ result = default;
+ return false;
+ }
+
+ _version++;
+ _size = size;
+ result = array[size];
+ if (_clearOnFree)
+ {
+ array[size] = default; // Free memory quicker.
+ }
+ return true;
+ }
+
+ ///
+ /// Pushes an item to the top of the stack.
+ ///
+ public void Push(T item)
+ {
+ int size = _size;
+ T[] array = _array;
+
+ if ((uint)size < (uint)array.Length)
+ {
+ array[size] = item;
+ _version++;
+ _size = size + 1;
+ }
+ else
+ {
+ PushWithResize(item);
+ }
+ }
+
+ // Non-inline from Stack.Push to improve its code quality as uncommon path
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private void PushWithResize(T item)
+ {
+ var newArray = _pool.Rent((_array.Length == 0) ? DefaultCapacity : 2 * _array.Length);
+ Array.Copy(_array, newArray, _size);
+ ReturnArray(replaceWith: newArray);
+ _array[_size] = item;
+ _version++;
+ _size++;
+ }
+
+ ///
+ /// Copies the Stack to an array, in the same order Pop would return the items.
+ ///
+ public T[] ToArray()
+ {
+ if (_size == 0)
+ return Array.Empty();
+
+ T[] objArray = new T[_size];
+ int i = 0;
+ while (i < _size)
+ {
+ objArray[i] = _array[_size - i - 1];
+ i++;
+ }
+ return objArray;
+ }
+
+ private void ThrowForEmptyStack()
+ {
+ Debug.Assert(_size == 0);
+ throw new InvalidOperationException("Stack was empty.");
+ }
+
+ private void ReturnArray(T[] replaceWith = null)
+ {
+ if (_array?.Length > 0)
+ {
+ try
+ {
+ _pool.Return(_array, clearArray: _clearOnFree);
+ }
+ catch (ArgumentException)
+ {
+ // oh well, the array pool didn't like our array
+ }
+ }
+
+ if (!(replaceWith is null))
+ {
+ _array = replaceWith;
+ }
+ }
+
+ private static bool ShouldClear(ClearMode mode)
+ {
+#if NETCOREAPP2_1
+ return mode == ClearMode.Always
+ || (mode == ClearMode.Auto && RuntimeHelpers.IsReferenceOrContainsReferences());
+#else
+ return mode != ClearMode.Never;
+#endif
+ }
+
+ public void Dispose()
+ {
+ ReturnArray(replaceWith: Array.Empty());
+ _size = 0;
+ _version++;
+ }
+
+ void IDeserializationCallback.OnDeserialization(object sender)
+ {
+ // We can't serialize array pools, so deserialized PooledStacks will
+ // have to use the shared pool, even if they were using a custom pool
+ // before serialization.
+ _pool = ArrayPool.Shared;
+ }
+
+ [SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes", Justification = "not an expected scenario")]
+ public struct Enumerator : IEnumerator, IEnumerator
+ {
+ private readonly PooledStack _stack;
+ private readonly int _version;
+ private int _index;
+ private T _currentElement;
+
+ internal Enumerator(PooledStack stack)
+ {
+ _stack = stack;
+ _version = stack._version;
+ _index = -2;
+ _currentElement = default;
+ }
+
+ public void Dispose()
+ {
+ _index = -1;
+ }
+
+ public bool MoveNext()
+ {
+ bool retval;
+ if (_version != _stack._version)
+ throw new InvalidOperationException("Collection was modified during enumeration.");
+ if (_index == -2)
+ { // First call to enumerator.
+ _index = _stack._size - 1;
+ retval = (_index >= 0);
+ if (retval)
+ _currentElement = _stack._array[_index];
+ return retval;
+ }
+ if (_index == -1)
+ { // End of enumeration.
+ return false;
+ }
+
+ retval = (--_index >= 0);
+ if (retval)
+ _currentElement = _stack._array[_index];
+ else
+ _currentElement = default;
+ return retval;
+ }
+
+ public T Current
+ {
+ get
+ {
+ if (_index < 0)
+ ThrowEnumerationNotStartedOrEnded();
+ return _currentElement;
+ }
+ }
+
+ private void ThrowEnumerationNotStartedOrEnded()
+ {
+ Debug.Assert(_index == -1 || _index == -2);
+ throw new InvalidOperationException(_index == -2 ? "Enumeration was not started." : "Enumeration has ended.");
+ }
+
+ object IEnumerator.Current
+ {
+ get { return Current; }
+ }
+
+ void IEnumerator.Reset()
+ {
+ if (_version != _stack._version)
+ throw new InvalidOperationException("Collection was modified during enumeration.");
+ _index = -2;
+ _currentElement = default;
+ }
+ }
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/StackDebugView.cs b/src/Avalonia.Base/Collections/Pooled/StackDebugView.cs
new file mode 100644
index 0000000000..b042388079
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/StackDebugView.cs
@@ -0,0 +1,28 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Diagnostics;
+
+namespace Avalonia.Collections.Pooled
+{
+ internal sealed class StackDebugView
+ {
+ private readonly PooledStack _stack;
+
+ public StackDebugView(PooledStack stack)
+ {
+ _stack = stack ?? throw new ArgumentNullException(nameof(stack));
+ }
+
+ [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
+ public T[] Items
+ {
+ get
+ {
+ return _stack.ToArray();
+ }
+ }
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/ThrowHelper.cs b/src/Avalonia.Base/Collections/Pooled/ThrowHelper.cs
new file mode 100644
index 0000000000..74558229c3
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/ThrowHelper.cs
@@ -0,0 +1,691 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+
+// This file defines an internal class used to throw exceptions in BCL code.
+// The main purpose is to reduce code size.
+//
+// The old way to throw an exception generates quite a lot IL code and assembly code.
+// Following is an example:
+// C# source
+// throw new ArgumentNullException(nameof(key), SR.ArgumentNull_Key);
+// IL code:
+// IL_0003: ldstr "key"
+// IL_0008: ldstr "ArgumentNull_Key"
+// IL_000d: call string System.Environment::GetResourceString(string)
+// IL_0012: newobj instance void System.ArgumentNullException::.ctor(string,string)
+// IL_0017: throw
+// which is 21bytes in IL.
+//
+// So we want to get rid of the ldstr and call to Environment.GetResource in IL.
+// In order to do that, I created two enums: ExceptionResource, ExceptionArgument to represent the
+// argument name and resource name in a small integer. The source code will be changed to
+// ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key, ExceptionResource.ArgumentNull_Key);
+//
+// The IL code will be 7 bytes.
+// IL_0008: ldc.i4.4
+// IL_0009: ldc.i4.4
+// IL_000a: call void System.ThrowHelper::ThrowArgumentNullException(valuetype System.ExceptionArgument)
+// IL_000f: ldarg.0
+//
+// This will also reduce the Jitted code size a lot.
+//
+// It is very important we do this for generic classes because we can easily generate the same code
+// multiple times for different instantiation.
+//
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
+using System.Runtime.Serialization;
+
+namespace Avalonia.Collections.Pooled
+{
+ internal static class ThrowHelper
+ {
+ internal static void ThrowArrayTypeMismatchException()
+ {
+ throw new ArrayTypeMismatchException();
+ }
+
+ internal static void ThrowIndexOutOfRangeException()
+ {
+ throw new IndexOutOfRangeException();
+ }
+
+ internal static void ThrowArgumentOutOfRangeException()
+ {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ internal static void ThrowArgumentException_DestinationTooShort()
+ {
+ throw new ArgumentException("Destination too short.");
+ }
+
+ internal static void ThrowArgumentException_OverlapAlignmentMismatch()
+ {
+ throw new ArgumentException("Overlap alignment mismatch.");
+ }
+
+ internal static void ThrowArgumentOutOfRange_IndexException()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.index,
+ ExceptionResource.ArgumentOutOfRange_Index);
+ }
+
+ internal static void ThrowIndexArgumentOutOfRange_NeedNonNegNumException()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.index,
+ ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+
+ internal static void ThrowValueArgumentOutOfRange_NeedNonNegNumException()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.value,
+ ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+
+ internal static void ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.length,
+ ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+
+ internal static void ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.startIndex,
+ ExceptionResource.ArgumentOutOfRange_Index);
+ }
+
+ internal static void ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.count,
+ ExceptionResource.ArgumentOutOfRange_Count);
+ }
+
+ internal static void ThrowWrongKeyTypeArgumentException(T key, Type targetType)
+ {
+ // Generic key to move the boxing to the right hand side of throw
+ throw GetWrongKeyTypeArgumentException((object)key, targetType);
+ }
+
+ internal static void ThrowWrongValueTypeArgumentException(T value, Type targetType)
+ {
+ // Generic key to move the boxing to the right hand side of throw
+ throw GetWrongValueTypeArgumentException((object)value, targetType);
+ }
+
+ private static ArgumentException GetAddingDuplicateWithKeyArgumentException(object key)
+ {
+ return new ArgumentException($"Error adding duplicate with key: {key}.");
+ }
+
+ internal static void ThrowAddingDuplicateWithKeyArgumentException(T key)
+ {
+ // Generic key to move the boxing to the right hand side of throw
+ throw GetAddingDuplicateWithKeyArgumentException((object)key);
+ }
+
+ internal static void ThrowKeyNotFoundException(T key)
+ {
+ // Generic key to move the boxing to the right hand side of throw
+ throw GetKeyNotFoundException((object)key);
+ }
+
+ internal static void ThrowArgumentException(ExceptionResource resource)
+ {
+ throw GetArgumentException(resource);
+ }
+
+ internal static void ThrowArgumentException(ExceptionResource resource, ExceptionArgument argument)
+ {
+ throw GetArgumentException(resource, argument);
+ }
+
+ private static ArgumentNullException GetArgumentNullException(ExceptionArgument argument)
+ {
+ return new ArgumentNullException(GetArgumentName(argument));
+ }
+
+ internal static void ThrowArgumentNullException(ExceptionArgument argument)
+ {
+ throw GetArgumentNullException(argument);
+ }
+
+ internal static void ThrowArgumentNullException(ExceptionResource resource)
+ {
+ throw new ArgumentNullException(GetResourceString(resource));
+ }
+
+ internal static void ThrowArgumentNullException(ExceptionArgument argument, ExceptionResource resource)
+ {
+ throw new ArgumentNullException(GetArgumentName(argument), GetResourceString(resource));
+ }
+
+ internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument)
+ {
+ throw new ArgumentOutOfRangeException(GetArgumentName(argument));
+ }
+
+ internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
+ {
+ throw GetArgumentOutOfRangeException(argument, resource);
+ }
+
+ internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, int paramNumber, ExceptionResource resource)
+ {
+ throw GetArgumentOutOfRangeException(argument, paramNumber, resource);
+ }
+
+ internal static void ThrowInvalidOperationException(ExceptionResource resource)
+ {
+ throw GetInvalidOperationException(resource);
+ }
+
+ internal static void ThrowInvalidOperationException(ExceptionResource resource, Exception e)
+ {
+ throw new InvalidOperationException(GetResourceString(resource), e);
+ }
+
+ internal static void ThrowSerializationException(ExceptionResource resource)
+ {
+ throw new SerializationException(GetResourceString(resource));
+ }
+
+ internal static void ThrowSecurityException(ExceptionResource resource)
+ {
+ throw new System.Security.SecurityException(GetResourceString(resource));
+ }
+
+ internal static void ThrowRankException(ExceptionResource resource)
+ {
+ throw new RankException(GetResourceString(resource));
+ }
+
+ internal static void ThrowNotSupportedException(ExceptionResource resource)
+ {
+ throw new NotSupportedException(GetResourceString(resource));
+ }
+
+ internal static void ThrowUnauthorizedAccessException(ExceptionResource resource)
+ {
+ throw new UnauthorizedAccessException(GetResourceString(resource));
+ }
+
+ internal static void ThrowObjectDisposedException(string objectName, ExceptionResource resource)
+ {
+ throw new ObjectDisposedException(objectName, GetResourceString(resource));
+ }
+
+ internal static void ThrowObjectDisposedException(ExceptionResource resource)
+ {
+ throw new ObjectDisposedException(null, GetResourceString(resource));
+ }
+
+ internal static void ThrowNotSupportedException()
+ {
+ throw new NotSupportedException();
+ }
+
+ internal static void ThrowAggregateException(List exceptions)
+ {
+ throw new AggregateException(exceptions);
+ }
+
+ internal static void ThrowOutOfMemoryException()
+ {
+ throw new OutOfMemoryException();
+ }
+
+ internal static void ThrowArgumentException_Argument_InvalidArrayType()
+ {
+ throw new ArgumentException("Invalid array type.");
+ }
+
+ internal static void ThrowInvalidOperationException_InvalidOperation_EnumNotStarted()
+ {
+ throw new InvalidOperationException("Enumeration has not started.");
+ }
+
+ internal static void ThrowInvalidOperationException_InvalidOperation_EnumEnded()
+ {
+ throw new InvalidOperationException("Enumeration has ended.");
+ }
+
+ internal static void ThrowInvalidOperationException_EnumCurrent(int index)
+ {
+ throw GetInvalidOperationException_EnumCurrent(index);
+ }
+
+ internal static void ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()
+ {
+ throw new InvalidOperationException("Collection was modified during enumeration.");
+ }
+
+ internal static void ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen()
+ {
+ throw new InvalidOperationException("Invalid enumerator state: enumeration cannot proceed.");
+ }
+
+ internal static void ThrowInvalidOperationException_InvalidOperation_NoValue()
+ {
+ throw new InvalidOperationException("No value provided.");
+ }
+
+ internal static void ThrowInvalidOperationException_ConcurrentOperationsNotSupported()
+ {
+ throw new InvalidOperationException("Concurrent operations are not supported.");
+ }
+
+ internal static void ThrowInvalidOperationException_HandleIsNotInitialized()
+ {
+ throw new InvalidOperationException("Handle is not initialized.");
+ }
+
+ internal static void ThrowFormatException_BadFormatSpecifier()
+ {
+ throw new FormatException("Bad format specifier.");
+ }
+
+ private static ArgumentException GetArgumentException(ExceptionResource resource)
+ {
+ return new ArgumentException(GetResourceString(resource));
+ }
+
+ private static InvalidOperationException GetInvalidOperationException(ExceptionResource resource)
+ {
+ return new InvalidOperationException(GetResourceString(resource));
+ }
+
+ private static ArgumentException GetWrongKeyTypeArgumentException(object key, Type targetType)
+ {
+ return new ArgumentException($"Wrong key type. Expected {targetType}, got: '{key}'.", nameof(key));
+ }
+
+ private static ArgumentException GetWrongValueTypeArgumentException(object value, Type targetType)
+ {
+ return new ArgumentException($"Wrong value type. Expected {targetType}, got: '{value}'.", nameof(value));
+ }
+
+ private static KeyNotFoundException GetKeyNotFoundException(object key)
+ {
+ return new KeyNotFoundException($"Key not found: {key}");
+ }
+
+ private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
+ {
+ return new ArgumentOutOfRangeException(GetArgumentName(argument), GetResourceString(resource));
+ }
+
+ private static ArgumentException GetArgumentException(ExceptionResource resource, ExceptionArgument argument)
+ {
+ return new ArgumentException(GetResourceString(resource), GetArgumentName(argument));
+ }
+
+ private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument, int paramNumber, ExceptionResource resource)
+ {
+ return new ArgumentOutOfRangeException(GetArgumentName(argument) + "[" + paramNumber.ToString() + "]", GetResourceString(resource));
+ }
+
+ private static InvalidOperationException GetInvalidOperationException_EnumCurrent(int index)
+ {
+ return new InvalidOperationException(
+ index < 0 ?
+ "Enumeration has not started" :
+ "Enumeration has ended");
+ }
+
+ // Allow nulls for reference types and Nullable, but not for value types.
+ // Aggressively inline so the jit evaluates the if in place and either drops the call altogether
+ // Or just leaves null test and call to the Non-returning ThrowHelper.ThrowArgumentNullException
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static void IfNullAndNullsAreIllegalThenThrow(object value, ExceptionArgument argName)
+ {
+ // Note that default(T) is not equal to null for value types except when T is Nullable.
+ if (!(default(T) == null) && value == null)
+ ThrowHelper.ThrowArgumentNullException(argName);
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static void ThrowForUnsupportedVectorBaseType() where T : struct
+ {
+ if (typeof(T) != typeof(byte) && typeof(T) != typeof(sbyte) &&
+ typeof(T) != typeof(short) && typeof(T) != typeof(ushort) &&
+ typeof(T) != typeof(int) && typeof(T) != typeof(uint) &&
+ typeof(T) != typeof(long) && typeof(T) != typeof(ulong) &&
+ typeof(T) != typeof(float) && typeof(T) != typeof(double))
+ {
+ ThrowNotSupportedException(ExceptionResource.Arg_TypeNotSupported);
+ }
+ }
+
+#if false // Reflection-based implementation does not work for CoreRT/ProjectN
+ // This function will convert an ExceptionArgument enum value to the argument name string.
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private static string GetArgumentName(ExceptionArgument argument)
+ {
+ Debug.Assert(Enum.IsDefined(typeof(ExceptionArgument), argument),
+ "The enum value is not defined, please check the ExceptionArgument Enum.");
+
+ return argument.ToString();
+ }
+#endif
+
+ private static string GetArgumentName(ExceptionArgument argument)
+ {
+ switch (argument)
+ {
+ case ExceptionArgument.obj:
+ return "obj";
+ case ExceptionArgument.dictionary:
+ return "dictionary";
+ case ExceptionArgument.array:
+ return "array";
+ case ExceptionArgument.info:
+ return "info";
+ case ExceptionArgument.key:
+ return "key";
+ case ExceptionArgument.text:
+ return "text";
+ case ExceptionArgument.values:
+ return "values";
+ case ExceptionArgument.value:
+ return "value";
+ case ExceptionArgument.startIndex:
+ return "startIndex";
+ case ExceptionArgument.task:
+ return "task";
+ case ExceptionArgument.ch:
+ return "ch";
+ case ExceptionArgument.s:
+ return "s";
+ case ExceptionArgument.input:
+ return "input";
+ case ExceptionArgument.list:
+ return "list";
+ case ExceptionArgument.index:
+ return "index";
+ case ExceptionArgument.capacity:
+ return "capacity";
+ case ExceptionArgument.collection:
+ return "collection";
+ case ExceptionArgument.item:
+ return "item";
+ case ExceptionArgument.converter:
+ return "converter";
+ case ExceptionArgument.match:
+ return "match";
+ case ExceptionArgument.count:
+ return "count";
+ case ExceptionArgument.action:
+ return "action";
+ case ExceptionArgument.comparison:
+ return "comparison";
+ case ExceptionArgument.exceptions:
+ return "exceptions";
+ case ExceptionArgument.exception:
+ return "exception";
+ case ExceptionArgument.enumerable:
+ return "enumerable";
+ case ExceptionArgument.start:
+ return "start";
+ case ExceptionArgument.format:
+ return "format";
+ case ExceptionArgument.culture:
+ return "culture";
+ case ExceptionArgument.comparer:
+ return "comparer";
+ case ExceptionArgument.comparable:
+ return "comparable";
+ case ExceptionArgument.source:
+ return "source";
+ case ExceptionArgument.state:
+ return "state";
+ case ExceptionArgument.length:
+ return "length";
+ case ExceptionArgument.comparisonType:
+ return "comparisonType";
+ case ExceptionArgument.manager:
+ return "manager";
+ case ExceptionArgument.sourceBytesToCopy:
+ return "sourceBytesToCopy";
+ case ExceptionArgument.callBack:
+ return "callBack";
+ case ExceptionArgument.creationOptions:
+ return "creationOptions";
+ case ExceptionArgument.function:
+ return "function";
+ case ExceptionArgument.delay:
+ return "delay";
+ case ExceptionArgument.millisecondsDelay:
+ return "millisecondsDelay";
+ case ExceptionArgument.millisecondsTimeout:
+ return "millisecondsTimeout";
+ case ExceptionArgument.timeout:
+ return "timeout";
+ case ExceptionArgument.type:
+ return "type";
+ case ExceptionArgument.sourceIndex:
+ return "sourceIndex";
+ case ExceptionArgument.sourceArray:
+ return "sourceArray";
+ case ExceptionArgument.destinationIndex:
+ return "destinationIndex";
+ case ExceptionArgument.destinationArray:
+ return "destinationArray";
+ case ExceptionArgument.other:
+ return "other";
+ case ExceptionArgument.newSize:
+ return "newSize";
+ case ExceptionArgument.lowerBounds:
+ return "lowerBounds";
+ case ExceptionArgument.lengths:
+ return "lengths";
+ case ExceptionArgument.len:
+ return "len";
+ case ExceptionArgument.keys:
+ return "keys";
+ case ExceptionArgument.indices:
+ return "indices";
+ case ExceptionArgument.endIndex:
+ return "endIndex";
+ case ExceptionArgument.elementType:
+ return "elementType";
+ case ExceptionArgument.arrayIndex:
+ return "arrayIndex";
+ default:
+ Debug.Fail("The enum value is not defined, please check the ExceptionArgument Enum.");
+ return argument.ToString();
+ }
+ }
+
+#if false // Reflection-based implementation does not work for CoreRT/ProjectN
+ // This function will convert an ExceptionResource enum value to the resource string.
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private static string GetResourceString(ExceptionResource resource)
+ {
+ Debug.Assert(Enum.IsDefined(typeof(ExceptionResource), resource),
+ "The enum value is not defined, please check the ExceptionResource Enum.");
+
+ return SR.GetResourceString(resource.ToString());
+ }
+#endif
+
+ private static string GetResourceString(ExceptionResource resource)
+ {
+ switch (resource)
+ {
+ case ExceptionResource.ArgumentOutOfRange_Index:
+ return "Argument 'index' was out of the range of valid values.";
+ case ExceptionResource.ArgumentOutOfRange_Count:
+ return "Argument 'count' was out of the range of valid values.";
+ case ExceptionResource.Arg_ArrayPlusOffTooSmall:
+ return "Array plus offset too small.";
+ case ExceptionResource.NotSupported_ReadOnlyCollection:
+ return "This operation is not supported on a read-only collection.";
+ case ExceptionResource.Arg_RankMultiDimNotSupported:
+ return "Multi-dimensional arrays are not supported.";
+ case ExceptionResource.Arg_NonZeroLowerBound:
+ return "Arrays with a non-zero lower bound are not supported.";
+ case ExceptionResource.ArgumentOutOfRange_ListInsert:
+ return "Insertion index was out of the range of valid values.";
+ case ExceptionResource.ArgumentOutOfRange_NeedNonNegNum:
+ return "The number must be non-negative.";
+ case ExceptionResource.ArgumentOutOfRange_SmallCapacity:
+ return "The capacity cannot be set below the current Count.";
+ case ExceptionResource.Argument_InvalidOffLen:
+ return "Invalid offset length.";
+ case ExceptionResource.ArgumentOutOfRange_BiggerThanCollection:
+ return "The given value was larger than the size of the collection.";
+ case ExceptionResource.Serialization_MissingKeys:
+ return "Serialization error: missing keys.";
+ case ExceptionResource.Serialization_NullKey:
+ return "Serialization error: null key.";
+ case ExceptionResource.NotSupported_KeyCollectionSet:
+ return "The KeyCollection does not support modification.";
+ case ExceptionResource.NotSupported_ValueCollectionSet:
+ return "The ValueCollection does not support modification.";
+ case ExceptionResource.InvalidOperation_NullArray:
+ return "Null arrays are not supported.";
+ case ExceptionResource.InvalidOperation_HSCapacityOverflow:
+ return "Set hash capacity overflow. Cannot increase size.";
+ case ExceptionResource.NotSupported_StringComparison:
+ return "String comparison not supported.";
+ case ExceptionResource.ConcurrentCollection_SyncRoot_NotSupported:
+ return "SyncRoot not supported.";
+ case ExceptionResource.ArgumentException_OtherNotArrayOfCorrectLength:
+ return "The other array is not of the correct length.";
+ case ExceptionResource.ArgumentOutOfRange_EndIndexStartIndex:
+ return "The end index does not come after the start index.";
+ case ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported:
+ return "Huge arrays are not supported.";
+ case ExceptionResource.Argument_AddingDuplicate:
+ return "Duplicate item added.";
+ case ExceptionResource.Argument_InvalidArgumentForComparison:
+ return "Invalid argument for comparison.";
+ case ExceptionResource.Arg_LowerBoundsMustMatch:
+ return "Array lower bounds must match.";
+ case ExceptionResource.Arg_MustBeType:
+ return "Argument must be of type: ";
+ case ExceptionResource.InvalidOperation_IComparerFailed:
+ return "IComparer failed.";
+ case ExceptionResource.NotSupported_FixedSizeCollection:
+ return "This operation is not suppored on a fixed-size collection.";
+ case ExceptionResource.Rank_MultiDimNotSupported:
+ return "Multi-dimensional arrays are not supported.";
+ case ExceptionResource.Arg_TypeNotSupported:
+ return "Type not supported.";
+ default:
+ Debug.Assert(false,
+ "The enum value is not defined, please check the ExceptionResource Enum.");
+ return resource.ToString();
+ }
+ }
+ }
+
+ //
+ // The convention for this enum is using the argument name as the enum name
+ //
+ internal enum ExceptionArgument
+ {
+ obj,
+ dictionary,
+ array,
+ info,
+ key,
+ text,
+ values,
+ value,
+ startIndex,
+ task,
+ ch,
+ s,
+ input,
+ list,
+ index,
+ capacity,
+ collection,
+ item,
+ converter,
+ match,
+ count,
+ action,
+ comparison,
+ exceptions,
+ exception,
+ enumerable,
+ start,
+ format,
+ culture,
+ comparer,
+ comparable,
+ source,
+ state,
+ length,
+ comparisonType,
+ manager,
+ sourceBytesToCopy,
+ callBack,
+ creationOptions,
+ function,
+ delay,
+ millisecondsDelay,
+ millisecondsTimeout,
+ timeout,
+ type,
+ sourceIndex,
+ sourceArray,
+ destinationIndex,
+ destinationArray,
+ other,
+ newSize,
+ lowerBounds,
+ lengths,
+ len,
+ keys,
+ indices,
+ endIndex,
+ elementType,
+ arrayIndex
+ }
+
+ //
+ // The convention for this enum is using the resource name as the enum name
+ //
+ internal enum ExceptionResource
+ {
+ ArgumentOutOfRange_Index,
+ ArgumentOutOfRange_Count,
+ Arg_ArrayPlusOffTooSmall,
+ NotSupported_ReadOnlyCollection,
+ Arg_RankMultiDimNotSupported,
+ Arg_NonZeroLowerBound,
+ ArgumentOutOfRange_ListInsert,
+ ArgumentOutOfRange_NeedNonNegNum,
+ ArgumentOutOfRange_SmallCapacity,
+ Argument_InvalidOffLen,
+ ArgumentOutOfRange_BiggerThanCollection,
+ Serialization_MissingKeys,
+ Serialization_NullKey,
+ NotSupported_KeyCollectionSet,
+ NotSupported_ValueCollectionSet,
+ InvalidOperation_NullArray,
+ InvalidOperation_HSCapacityOverflow,
+ NotSupported_StringComparison,
+ ConcurrentCollection_SyncRoot_NotSupported,
+ ArgumentException_OtherNotArrayOfCorrectLength,
+ ArgumentOutOfRange_EndIndexStartIndex,
+ ArgumentOutOfRange_HugeArrayNotSupported,
+ Argument_AddingDuplicate,
+ Argument_InvalidArgumentForComparison,
+ Arg_LowerBoundsMustMatch,
+ Arg_MustBeType,
+ InvalidOperation_IComparerFailed,
+ NotSupported_FixedSizeCollection,
+ Rank_MultiDimNotSupported,
+ Arg_TypeNotSupported,
+ }
+}
diff --git a/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs b/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs
index 1a9347e317..f27bb5fac6 100644
--- a/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs
+++ b/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs
@@ -7,6 +7,7 @@ using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Avalonia.Controls.Primitives;
+using Avalonia.Rendering;
using Avalonia.VisualTree;
namespace Avalonia.Controls.Notifications
@@ -14,7 +15,7 @@ namespace Avalonia.Controls.Notifications
///
/// An that displays notifications in a .
///
- public class WindowNotificationManager : TemplatedControl, IManagedNotificationManager
+ public class WindowNotificationManager : TemplatedControl, IManagedNotificationManager, ICustomSimpleHitTest
{
private IList _items;
@@ -153,5 +154,7 @@ namespace Avalonia.Controls.Notifications
adornerLayer?.Children.Add(this);
}
+
+ public bool HitTest(Point point) => VisualChildren.HitTestCustom(point);
}
}
diff --git a/src/Avalonia.Controls/Primitives/AdornerLayer.cs b/src/Avalonia.Controls/Primitives/AdornerLayer.cs
index ebe5e0a93e..9a2f0310d7 100644
--- a/src/Avalonia.Controls/Primitives/AdornerLayer.cs
+++ b/src/Avalonia.Controls/Primitives/AdornerLayer.cs
@@ -138,10 +138,7 @@ namespace Avalonia.Controls.Primitives
}
}
- public bool HitTest(Point point)
- {
- return Children.Any(ctrl => ctrl.TransformedBounds?.Contains(point) == true);
- }
+ public bool HitTest(Point point) => Children.HitTestCustom(point);
private class AdornedElementInfo
{
diff --git a/src/Avalonia.Controls/Primitives/OverlayLayer.cs b/src/Avalonia.Controls/Primitives/OverlayLayer.cs
index 487a5e91e4..5150033a53 100644
--- a/src/Avalonia.Controls/Primitives/OverlayLayer.cs
+++ b/src/Avalonia.Controls/Primitives/OverlayLayer.cs
@@ -21,11 +21,8 @@ namespace Avalonia.Controls.Primitives
return null;
}
-
- public bool HitTest(Point point)
- {
- return Children.Any(ctrl => ctrl.TransformedBounds?.Contains(point) == true);
- }
+
+ public bool HitTest(Point point) => Children.HitTestCustom(point);
protected override Size ArrangeOverride(Size finalSize)
{
diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
index a5bbcec186..69da211aa4 100644
--- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
+++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
@@ -5,6 +5,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
+using System.Diagnostics;
using System.Linq;
using Avalonia.Collections;
using Avalonia.Controls.Generators;
@@ -240,17 +241,14 @@ namespace Avalonia.Controls.Primitives
public override void BeginInit()
{
base.BeginInit();
- ++_updateCount;
- _updateSelectedIndex = int.MinValue;
+
+ InternalBeginInit();
}
///
public override void EndInit()
{
- if (--_updateCount == 0)
- {
- UpdateFinished();
- }
+ InternalEndInit();
base.EndInit();
}
@@ -437,7 +435,8 @@ namespace Avalonia.Controls.Primitives
protected override void OnDataContextBeginUpdate()
{
base.OnDataContextBeginUpdate();
- ++_updateCount;
+
+ InternalBeginInit();
}
///
@@ -445,10 +444,7 @@ namespace Avalonia.Controls.Primitives
{
base.OnDataContextEndUpdate();
- if (--_updateCount == 0)
- {
- UpdateFinished();
- }
+ InternalEndInit();
}
protected override void OnKeyDown(KeyEventArgs e)
@@ -1118,6 +1114,26 @@ namespace Avalonia.Controls.Primitives
}
}
+ private void InternalBeginInit()
+ {
+ if (_updateCount == 0)
+ {
+ _updateSelectedIndex = int.MinValue;
+ }
+
+ ++_updateCount;
+ }
+
+ private void InternalEndInit()
+ {
+ Debug.Assert(_updateCount > 0);
+
+ if (--_updateCount == 0)
+ {
+ UpdateFinished();
+ }
+ }
+
private class Selection : IEnumerable
{
private readonly List _list = new List();
diff --git a/src/Avalonia.Controls/Primitives/ToggleButton.cs b/src/Avalonia.Controls/Primitives/ToggleButton.cs
index e7cd0697a0..4b3e8e2110 100644
--- a/src/Avalonia.Controls/Primitives/ToggleButton.cs
+++ b/src/Avalonia.Controls/Primitives/ToggleButton.cs
@@ -7,8 +7,14 @@ using Avalonia.Data;
namespace Avalonia.Controls.Primitives
{
+ ///
+ /// Represents a control that a user can select (check) or clear (uncheck). Base class for controls that can switch states.
+ ///
public class ToggleButton : Button
{
+ ///
+ /// Defines the property.
+ ///
public static readonly DirectProperty IsCheckedProperty =
AvaloniaProperty.RegisterDirect(
nameof(IsChecked),
@@ -17,9 +23,30 @@ namespace Avalonia.Controls.Primitives
unsetValue: null,
defaultBindingMode: BindingMode.TwoWay);
+ ///
+ /// Defines the property.
+ ///
public static readonly StyledProperty IsThreeStateProperty =
AvaloniaProperty.Register(nameof(IsThreeState));
+ ///
+ /// Defines the event.
+ ///
+ public static readonly RoutedEvent CheckedEvent =
+ RoutedEvent.Register(nameof(Checked), RoutingStrategies.Bubble);
+
+ ///
+ /// Defines the event.
+ ///
+ public static readonly RoutedEvent UncheckedEvent =
+ RoutedEvent.Register(nameof(Unchecked), RoutingStrategies.Bubble);
+
+ ///
+ /// Defines the event.
+ ///
+ public static readonly RoutedEvent IndeterminateEvent =
+ RoutedEvent.Register(nameof(Indeterminate), RoutingStrategies.Bubble);
+
private bool? _isChecked = false;
static ToggleButton()
@@ -27,14 +54,49 @@ namespace Avalonia.Controls.Primitives
PseudoClass(IsCheckedProperty, c => c == true, ":checked");
PseudoClass(IsCheckedProperty, c => c == false, ":unchecked");
PseudoClass(IsCheckedProperty, c => c == null, ":indeterminate");
+
+ IsCheckedProperty.Changed.AddClassHandler((x, e) => x.OnIsCheckedChanged(e));
+ }
+
+ ///
+ /// Raised when a is checked.
+ ///
+ public event EventHandler Checked
+ {
+ add => AddHandler(CheckedEvent, value);
+ remove => RemoveHandler(CheckedEvent, value);
+ }
+
+ ///
+ /// Raised when a is unchecked.
+ ///
+ public event EventHandler Unchecked
+ {
+ add => AddHandler(UncheckedEvent, value);
+ remove => RemoveHandler(UncheckedEvent, value);
+ }
+
+ ///
+ /// Raised when a is neither checked nor unchecked.
+ ///
+ public event EventHandler Indeterminate
+ {
+ add => AddHandler(IndeterminateEvent, value);
+ remove => RemoveHandler(IndeterminateEvent, value);
}
+ ///
+ /// Gets or sets whether the is checked.
+ ///
public bool? IsChecked
{
- get { return _isChecked; }
- set { SetAndRaise(IsCheckedProperty, ref _isChecked, value); }
+ get => _isChecked;
+ set => SetAndRaise(IsCheckedProperty, ref _isChecked, value);
}
+ ///
+ /// Gets or sets a value that indicates whether the control supports three states.
+ ///
public bool IsThreeState
{
get => GetValue(IsThreeStateProperty);
@@ -47,18 +109,78 @@ namespace Avalonia.Controls.Primitives
base.OnClick();
}
+ ///
+ /// Toggles the property.
+ ///
protected virtual void Toggle()
{
if (IsChecked.HasValue)
+ {
if (IsChecked.Value)
+ {
if (IsThreeState)
+ {
IsChecked = null;
+ }
else
+ {
IsChecked = false;
+ }
+ }
else
+ {
IsChecked = true;
+ }
+ }
else
+ {
IsChecked = false;
+ }
+ }
+
+ ///
+ /// Called when becomes true.
+ ///
+ /// Event arguments for the routed event that is raised by the default implementation of this method.
+ protected virtual void OnChecked(RoutedEventArgs e)
+ {
+ RaiseEvent(e);
+ }
+
+ ///
+ /// Called when becomes false.
+ ///
+ /// Event arguments for the routed event that is raised by the default implementation of this method.
+ protected virtual void OnUnchecked(RoutedEventArgs e)
+ {
+ RaiseEvent(e);
+ }
+
+ ///
+ /// Called when becomes null.
+ ///
+ /// Event arguments for the routed event that is raised by the default implementation of this method.
+ protected virtual void OnIndeterminate(RoutedEventArgs e)
+ {
+ RaiseEvent(e);
+ }
+
+ private void OnIsCheckedChanged(AvaloniaPropertyChangedEventArgs e)
+ {
+ var newValue = (bool?)e.NewValue;
+
+ switch (newValue)
+ {
+ case true:
+ OnChecked(new RoutedEventArgs(CheckedEvent));
+ break;
+ case false:
+ OnUnchecked(new RoutedEventArgs(UncheckedEvent));
+ break;
+ default:
+ OnIndeterminate(new RoutedEventArgs(IndeterminateEvent));
+ break;
+ }
}
}
}
diff --git a/src/Avalonia.Controls/Repeater/ItemsRepeater.cs b/src/Avalonia.Controls/Repeater/ItemsRepeater.cs
index 0e2136a6f3..cbac1d6c1b 100644
--- a/src/Avalonia.Controls/Repeater/ItemsRepeater.cs
+++ b/src/Avalonia.Controls/Repeater/ItemsRepeater.cs
@@ -562,33 +562,35 @@ namespace Avalonia.Controls
if (Layout != null)
{
- if (Layout is VirtualizingLayout virtualLayout)
- {
- var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
+ var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
+ try
+ {
_processingItemsSourceChange = args;
- try
+ if (Layout is VirtualizingLayout virtualLayout)
{
virtualLayout.OnItemsChanged(GetLayoutContext(), newValue, args);
}
- finally
- {
- _processingItemsSourceChange = null;
- }
- }
- else if (Layout is NonVirtualizingLayout nonVirtualLayout)
- {
- // Walk through all the elements and make sure they are cleared for
- // non-virtualizing layouts.
- foreach (var element in Children)
+ else if (Layout is NonVirtualizingLayout nonVirtualLayout)
{
- if (GetVirtualizationInfo(element).IsRealized)
+ // Walk through all the elements and make sure they are cleared for
+ // non-virtualizing layouts.
+ foreach (var element in Children)
{
- ClearElementImpl(element);
+ if (GetVirtualizationInfo(element).IsRealized)
+ {
+ ClearElementImpl(element);
+ }
}
+
+ Children.Clear();
}
}
+ finally
+ {
+ _processingItemsSourceChange = null;
+ }
InvalidateMeasure();
}
diff --git a/src/Avalonia.Controls/Repeater/ViewManager.cs b/src/Avalonia.Controls/Repeater/ViewManager.cs
index 51c14d47d6..7d005a30b4 100644
--- a/src/Avalonia.Controls/Repeater/ViewManager.cs
+++ b/src/Avalonia.Controls/Repeater/ViewManager.cs
@@ -109,11 +109,22 @@ namespace Avalonia.Controls
public void ClearElementToElementFactory(IControl element)
{
- var virtInfo = ItemsRepeater.GetVirtualizationInfo(element);
- var clearedIndex = virtInfo.Index;
_owner.OnElementClearing(element);
- _owner.ItemTemplateShim.RecycleElement(_owner, element);
+ if (_owner.ItemTemplateShim != null)
+ {
+ _owner.ItemTemplateShim.RecycleElement(_owner, element);
+ }
+ else
+ {
+ // No ItemTemplate to recycle to, remove the element from the children collection.
+ if (!_owner.Children.Remove(element))
+ {
+ throw new InvalidOperationException("ItemsRepeater's child not found in its Children collection.");
+ }
+ }
+
+ var virtInfo = ItemsRepeater.GetVirtualizationInfo(element);
virtInfo.MoveOwnershipToElementFactory();
if (_lastFocusedElement == element)
@@ -121,9 +132,8 @@ namespace Avalonia.Controls
// Focused element is going away. Remove the tracked last focused element
// and pick a reasonable next focus if we can find one within the layout
// realized elements.
- MoveFocusFromClearedIndex(clearedIndex);
+ MoveFocusFromClearedIndex(virtInfo.Index);
}
-
}
private void MoveFocusFromClearedIndex(int clearedIndex)
@@ -190,7 +200,8 @@ namespace Avalonia.Controls
{
if (virtInfo == null)
{
- throw new ArgumentException("Element is not a child of this ItemsRepeater.");
+ //Element is not a child of this ItemsRepeater.
+ return -1;
}
return virtInfo.IsRealized || virtInfo.IsInUniqueIdResetPool ? virtInfo.Index : -1;
@@ -515,21 +526,52 @@ namespace Avalonia.Controls
return element;
}
+ // There are several cases handled here with respect to which element gets returned and when DataContext is modified.
+ //
+ // 1. If there is no ItemTemplate:
+ // 1.1 If data is an IControl -> the data is returned
+ // 1.2 If data is not an IControl -> a default DataTemplate is used to fetch element and DataContext is set to data
+ //
+ // 2. If there is an ItemTemplate:
+ // 2.1 If data is not an IControl -> Element is fetched from ElementFactory and DataContext is set to the data
+ // 2.2 If data is an IControl:
+ // 2.2.1 If Element returned by the ElementFactory is the same as the data -> Element (a.k.a. data) is returned as is
+ // 2.2.2 If Element returned by the ElementFactory is not the same as the data
+ // -> Element that is fetched from the ElementFactory is returned and
+ // DataContext is set to the data's DataContext (if it exists), otherwise it is set to the data itself
private IControl GetElementFromElementFactory(int index)
{
// The view generator is the provider of last resort.
+ var data = _owner.ItemsSourceView.GetAt(index);
+ var providedElementFactory = _owner.ItemTemplateShim;
+
+ ItemTemplateWrapper GetElementFactory()
+ {
+ if (providedElementFactory == null)
+ {
+ var factory = FuncDataTemplate.Default;
+ _owner.ItemTemplate = factory;
+ return _owner.ItemTemplateShim;
+ }
- var itemTemplateFactory = _owner.ItemTemplateShim;
- if (itemTemplateFactory == null)
+ return providedElementFactory;
+ }
+
+ IControl GetElement()
{
- // If no ItemTemplate was provided, use a default
- var factory = FuncDataTemplate.Default;
- _owner.ItemTemplate = factory;
- itemTemplateFactory = _owner.ItemTemplateShim;
+ if (providedElementFactory == null)
+ {
+ if (data is IControl dataAsElement)
+ {
+ return dataAsElement;
+ }
+ }
+
+ var elementFactory = GetElementFactory();
+ return elementFactory.GetElement(_owner, data);
}
- var data = _owner.ItemsSourceView.GetAt(index);
- var element = itemTemplateFactory.GetElement(_owner, data);
+ var element = GetElement();
var virtInfo = ItemsRepeater.TryGetVirtualizationInfo(element);
if (virtInfo == null)
@@ -537,8 +579,11 @@ namespace Avalonia.Controls
virtInfo = ItemsRepeater.CreateAndInitializeVirtualizationInfo(element);
}
- // Prepare the element
- element.DataContext = data;
+ if (data != element)
+ {
+ // Prepare the element
+ element.DataContext = data;
+ }
virtInfo.MoveOwnershipToLayoutFromElementFactory(
index,
diff --git a/src/Avalonia.Input/AccessKeyHandler.cs b/src/Avalonia.Input/AccessKeyHandler.cs
index aa009770f6..00e68d629b 100644
--- a/src/Avalonia.Input/AccessKeyHandler.cs
+++ b/src/Avalonia.Input/AccessKeyHandler.cs
@@ -140,7 +140,7 @@ namespace Avalonia.Input
/// The event args.
protected virtual void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
- if (e.Key == Key.LeftAlt)
+ if (e.Key == Key.LeftAlt || e.Key == Key.RightAlt)
{
_altIsDown = true;
@@ -218,6 +218,7 @@ namespace Avalonia.Input
switch (e.Key)
{
case Key.LeftAlt:
+ case Key.RightAlt:
_altIsDown = false;
if (_ignoreAltUp)
diff --git a/src/Avalonia.Input/InputExtensions.cs b/src/Avalonia.Input/InputExtensions.cs
index f83c41e266..4dcee0bbc8 100644
--- a/src/Avalonia.Input/InputExtensions.cs
+++ b/src/Avalonia.Input/InputExtensions.cs
@@ -38,7 +38,9 @@ namespace Avalonia.Input
/// The topmost at the specified position.
public static IInputElement InputHitTest(this IInputElement element, Point p)
{
- return element.GetInputElementsAt(p).FirstOrDefault();
+ Contract.Requires(element != null);
+
+ return element.GetVisualAt(p, s_hitTestDelegate) as IInputElement;
}
private static bool IsHitTestVisible(IVisual visual)
diff --git a/src/Avalonia.Layout/FlowLayoutAlgorithm.cs b/src/Avalonia.Layout/FlowLayoutAlgorithm.cs
index 615ce725bd..7f44c80a64 100644
--- a/src/Avalonia.Layout/FlowLayoutAlgorithm.cs
+++ b/src/Avalonia.Layout/FlowLayoutAlgorithm.cs
@@ -72,6 +72,7 @@ namespace Avalonia.Layout
bool isWrapping,
double minItemSpacing,
double lineSpacing,
+ int maxItemsPerLine,
ScrollOrientation orientation,
string layoutId)
{
@@ -94,14 +95,14 @@ namespace Avalonia.Layout
_elementManager.OnBeginMeasure(orientation);
int anchorIndex = GetAnchorIndex(availableSize, isWrapping, minItemSpacing, layoutId);
- Generate(GenerateDirection.Forward, anchorIndex, availableSize, minItemSpacing, lineSpacing, layoutId);
- Generate(GenerateDirection.Backward, anchorIndex, availableSize, minItemSpacing, lineSpacing, layoutId);
+ Generate(GenerateDirection.Forward, anchorIndex, availableSize, minItemSpacing, lineSpacing, maxItemsPerLine, layoutId);
+ Generate(GenerateDirection.Backward, anchorIndex, availableSize, minItemSpacing, lineSpacing, maxItemsPerLine, layoutId);
if (isWrapping && IsReflowRequired())
{
var firstElementBounds = _elementManager.GetLayoutBoundsForRealizedIndex(0);
_orientation.SetMinorStart(ref firstElementBounds, 0);
_elementManager.SetLayoutBoundsForRealizedIndex(0, firstElementBounds);
- Generate(GenerateDirection.Forward, 0 /*anchorIndex*/, availableSize, minItemSpacing, lineSpacing, layoutId);
+ Generate(GenerateDirection.Forward, 0 /*anchorIndex*/, availableSize, minItemSpacing, lineSpacing, maxItemsPerLine, layoutId);
}
RaiseLineArranged();
@@ -115,10 +116,11 @@ namespace Avalonia.Layout
public Size Arrange(
Size finalSize,
VirtualizingLayoutContext context,
+ bool isWrapping,
LineAlignment lineAlignment,
string layoutId)
{
- ArrangeVirtualizingLayout(finalSize, lineAlignment, layoutId);
+ ArrangeVirtualizingLayout(finalSize, lineAlignment, isWrapping, layoutId);
return new Size(
Math.Max(finalSize.Width, _lastExtent.Width),
@@ -270,6 +272,7 @@ namespace Avalonia.Layout
Size availableSize,
double minItemSpacing,
double lineSpacing,
+ int maxItemsPerLine,
string layoutId)
{
if (anchorIndex != -1)
@@ -280,7 +283,7 @@ namespace Avalonia.Layout
var anchorBounds = _elementManager.GetLayoutBoundsForDataIndex(anchorIndex);
var lineOffset = _orientation.MajorStart(anchorBounds);
var lineMajorSize = _orientation.MajorSize(anchorBounds);
- int countInLine = 1;
+ var countInLine = 1;
int count = 0;
bool lineNeedsReposition = false;
@@ -301,7 +304,7 @@ namespace Avalonia.Layout
if (direction == GenerateDirection.Forward)
{
double remainingSpace = _orientation.Minor(availableSize) - (_orientation.MinorStart(previousElementBounds) + _orientation.MinorSize(previousElementBounds) + minItemSpacing + _orientation.Minor(desiredSize));
- if (_algorithmCallbacks.Algorithm_ShouldBreakLine(currentIndex, remainingSpace))
+ if (countInLine >= maxItemsPerLine || _algorithmCallbacks.Algorithm_ShouldBreakLine(currentIndex, remainingSpace))
{
// No more space in this row. wrap to next row.
_orientation.SetMinorStart(ref currentBounds, 0);
@@ -339,7 +342,7 @@ namespace Avalonia.Layout
{
// Backward
double remainingSpace = _orientation.MinorStart(previousElementBounds) - (_orientation.Minor(desiredSize) + minItemSpacing);
- if (_algorithmCallbacks.Algorithm_ShouldBreakLine(currentIndex, remainingSpace))
+ if (countInLine >= maxItemsPerLine || _algorithmCallbacks.Algorithm_ShouldBreakLine(currentIndex, remainingSpace))
{
// Does not fit, wrap to the previous row
var availableSizeMinor = _orientation.Minor(availableSize);
@@ -544,6 +547,7 @@ namespace Avalonia.Layout
private void ArrangeVirtualizingLayout(
Size finalSize,
LineAlignment lineAlignment,
+ bool isWrapping,
string layoutId)
{
// Walk through the realized elements one line at a time and
@@ -563,7 +567,7 @@ namespace Avalonia.Layout
if (_orientation.MajorStart(currentBounds) != currentLineOffset)
{
spaceAtLineEnd = _orientation.Minor(finalSize) - _orientation.MinorStart(previousElementBounds) - _orientation.MinorSize(previousElementBounds);
- PerformLineAlignment(i - countInLine, countInLine, spaceAtLineStart, spaceAtLineEnd, currentLineSize, lineAlignment, layoutId);
+ PerformLineAlignment(i - countInLine, countInLine, spaceAtLineStart, spaceAtLineEnd, currentLineSize, lineAlignment, isWrapping, finalSize, layoutId);
spaceAtLineStart = _orientation.MinorStart(currentBounds);
countInLine = 0;
currentLineOffset = _orientation.MajorStart(currentBounds);
@@ -580,7 +584,7 @@ namespace Avalonia.Layout
if (countInLine > 0)
{
var spaceAtEnd = _orientation.Minor(finalSize) - _orientation.MinorStart(previousElementBounds) - _orientation.MinorSize(previousElementBounds);
- PerformLineAlignment(realizedElementCount - countInLine, countInLine, spaceAtLineStart, spaceAtEnd, currentLineSize, lineAlignment, layoutId);
+ PerformLineAlignment(realizedElementCount - countInLine, countInLine, spaceAtLineStart, spaceAtEnd, currentLineSize, lineAlignment, isWrapping, finalSize, layoutId);
}
}
}
@@ -594,6 +598,8 @@ namespace Avalonia.Layout
double spaceAtLineEnd,
double lineSize,
LineAlignment lineAlignment,
+ bool isWrapping,
+ Size finalSize,
string layoutId)
{
for (int rangeIndex = lineStartIndex; rangeIndex < lineStartIndex + countInLine; ++rangeIndex)
@@ -659,6 +665,14 @@ namespace Avalonia.Layout
}
bounds = bounds.Translate(-_lastExtent.Position);
+
+ if (!isWrapping)
+ {
+ _orientation.SetMinorSize(
+ ref bounds,
+ Math.Max(_orientation.MinorSize(bounds), _orientation.Minor(finalSize)));
+ }
+
var element = _elementManager.GetAt(rangeIndex);
element.Arrange(bounds);
}
diff --git a/src/Avalonia.Layout/NonVirtualizingLayout.cs b/src/Avalonia.Layout/NonVirtualizingLayout.cs
index fba91e66c7..5d27ba9199 100644
--- a/src/Avalonia.Layout/NonVirtualizingLayout.cs
+++ b/src/Avalonia.Layout/NonVirtualizingLayout.cs
@@ -20,25 +20,25 @@ namespace Avalonia.Layout
///
public sealed override void InitializeForContext(LayoutContext context)
{
- InitializeForContextCore((VirtualizingLayoutContext)context);
+ InitializeForContextCore((NonVirtualizingLayoutContext)context);
}
///
public sealed override void UninitializeForContext(LayoutContext context)
{
- UninitializeForContextCore((VirtualizingLayoutContext)context);
+ UninitializeForContextCore((NonVirtualizingLayoutContext)context);
}
///
public sealed override Size Measure(LayoutContext context, Size availableSize)
{
- return MeasureOverride((VirtualizingLayoutContext)context, availableSize);
+ return MeasureOverride((NonVirtualizingLayoutContext)context, availableSize);
}
///
public sealed override Size Arrange(LayoutContext context, Size finalSize)
{
- return ArrangeOverride((VirtualizingLayoutContext)context, finalSize);
+ return ArrangeOverride((NonVirtualizingLayoutContext)context, finalSize);
}
///
@@ -49,7 +49,7 @@ namespace Avalonia.Layout
/// The context object that facilitates communication between the layout and its host
/// container.
///
- protected virtual void InitializeForContextCore(VirtualizingLayoutContext context)
+ protected virtual void InitializeForContextCore(LayoutContext context)
{
}
@@ -61,7 +61,7 @@ namespace Avalonia.Layout
/// The context object that facilitates communication between the layout and its host
/// container.
///
- protected virtual void UninitializeForContextCore(VirtualizingLayoutContext context)
+ protected virtual void UninitializeForContextCore(LayoutContext context)
{
}
@@ -83,7 +83,7 @@ namespace Avalonia.Layout
/// of the allocated sizes for child objects or based on other considerations such as a
/// fixed container size.
///
- protected abstract Size MeasureOverride(VirtualizingLayoutContext context, Size availableSize);
+ protected abstract Size MeasureOverride(NonVirtualizingLayoutContext context, Size availableSize);
///
/// When implemented in a derived class, provides the behavior for the "Arrange" pass of
@@ -98,6 +98,6 @@ namespace Avalonia.Layout
/// its children.
///
/// The actual size that is used after the element is arranged in layout.
- protected virtual Size ArrangeOverride(VirtualizingLayoutContext context, Size finalSize) => finalSize;
+ protected virtual Size ArrangeOverride(NonVirtualizingLayoutContext context, Size finalSize) => finalSize;
}
}
diff --git a/src/Avalonia.Layout/NonVirtualizingLayoutContext.cs b/src/Avalonia.Layout/NonVirtualizingLayoutContext.cs
new file mode 100644
index 0000000000..d3dec83e9b
--- /dev/null
+++ b/src/Avalonia.Layout/NonVirtualizingLayoutContext.cs
@@ -0,0 +1,14 @@
+// This source file is adapted from the WinUI project.
+// (https://github.com/microsoft/microsoft-ui-xaml)
+//
+// Licensed to The Avalonia Project under MIT License, courtesy of The .NET Foundation.
+
+namespace Avalonia.Layout
+{
+ ///
+ /// Represents the base class for layout context types that do not support virtualization.
+ ///
+ public abstract class NonVirtualizingLayoutContext : LayoutContext
+ {
+ }
+}
diff --git a/src/Avalonia.Layout/StackLayout.cs b/src/Avalonia.Layout/StackLayout.cs
index e9735b9b31..3c3729272c 100644
--- a/src/Avalonia.Layout/StackLayout.cs
+++ b/src/Avalonia.Layout/StackLayout.cs
@@ -267,6 +267,7 @@ namespace Avalonia.Layout
false,
0,
Spacing,
+ int.MaxValue,
_orientation.ScrollOrientation,
LayoutId);
@@ -278,6 +279,7 @@ namespace Avalonia.Layout
var value = GetFlowAlgorithm(context).Arrange(
finalSize,
context,
+ false,
FlowLayoutAlgorithm.LineAlignment.Start,
LayoutId);
diff --git a/src/Avalonia.Layout/UniformGridLayout.cs b/src/Avalonia.Layout/UniformGridLayout.cs
index edc2042922..11a521ed1e 100644
--- a/src/Avalonia.Layout/UniformGridLayout.cs
+++ b/src/Avalonia.Layout/UniformGridLayout.cs
@@ -110,6 +110,12 @@ namespace Avalonia.Layout
public static readonly StyledProperty MinRowSpacingProperty =
AvaloniaProperty.Register(nameof(MinRowSpacing));
+ ///
+ /// Defines the property.
+ ///
+ public static readonly StyledProperty MaximumRowsOrColumnsProperty =
+ AvaloniaProperty.Register(nameof(MinItemWidth));
+
///
/// Defines the property.
///
@@ -123,6 +129,7 @@ namespace Avalonia.Layout
private double _minColumnSpacing;
private UniformGridLayoutItemsJustification _itemsJustification;
private UniformGridLayoutItemsStretch _itemsStretch;
+ private int _maximumRowsOrColumns = int.MaxValue;
///
/// Initializes a new instance of the class.
@@ -219,6 +226,15 @@ namespace Avalonia.Layout
set => SetValue(MinRowSpacingProperty, value);
}
+ ///
+ /// Gets or sets the maximum row or column count.
+ ///
+ public int MaximumRowsOrColumns
+ {
+ get => GetValue(MaximumRowsOrColumnsProperty);
+ set => SetValue(MaximumRowsOrColumnsProperty, value);
+ }
+
///
/// Gets or sets the axis along which items are laid out.
///
@@ -269,15 +285,17 @@ namespace Avalonia.Layout
{
var gridState = (UniformGridLayoutState)context.LayoutState;
var lastExtent = gridState.FlowAlgorithm.LastExtent;
- int itemsPerLine = Math.Max(1, (int)(_orientation.Minor(availableSize) / GetMinorSizeWithSpacing(context)));
- double majorSize = (itemsCount / itemsPerLine) * GetMajorSizeWithSpacing(context);
- double realizationWindowStartWithinExtent = _orientation.MajorStart(realizationRect) - _orientation.MajorStart(lastExtent);
+ var itemsPerLine = Math.Min( // note use of unsigned ints
+ Math.Max(1u, (uint)(_orientation.Minor(availableSize) / GetMinorSizeWithSpacing(context))),
+ Math.Max(1u, (uint)_maximumRowsOrColumns));
+ var majorSize = (itemsCount / itemsPerLine) * GetMajorSizeWithSpacing(context);
+ var realizationWindowStartWithinExtent = _orientation.MajorStart(realizationRect) - _orientation.MajorStart(lastExtent);
if ((realizationWindowStartWithinExtent + _orientation.MajorSize(realizationRect)) >= 0 && realizationWindowStartWithinExtent <= majorSize)
{
double offset = Math.Max(0.0, _orientation.MajorStart(realizationRect) - _orientation.MajorStart(lastExtent));
int anchorRowIndex = (int)(offset / GetMajorSizeWithSpacing(context));
- anchorIndex = Math.Max(0, Math.Min(itemsCount - 1, anchorRowIndex * itemsPerLine));
+ anchorIndex = (int)Math.Max(0, Math.Min(itemsCount - 1, anchorRowIndex * itemsPerLine));
bounds = GetLayoutRectForDataIndex(availableSize, anchorIndex, lastExtent, context);
}
}
@@ -299,7 +317,9 @@ namespace Avalonia.Layout
int count = context.ItemCount;
if (targetIndex >= 0 && targetIndex < count)
{
- int itemsPerLine = Math.Max(1, (int)(_orientation.Minor(availableSize) / GetMinorSizeWithSpacing(context)));
+ int itemsPerLine = (int)Math.Min( // note use of unsigned ints
+ Math.Max(1u, (uint)(_orientation.Minor(availableSize) / GetMinorSizeWithSpacing(context))),
+ Math.Max(1u, _maximumRowsOrColumns));
int indexOfFirstInLine = (targetIndex / itemsPerLine) * itemsPerLine;
index = indexOfFirstInLine;
var state = context.LayoutState as UniformGridLayoutState;
@@ -329,17 +349,21 @@ namespace Avalonia.Layout
// Constants
int itemsCount = context.ItemCount;
double availableSizeMinor = _orientation.Minor(availableSize);
- int itemsPerLine = Math.Max(1, !double.IsInfinity(availableSizeMinor) ?
- (int)(availableSizeMinor / GetMinorSizeWithSpacing(context)) : itemsCount);
+ int itemsPerLine =
+ (int)Math.Min( // note use of unsigned ints
+ Math.Max(1u, !double.IsInfinity(availableSizeMinor)
+ ? (uint)(availableSizeMinor / GetMinorSizeWithSpacing(context))
+ : (uint)itemsCount),
+ Math.Max(1u, _maximumRowsOrColumns));
double lineSize = GetMajorSizeWithSpacing(context);
if (itemsCount > 0)
{
_orientation.SetMinorSize(
ref extent,
- !double.IsInfinity(availableSizeMinor) ?
+ !double.IsInfinity(availableSizeMinor) && _itemsStretch == UniformGridLayoutItemsStretch.Fill ?
availableSizeMinor :
- Math.Max(0.0, itemsCount * GetMinorSizeWithSpacing(context) - (double)MinItemSpacing));
+ Math.Max(0.0, itemsPerLine * GetMinorSizeWithSpacing(context) - (double)MinItemSpacing));
_orientation.SetMajorSize(
ref extent,
Math.Max(0.0, (itemsCount / itemsPerLine) * lineSize - (double)LineSpacing));
@@ -398,7 +422,7 @@ namespace Avalonia.Layout
// Set the width and height on the grid state. If the user already set them then use the preset.
// If not, we have to measure the first element and get back a size which we're going to be using for the rest of the items.
var gridState = (UniformGridLayoutState)context.LayoutState;
- gridState.EnsureElementSize(availableSize, context, _minItemWidth, _minItemHeight, _itemsStretch, Orientation, MinRowSpacing, MinColumnSpacing);
+ gridState.EnsureElementSize(availableSize, context, _minItemWidth, _minItemHeight, _itemsStretch, Orientation, MinRowSpacing, MinColumnSpacing, _maximumRowsOrColumns);
var desiredSize = GetFlowAlgorithm(context).Measure(
availableSize,
@@ -406,6 +430,7 @@ namespace Avalonia.Layout
true,
MinItemSpacing,
LineSpacing,
+ _maximumRowsOrColumns,
_orientation.ScrollOrientation,
LayoutId);
@@ -421,6 +446,7 @@ namespace Avalonia.Layout
var value = GetFlowAlgorithm(context).Arrange(
finalSize,
context,
+ true,
(FlowLayoutAlgorithm.LineAlignment)_itemsJustification,
LayoutId);
return new Size(value.Width, value.Height);
@@ -471,6 +497,10 @@ namespace Avalonia.Layout
{
_minItemHeight = (double)args.NewValue;
}
+ else if (args.Property == MaximumRowsOrColumnsProperty)
+ {
+ _maximumRowsOrColumns = (int)args.NewValue;
+ }
InvalidateLayout();
}
@@ -499,7 +529,9 @@ namespace Avalonia.Layout
Rect lastExtent,
VirtualizingLayoutContext context)
{
- int itemsPerLine = Math.Max(1, (int)(_orientation.Minor(availableSize) / GetMinorSizeWithSpacing(context)));
+ int itemsPerLine = (int)Math.Min( //note use of unsigned ints
+ Math.Max(1u, (uint)(_orientation.Minor(availableSize) / GetMinorSizeWithSpacing(context))),
+ Math.Max(1u, _maximumRowsOrColumns));
int rowIndex = (int)(index / itemsPerLine);
int indexInRow = index - (rowIndex * itemsPerLine);
diff --git a/src/Avalonia.Layout/UniformGridLayoutState.cs b/src/Avalonia.Layout/UniformGridLayoutState.cs
index e6d75bcf35..62c5174775 100644
--- a/src/Avalonia.Layout/UniformGridLayoutState.cs
+++ b/src/Avalonia.Layout/UniformGridLayoutState.cs
@@ -48,8 +48,14 @@ namespace Avalonia.Layout
UniformGridLayoutItemsStretch stretch,
Orientation orientation,
double minRowSpacing,
- double minColumnSpacing)
+ double minColumnSpacing,
+ int maxItemsPerLine)
{
+ if (maxItemsPerLine == 0)
+ {
+ maxItemsPerLine = 1;
+ }
+
if (context.ItemCount > 0)
{
// If the first element is realized we don't need to cache it or to get it from the context
@@ -57,7 +63,7 @@ namespace Avalonia.Layout
if (realizedElement != null)
{
realizedElement.Measure(availableSize);
- SetSize(realizedElement, layoutItemWidth, LayoutItemHeight, availableSize, stretch, orientation, minRowSpacing, minColumnSpacing);
+ SetSize(realizedElement, layoutItemWidth, LayoutItemHeight, availableSize, stretch, orientation, minRowSpacing, minColumnSpacing, maxItemsPerLine);
_cachedFirstElement = null;
}
else
@@ -72,7 +78,7 @@ namespace Avalonia.Layout
_cachedFirstElement.Measure(availableSize);
- SetSize(_cachedFirstElement, layoutItemWidth, LayoutItemHeight, availableSize, stretch, orientation, minRowSpacing, minColumnSpacing);
+ SetSize(_cachedFirstElement, layoutItemWidth, LayoutItemHeight, availableSize, stretch, orientation, minRowSpacing, minColumnSpacing, maxItemsPerLine);
// See if we can move ownership to the flow algorithm. If we can, we do not need a local cache.
bool added = FlowAlgorithm.TryAddElement0(_cachedFirstElement);
@@ -92,8 +98,14 @@ namespace Avalonia.Layout
UniformGridLayoutItemsStretch stretch,
Orientation orientation,
double minRowSpacing,
- double minColumnSpacing)
+ double minColumnSpacing,
+ int maxItemsPerLine)
{
+ if (maxItemsPerLine == 0)
+ {
+ maxItemsPerLine = 1;
+ }
+
EffectiveItemWidth = (double.IsNaN(layoutItemWidth) ? element.DesiredSize.Width : layoutItemWidth);
EffectiveItemHeight = (double.IsNaN(LayoutItemHeight) ? element.DesiredSize.Height : LayoutItemHeight);
@@ -101,11 +113,17 @@ namespace Avalonia.Layout
var minorItemSpacing = orientation == Orientation.Vertical ? minRowSpacing : minColumnSpacing;
var itemSizeMinor = orientation == Orientation.Horizontal ? EffectiveItemWidth : EffectiveItemHeight;
- itemSizeMinor += minorItemSpacing;
- var numItemsPerColumn = (int)(Math.Max(1.0, availableSizeMinor / itemSizeMinor));
- var remainingSpace = ((int)availableSizeMinor) % ((int)itemSizeMinor);
- var extraMinorPixelsForEachItem = remainingSpace / numItemsPerColumn;
+ double extraMinorPixelsForEachItem = 0.0;
+ if (!double.IsInfinity(availableSizeMinor))
+ {
+ var numItemsPerColumn = Math.Min(
+ maxItemsPerLine,
+ Math.Max(1.0, availableSizeMinor / (itemSizeMinor + minorItemSpacing)));
+ var usedSpace = (numItemsPerColumn * (itemSizeMinor + minorItemSpacing)) - minorItemSpacing;
+ var remainingSpace = ((int)(availableSizeMinor - usedSpace));
+ extraMinorPixelsForEachItem = remainingSpace / ((int)numItemsPerColumn);
+ }
if (stretch == UniformGridLayoutItemsStretch.Fill)
{
diff --git a/src/Avalonia.Visuals/Media/Color.cs b/src/Avalonia.Visuals/Media/Color.cs
index a37463a0f0..c7439b5bf6 100644
--- a/src/Avalonia.Visuals/Media/Color.cs
+++ b/src/Avalonia.Visuals/Media/Color.cs
@@ -19,22 +19,22 @@ namespace Avalonia.Media
}
///
- /// Gets or sets the Alpha component of the color.
+ /// Gets the Alpha component of the color.
///
public byte A { get; }
///
- /// Gets or sets the Red component of the color.
+ /// Gets the Red component of the color.
///
public byte R { get; }
///
- /// Gets or sets the Green component of the color.
+ /// Gets the Green component of the color.
///
public byte G { get; }
///
- /// Gets or sets the Blue component of the color.
+ /// Gets the Blue component of the color.
///
public byte B { get; }
diff --git a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs
index 585b132bd3..b065079564 100644
--- a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs
+++ b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs
@@ -160,16 +160,23 @@ namespace Avalonia.Rendering
///
public IEnumerable HitTest(Point p, IVisual root, Func filter)
{
- if (_renderLoop == null && (_dirty == null || _dirty.Count > 0))
- {
- // When unit testing the renderLoop may be null, so update the scene manually.
- UpdateScene();
- }
+ EnsureCanHitTest();
+
//It's safe to access _scene here without a lock since
//it's only changed from UI thread which we are currently on
return _scene?.Item.HitTest(p, root, filter) ?? Enumerable.Empty();
}
+ ///
+ public IVisual HitTestFirst(Point p, IVisual root, Func filter)
+ {
+ EnsureCanHitTest();
+
+ //It's safe to access _scene here without a lock since
+ //it's only changed from UI thread which we are currently on
+ return _scene?.Item.HitTestFirst(p, root, filter);
+ }
+
///
public void Paint(Rect rect)
{
@@ -235,6 +242,15 @@ namespace Avalonia.Rendering
internal Scene UnitTestScene() => _scene.Item;
+ private void EnsureCanHitTest()
+ {
+ if (_renderLoop == null && (_dirty == null || _dirty.Count > 0))
+ {
+ // When unit testing the renderLoop may be null, so update the scene manually.
+ UpdateScene();
+ }
+ }
+
private void Render(bool forceComposite)
{
using (var l = _lock.TryLock())
diff --git a/src/Avalonia.Visuals/Rendering/ICustomSimpleHitTest.cs b/src/Avalonia.Visuals/Rendering/ICustomSimpleHitTest.cs
index 7199053b08..4c15de0312 100644
--- a/src/Avalonia.Visuals/Rendering/ICustomSimpleHitTest.cs
+++ b/src/Avalonia.Visuals/Rendering/ICustomSimpleHitTest.cs
@@ -1,3 +1,7 @@
+using System.Collections.Generic;
+using System.Linq;
+using Avalonia.VisualTree;
+
namespace Avalonia.Rendering
{
///
@@ -9,4 +13,13 @@ namespace Avalonia.Rendering
{
bool HitTest(Point point);
}
+
+ public static class CustomSimpleHitTestExtensions
+ {
+ public static bool HitTestCustom(this IVisual visual, Point point)
+ => (visual as ICustomSimpleHitTest)?.HitTest(point) ?? visual.TransformedBounds?.Contains(point) == true;
+
+ public static bool HitTestCustom(this IEnumerable children, Point point)
+ => children.Any(ctrl => ctrl.HitTestCustom(point));
+ }
}
diff --git a/src/Avalonia.Visuals/Rendering/IRenderer.cs b/src/Avalonia.Visuals/Rendering/IRenderer.cs
index 9ad7186dca..7dc2dfde88 100644
--- a/src/Avalonia.Visuals/Rendering/IRenderer.cs
+++ b/src/Avalonia.Visuals/Rendering/IRenderer.cs
@@ -50,6 +50,18 @@ namespace Avalonia.Rendering
/// The visuals at the specified point, topmost first.
IEnumerable HitTest(Point p, IVisual root, Func filter);
+ ///
+ /// Hit tests a location to find first visual at the specified point.
+ ///
+ /// The point, in client coordinates.
+ /// The root of the subtree to search.
+ ///
+ /// A filter predicate. If the predicate returns false then the visual and all its
+ /// children will be excluded from the results.
+ ///
+ /// The visual at the specified point, topmost first.
+ IVisual HitTestFirst(Point p, IVisual root, Func filter);
+
///
/// Informs the renderer that the z-ordering of a visual's children has changed.
///
diff --git a/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs b/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs
index 5914bcb1fe..aade57b7f0 100644
--- a/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs
+++ b/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs
@@ -164,6 +164,11 @@ namespace Avalonia.Rendering
return HitTest(root, p, filter);
}
+ public IVisual HitTestFirst(Point p, IVisual root, Func filter)
+ {
+ return HitTest(root, p, filter).FirstOrDefault();
+ }
+
///
public void RecalculateChildren(IVisual visual) => AddDirty(visual);
diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs
index 1afc096c98..70355e6833 100644
--- a/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs
+++ b/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs
@@ -2,8 +2,10 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Linq;
+using Avalonia.Collections.Pooled;
using Avalonia.VisualTree;
namespace Avalonia.Rendering.SceneGraph
@@ -128,7 +130,20 @@ namespace Avalonia.Rendering.SceneGraph
public IEnumerable HitTest(Point p, IVisual root, Func filter)
{
var node = FindNode(root);
- return (node != null) ? HitTest(node, p, null, filter) : Enumerable.Empty();
+ return (node != null) ? new HitTestEnumerable(node, filter, p, Root) : Enumerable.Empty();
+ }
+
+ ///
+ /// Gets the visual at a point in the scene.
+ ///
+ /// The point.
+ /// The root of the subtree to search.
+ /// A filter. May be null.
+ /// The visual at the specified point.
+ public IVisual HitTestFirst(Point p, IVisual root, Func filter)
+ {
+ var node = FindNode(root);
+ return (node != null) ? HitTestFirst(node, p, filter) : null;
}
///
@@ -158,38 +173,157 @@ namespace Avalonia.Rendering.SceneGraph
return result;
}
- private IEnumerable HitTest(IVisualNode node, Point p, Rect? clip, Func filter)
+ private IVisual HitTestFirst(IVisualNode root, Point p, Func filter)
{
- if (filter?.Invoke(node.Visual) != false && node.Visual.IsAttachedToVisualTree)
+ using var enumerator = new HitTestEnumerator(root, filter, p, Root);
+
+ enumerator.MoveNext();
+
+ return enumerator.Current;
+ }
+
+ private class HitTestEnumerable : IEnumerable
+ {
+ private readonly IVisualNode _root;
+ private readonly Func _filter;
+ private readonly IVisualNode _sceneRoot;
+ private readonly Point _point;
+
+ public HitTestEnumerable(IVisualNode root, Func filter, Point point, IVisualNode sceneRoot)
{
- var clipped = false;
+ _root = root;
+ _filter = filter;
+ _point = point;
+ _sceneRoot = sceneRoot;
+ }
- if (node.ClipToBounds)
- {
- clip = clip == null ? node.ClipBounds : clip.Value.Intersect(node.ClipBounds);
- clipped = !clip.Value.Contains(p);
- }
+ public IEnumerator GetEnumerator()
+ {
+ return new HitTestEnumerator(_root, _filter, _point, _sceneRoot);
+ }
- if (node.GeometryClip != null)
- {
- var controlPoint = Root.Visual.TranslatePoint(p, node.Visual);
- clipped = !node.GeometryClip.FillContains(controlPoint.Value);
- }
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+ }
+
+ private struct HitTestEnumerator : IEnumerator
+ {
+ private readonly PooledStack _nodeStack;
+ private readonly Func _filter;
+ private readonly IVisualNode _sceneRoot;
+ private IVisual _current;
+ private readonly Point _point;
+
+ public HitTestEnumerator(IVisualNode root, Func filter, Point point, IVisualNode sceneRoot)
+ {
+ _nodeStack = new PooledStack();
+ _nodeStack.Push(new Entry(root, false, null, true));
+
+ _filter = filter;
+ _point = point;
+ _sceneRoot = sceneRoot;
- if (!clipped)
+ _current = null;
+ }
+
+ public bool MoveNext()
+ {
+ while (_nodeStack.Count > 0)
{
- for (var i = node.Children.Count - 1; i >= 0; --i)
+ (var wasVisited, var isRoot, IVisualNode node, Rect? clip) = _nodeStack.Pop();
+
+ if (wasVisited && isRoot)
{
- foreach (var h in HitTest(node.Children[i], p, clip, filter))
+ break;
+ }
+
+ var children = node.Children;
+ int childCount = children.Count;
+
+ if (childCount == 0 || wasVisited)
+ {
+ if ((wasVisited || FilterAndClip(node, ref clip)) && node.HitTest(_point))
{
- yield return h;
+ _current = node.Visual;
+
+ return true;
}
}
+ else if (FilterAndClip(node, ref clip))
+ {
+ _nodeStack.Push(new Entry(node, true, null));
+
+ for (var i = 0; i < childCount; i++)
+ {
+ _nodeStack.Push(new Entry(children[i], false, clip));
+ }
+ }
+ }
+
+ return false;
+ }
+
+ public void Reset()
+ {
+ throw new NotSupportedException();
+ }
+
+ public IVisual Current => _current;
+
+ object IEnumerator.Current => Current;
+
+ public void Dispose()
+ {
+ _nodeStack.Dispose();
+ }
- if (node.HitTest(p))
+ private bool FilterAndClip(IVisualNode node, ref Rect? clip)
+ {
+ if (_filter?.Invoke(node.Visual) != false && node.Visual.IsAttachedToVisualTree)
+ {
+ var clipped = false;
+
+ if (node.ClipToBounds)
{
- yield return node.Visual;
+ clip = clip == null ? node.ClipBounds : clip.Value.Intersect(node.ClipBounds);
+ clipped = !clip.Value.Contains(_point);
}
+
+ if (node.GeometryClip != null)
+ {
+ var controlPoint = _sceneRoot.Visual.TranslatePoint(_point, node.Visual);
+ clipped = !node.GeometryClip.FillContains(controlPoint.Value);
+ }
+
+ return !clipped;
+ }
+
+ return false;
+ }
+
+ private readonly struct Entry
+ {
+ public readonly bool WasVisited;
+ public readonly bool IsRoot;
+ public readonly IVisualNode Node;
+ public readonly Rect? Clip;
+
+ public Entry(IVisualNode node, bool wasVisited, Rect? clip, bool isRoot = false)
+ {
+ Node = node;
+ WasVisited = wasVisited;
+ IsRoot = isRoot;
+ Clip = clip;
+ }
+
+ public void Deconstruct(out bool wasVisited, out bool isRoot, out IVisualNode node, out Rect? clip)
+ {
+ wasVisited = WasVisited;
+ isRoot = IsRoot;
+ node = Node;
+ clip = Clip;
}
}
}
diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
index d2a9e0a673..93d68e5230 100644
--- a/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
+++ b/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
@@ -280,8 +280,13 @@ namespace Avalonia.Rendering.SceneGraph
///
public bool HitTest(Point p)
{
- foreach (var operation in DrawOperations)
+ var drawOperations = DrawOperations;
+ var drawOperationsCount = drawOperations.Count;
+
+ for (var i = 0; i < drawOperationsCount; i++)
{
+ var operation = drawOperations[i];
+
if (operation?.Item?.HitTest(p) == true)
{
return true;
diff --git a/src/Avalonia.Visuals/VisualTree/VisualExtensions.cs b/src/Avalonia.Visuals/VisualTree/VisualExtensions.cs
index 8e3c7e0765..f32d67db1e 100644
--- a/src/Avalonia.Visuals/VisualTree/VisualExtensions.cs
+++ b/src/Avalonia.Visuals/VisualTree/VisualExtensions.cs
@@ -213,12 +213,37 @@ namespace Avalonia.VisualTree
///
/// The root visual to test.
/// The point.
- /// The visuals at the requested point.
+ /// The visual at the requested point.
public static IVisual GetVisualAt(this IVisual visual, Point p)
{
Contract.Requires(visual != null);
- return visual.GetVisualsAt(p).FirstOrDefault();
+ return visual.GetVisualAt(p, x => x.IsVisible);
+ }
+
+ ///
+ /// Gets the first visual in the visual tree whose bounds contain a point.
+ ///
+ /// The root visual to test.
+ /// The point.
+ ///
+ /// A filter predicate. If the predicate returns false then the visual and all its
+ /// children will be excluded from the results.
+ ///
+ /// The visual at the requested point.
+ public static IVisual GetVisualAt(this IVisual visual, Point p, Func filter)
+ {
+ Contract.Requires(visual != null);
+
+ var root = visual.GetVisualRoot();
+ var rootPoint = visual.TranslatePoint(p, root);
+
+ if (rootPoint.HasValue)
+ {
+ return root.Renderer.HitTestFirst(rootPoint.Value, visual, filter);
+ }
+
+ return null;
}
///
diff --git a/src/Windows/Avalonia.Win32/Input/KeyInterop.cs b/src/Windows/Avalonia.Win32/Input/KeyInterop.cs
index 11c0a6dca9..b38c09c07a 100644
--- a/src/Windows/Avalonia.Win32/Input/KeyInterop.cs
+++ b/src/Windows/Avalonia.Win32/Input/KeyInterop.cs
@@ -2,7 +2,6 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System.Collections.Generic;
-using System.Text;
using Avalonia.Input;
using Avalonia.Win32.Interop;
@@ -211,7 +210,7 @@ namespace Avalonia.Win32.Input
{ 31, Key.ImeModeChange },
{ 32, Key.Space },
{ 33, Key.PageUp },
- { 34, Key.Next },
+ { 34, Key.PageDown },
{ 35, Key.End },
{ 36, Key.Home },
{ 37, Key.Left },
@@ -364,17 +363,80 @@ namespace Avalonia.Win32.Input
{ 254, Key.OemClear },
};
- public static Key KeyFromVirtualKey(int virtualKey)
+ ///
+ /// Indicates whether the key is an extended key, such as the right-hand ALT and CTRL keys.
+ /// According to https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-keydown.
+ ///
+ private static bool IsExtended(int keyData)
{
- Key result;
- s_keyFromVirtualKey.TryGetValue(virtualKey, out result);
+ const int extendedMask = 1 << 24;
+
+ return (keyData & extendedMask) != 0;
+ }
+
+ private static int GetVirtualKey(int virtualKey, int keyData)
+ {
+ // Adapted from https://github.com/dotnet/wpf/blob/master/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndKeyboardInputProvider.cs.
+
+ if (virtualKey == (int)UnmanagedMethods.VirtualKeyStates.VK_SHIFT)
+ {
+ // Bits from 16 to 23 represent scan code.
+ const int scanCodeMask = 0xFF0000;
+
+ var scanCode = (keyData & scanCodeMask) >> 16;
+
+ virtualKey = (int)UnmanagedMethods.MapVirtualKey((uint)scanCode, (uint)UnmanagedMethods.MapVirtualKeyMapTypes.MAPVK_VSC_TO_VK_EX);
+
+ if (virtualKey == 0)
+ {
+ virtualKey = (int)UnmanagedMethods.VirtualKeyStates.VK_LSHIFT;
+ }
+ }
+
+ if (virtualKey == (int)UnmanagedMethods.VirtualKeyStates.VK_MENU)
+ {
+ bool isRight = IsExtended(keyData);
+
+ if (isRight)
+ {
+ virtualKey = (int)UnmanagedMethods.VirtualKeyStates.VK_RMENU;
+ }
+ else
+ {
+ virtualKey = (int)UnmanagedMethods.VirtualKeyStates.VK_LMENU;
+ }
+ }
+
+ if (virtualKey == (int)UnmanagedMethods.VirtualKeyStates.VK_CONTROL)
+ {
+ bool isRight = IsExtended(keyData);
+
+ if (isRight)
+ {
+ virtualKey = (int)UnmanagedMethods.VirtualKeyStates.VK_RCONTROL;
+ }
+ else
+ {
+ virtualKey = (int)UnmanagedMethods.VirtualKeyStates.VK_LCONTROL;
+ }
+ }
+
+ return virtualKey;
+ }
+
+ public static Key KeyFromVirtualKey(int virtualKey, int keyData)
+ {
+ virtualKey = GetVirtualKey(virtualKey, keyData);
+
+ s_keyFromVirtualKey.TryGetValue(virtualKey, out var result);
+
return result;
}
public static int VirtualKeyFromKey(Key key)
{
- int result;
- s_virtualKeyFromKey.TryGetValue(key, out result);
+ s_virtualKeyFromKey.TryGetValue(key, out var result);
+
return result;
}
}
diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
index ed32382760..904e122382 100644
--- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
+++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
@@ -241,6 +241,170 @@ namespace Avalonia.Win32.Interop
MK_XBUTTON2 = 0x0040
}
+ public enum VirtualKeyStates : int
+ {
+ VK_LBUTTON = 0x01,
+ VK_RBUTTON = 0x02,
+ VK_CANCEL = 0x03,
+ VK_MBUTTON = 0x04,
+ VK_XBUTTON1 = 0x05,
+ VK_XBUTTON2 = 0x06,
+ VK_BACK = 0x08,
+ VK_TAB = 0x09,
+ VK_CLEAR = 0x0C,
+ VK_RETURN = 0x0D,
+ VK_SHIFT = 0x10,
+ VK_CONTROL = 0x11,
+ VK_MENU = 0x12,
+ VK_PAUSE = 0x13,
+ VK_CAPITAL = 0x14,
+ VK_KANA = 0x15,
+ VK_HANGEUL = 0x15,
+ VK_HANGUL = 0x15,
+ VK_JUNJA = 0x17,
+ VK_FINAL = 0x18,
+ VK_HANJA = 0x19,
+ VK_KANJI = 0x19,
+ VK_ESCAPE = 0x1B,
+ VK_CONVERT = 0x1C,
+ VK_NONCONVERT = 0x1D,
+ VK_ACCEPT = 0x1E,
+ VK_MODECHANGE = 0x1F,
+ VK_SPACE = 0x20,
+ VK_PRIOR = 0x21,
+ VK_NEXT = 0x22,
+ VK_END = 0x23,
+ VK_HOME = 0x24,
+ VK_LEFT = 0x25,
+ VK_UP = 0x26,
+ VK_RIGHT = 0x27,
+ VK_DOWN = 0x28,
+ VK_SELECT = 0x29,
+ VK_PRINT = 0x2A,
+ VK_EXECUTE = 0x2B,
+ VK_SNAPSHOT = 0x2C,
+ VK_INSERT = 0x2D,
+ VK_DELETE = 0x2E,
+ VK_HELP = 0x2F,
+ VK_LWIN = 0x5B,
+ VK_RWIN = 0x5C,
+ VK_APPS = 0x5D,
+ VK_SLEEP = 0x5F,
+ VK_NUMPAD0 = 0x60,
+ VK_NUMPAD1 = 0x61,
+ VK_NUMPAD2 = 0x62,
+ VK_NUMPAD3 = 0x63,
+ VK_NUMPAD4 = 0x64,
+ VK_NUMPAD5 = 0x65,
+ VK_NUMPAD6 = 0x66,
+ VK_NUMPAD7 = 0x67,
+ VK_NUMPAD8 = 0x68,
+ VK_NUMPAD9 = 0x69,
+ VK_MULTIPLY = 0x6A,
+ VK_ADD = 0x6B,
+ VK_SEPARATOR = 0x6C,
+ VK_SUBTRACT = 0x6D,
+ VK_DECIMAL = 0x6E,
+ VK_DIVIDE = 0x6F,
+ VK_F1 = 0x70,
+ VK_F2 = 0x71,
+ VK_F3 = 0x72,
+ VK_F4 = 0x73,
+ VK_F5 = 0x74,
+ VK_F6 = 0x75,
+ VK_F7 = 0x76,
+ VK_F8 = 0x77,
+ VK_F9 = 0x78,
+ VK_F10 = 0x79,
+ VK_F11 = 0x7A,
+ VK_F12 = 0x7B,
+ VK_F13 = 0x7C,
+ VK_F14 = 0x7D,
+ VK_F15 = 0x7E,
+ VK_F16 = 0x7F,
+ VK_F17 = 0x80,
+ VK_F18 = 0x81,
+ VK_F19 = 0x82,
+ VK_F20 = 0x83,
+ VK_F21 = 0x84,
+ VK_F22 = 0x85,
+ VK_F23 = 0x86,
+ VK_F24 = 0x87,
+ VK_NUMLOCK = 0x90,
+ VK_SCROLL = 0x91,
+ VK_OEM_NEC_EQUAL = 0x92,
+ VK_OEM_FJ_JISHO = 0x92,
+ VK_OEM_FJ_MASSHOU = 0x93,
+ VK_OEM_FJ_TOUROKU = 0x94,
+ VK_OEM_FJ_LOYA = 0x95,
+ VK_OEM_FJ_ROYA = 0x96,
+ VK_LSHIFT = 0xA0,
+ VK_RSHIFT = 0xA1,
+ VK_LCONTROL = 0xA2,
+ VK_RCONTROL = 0xA3,
+ VK_LMENU = 0xA4,
+ VK_RMENU = 0xA5,
+ VK_BROWSER_BACK = 0xA6,
+ VK_BROWSER_FORWARD = 0xA7,
+ VK_BROWSER_REFRESH = 0xA8,
+ VK_BROWSER_STOP = 0xA9,
+ VK_BROWSER_SEARCH = 0xAA,
+ VK_BROWSER_FAVORITES = 0xAB,
+ VK_BROWSER_HOME = 0xAC,
+ VK_VOLUME_MUTE = 0xAD,
+ VK_VOLUME_DOWN = 0xAE,
+ VK_VOLUME_UP = 0xAF,
+ VK_MEDIA_NEXT_TRACK = 0xB0,
+ VK_MEDIA_PREV_TRACK = 0xB1,
+ VK_MEDIA_STOP = 0xB2,
+ VK_MEDIA_PLAY_PAUSE = 0xB3,
+ VK_LAUNCH_MAIL = 0xB4,
+ VK_LAUNCH_MEDIA_SELECT = 0xB5,
+ VK_LAUNCH_APP1 = 0xB6,
+ VK_LAUNCH_APP2 = 0xB7,
+ VK_OEM_1 = 0xBA,
+ VK_OEM_PLUS = 0xBB,
+ VK_OEM_COMMA = 0xBC,
+ VK_OEM_MINUS = 0xBD,
+ VK_OEM_PERIOD = 0xBE,
+ VK_OEM_2 = 0xBF,
+ VK_OEM_3 = 0xC0,
+ VK_OEM_4 = 0xDB,
+ VK_OEM_5 = 0xDC,
+ VK_OEM_6 = 0xDD,
+ VK_OEM_7 = 0xDE,
+ VK_OEM_8 = 0xDF,
+ VK_OEM_AX = 0xE1,
+ VK_OEM_102 = 0xE2,
+ VK_ICO_HELP = 0xE3,
+ VK_ICO_00 = 0xE4,
+ VK_PROCESSKEY = 0xE5,
+ VK_ICO_CLEAR = 0xE6,
+ VK_PACKET = 0xE7,
+ VK_OEM_RESET = 0xE9,
+ VK_OEM_JUMP = 0xEA,
+ VK_OEM_PA1 = 0xEB,
+ VK_OEM_PA2 = 0xEC,
+ VK_OEM_PA3 = 0xED,
+ VK_OEM_WSCTRL = 0xEE,
+ VK_OEM_CUSEL = 0xEF,
+ VK_OEM_ATTN = 0xF0,
+ VK_OEM_FINISH = 0xF1,
+ VK_OEM_COPY = 0xF2,
+ VK_OEM_AUTO = 0xF3,
+ VK_OEM_ENLW = 0xF4,
+ VK_OEM_BACKTAB = 0xF5,
+ VK_ATTN = 0xF6,
+ VK_CRSEL = 0xF7,
+ VK_EXSEL = 0xF8,
+ VK_EREOF = 0xF9,
+ VK_PLAY = 0xFA,
+ VK_ZOOM = 0xFB,
+ VK_NONAME = 0xFC,
+ VK_PA1 = 0xFD,
+ VK_OEM_CLEAR = 0xFE
+ }
+
public enum WindowActivate
{
WA_INACTIVE,
@@ -581,6 +745,14 @@ namespace Avalonia.Win32.Interop
WM_DISPATCH_WORK_ITEM = WM_USER,
}
+ public enum MapVirtualKeyMapTypes : uint
+ {
+ MAPVK_VK_TO_VSC = 0x00,
+ MAPVK_VSC_TO_VK = 0x01,
+ MAPVK_VK_TO_CHAR = 0x02,
+ MAPVK_VSC_TO_VK_EX = 0x03,
+ }
+
public enum BitmapCompressionMode : uint
{
BI_RGB = 0,
@@ -756,6 +928,9 @@ namespace Avalonia.Win32.Interop
[DllImport("user32.dll")]
public static extern bool GetKeyboardState(byte[] lpKeyState);
+ [DllImport("user32.dll", EntryPoint = "MapVirtualKeyW")]
+ public static extern uint MapVirtualKey(uint uCode, uint uMapType);
+
[DllImport("user32.dll", EntryPoint = "GetMessageW")]
public static extern sbyte GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs
index 0f5db58dfe..c16b76b539 100644
--- a/src/Windows/Avalonia.Win32/WindowImpl.cs
+++ b/src/Windows/Avalonia.Win32/WindowImpl.cs
@@ -516,7 +516,7 @@ namespace Avalonia.Win32
timestamp,
_owner,
RawKeyEventType.KeyDown,
- KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
+ KeyInterop.KeyFromVirtualKey(ToInt32(wParam), ToInt32(lParam)), WindowsKeyboardDevice.Instance.Modifiers);
break;
case UnmanagedMethods.WindowsMessage.WM_MENUCHAR:
@@ -530,7 +530,7 @@ namespace Avalonia.Win32
timestamp,
_owner,
RawKeyEventType.KeyUp,
- KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
+ KeyInterop.KeyFromVirtualKey(ToInt32(wParam), ToInt32(lParam)), WindowsKeyboardDevice.Instance.Modifiers);
break;
case UnmanagedMethods.WindowsMessage.WM_CHAR:
// Ignore control chars
@@ -795,9 +795,9 @@ namespace Avalonia.Win32
modifiers |= RawInputModifiers.RightMouseButton;
if (keys.HasFlagCustom(UnmanagedMethods.ModifierKeys.MK_MBUTTON))
modifiers |= RawInputModifiers.MiddleMouseButton;
- if (keys.HasFlag(UnmanagedMethods.ModifierKeys.MK_XBUTTON1))
+ if (keys.HasFlagCustom(UnmanagedMethods.ModifierKeys.MK_XBUTTON1))
modifiers |= RawInputModifiers.XButton1MouseButton;
- if (keys.HasFlag(UnmanagedMethods.ModifierKeys.MK_XBUTTON2))
+ if (keys.HasFlagCustom(UnmanagedMethods.ModifierKeys.MK_XBUTTON2))
modifiers |= RawInputModifiers.XButton2MouseButton;
return modifiers;
}
diff --git a/tests/Avalonia.Benchmarks/NullRenderer.cs b/tests/Avalonia.Benchmarks/NullRenderer.cs
index 7167eafc87..9a756aaf0b 100644
--- a/tests/Avalonia.Benchmarks/NullRenderer.cs
+++ b/tests/Avalonia.Benchmarks/NullRenderer.cs
@@ -21,6 +21,8 @@ namespace Avalonia.Benchmarks
public IEnumerable HitTest(Point p, IVisual root, Func filter) => null;
+ public IVisual HitTestFirst(Point p, IVisual root, Func filter) => null;
+
public void Paint(Rect rect)
{
}
diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs
index d819581000..696c0dbf46 100644
--- a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs
@@ -129,6 +129,23 @@ namespace Avalonia.Controls.UnitTests.Primitives
Assert.Equal(-1, target.SelectedIndex);
}
+ [Fact]
+ public void SelectedIndex_Should_Be_Minus_1_Without_Initialize()
+ {
+ var items = new[]
+ {
+ new Item(),
+ new Item(),
+ };
+
+ var target = new ListBox();
+ target.Items = items;
+ target.Template = Template();
+ target.DataContext = new object();
+
+ Assert.Equal(-1, target.SelectedIndex);
+ }
+
[Fact]
public void SelectedIndex_Should_Be_0_After_Initialize_With_AlwaysSelected()
{
diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/ToggleButtonTests.cs b/tests/Avalonia.Controls.UnitTests/Primitives/ToggleButtonTests.cs
index 4f4ab47b0a..9acd42aba6 100644
--- a/tests/Avalonia.Controls.UnitTests/Primitives/ToggleButtonTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/Primitives/ToggleButtonTests.cs
@@ -1,5 +1,4 @@
using Avalonia.Data;
-using Avalonia.Markup.Data;
using Avalonia.UnitTests;
using Xunit;
@@ -63,6 +62,54 @@ namespace Avalonia.Controls.Primitives.UnitTests
Assert.Null(threeStateButton.IsChecked);
}
+ [Fact]
+ public void ToggleButton_Events_Are_Raised_On_Is_Checked_Changes()
+ {
+ var threeStateButton = new ToggleButton();
+
+ bool checkedRaised = false;
+ threeStateButton.Checked += (_, __) => checkedRaised = true;
+
+ threeStateButton.IsChecked = true;
+ Assert.True(checkedRaised);
+
+ bool uncheckedRaised = false;
+ threeStateButton.Unchecked += (_, __) => uncheckedRaised = true;
+
+ threeStateButton.IsChecked = false;
+ Assert.True(uncheckedRaised);
+
+ bool indeterminateRaised = false;
+ threeStateButton.Indeterminate += (_, __) => indeterminateRaised = true;
+
+ threeStateButton.IsChecked = null;
+ Assert.True(indeterminateRaised);
+ }
+
+ [Fact]
+ public void ToggleButton_Events_Are_Raised_When_Toggling()
+ {
+ var threeStateButton = new TestToggleButton { IsThreeState = true };
+
+ bool checkedRaised = false;
+ threeStateButton.Checked += (_, __) => checkedRaised = true;
+
+ threeStateButton.Toggle();
+ Assert.True(checkedRaised);
+
+ bool indeterminateRaised = false;
+ threeStateButton.Indeterminate += (_, __) => indeterminateRaised = true;
+
+ threeStateButton.Toggle();
+ Assert.True(indeterminateRaised);
+
+ bool uncheckedRaised = false;
+ threeStateButton.Unchecked += (_, __) => uncheckedRaised = true;
+
+ threeStateButton.Toggle();
+ Assert.True(uncheckedRaised);
+ }
+
private class Class1 : NotifyingBase
{
private bool _foo;
@@ -80,5 +127,10 @@ namespace Avalonia.Controls.Primitives.UnitTests
set { nullableFoo = value; RaisePropertyChanged(); }
}
}
+
+ private class TestToggleButton : ToggleButton
+ {
+ public new void Toggle() => base.Toggle();
+ }
}
}
diff --git a/tests/Avalonia.Input.UnitTests/MouseDeviceTests.cs b/tests/Avalonia.Input.UnitTests/MouseDeviceTests.cs
index 214aead521..68d4e3ada9 100644
--- a/tests/Avalonia.Input.UnitTests/MouseDeviceTests.cs
+++ b/tests/Avalonia.Input.UnitTests/MouseDeviceTests.cs
@@ -238,6 +238,9 @@ namespace Avalonia.Input.UnitTests
{
renderer.Setup(x => x.HitTest(It.IsAny(), It.IsAny(), It.IsAny>()))
.Returns(new[] { hit });
+
+ renderer.Setup(x => x.HitTestFirst(It.IsAny(), It.IsAny(), It.IsAny>()))
+ .Returns(hit);
}
private IDisposable TestApplication(IRenderer renderer)
diff --git a/tests/Avalonia.LeakTests/ControlTests.cs b/tests/Avalonia.LeakTests/ControlTests.cs
index 1da4746516..389b3c8df8 100644
--- a/tests/Avalonia.LeakTests/ControlTests.cs
+++ b/tests/Avalonia.LeakTests/ControlTests.cs
@@ -397,6 +397,8 @@ namespace Avalonia.LeakTests
public IEnumerable HitTest(Point p, IVisual root, Func filter) => null;
+ public IVisual HitTestFirst(Point p, IVisual root, Func filter) => null;
+
public void Paint(Rect rect)
{
}