Browse Source

Remove unused internal classes, methods, and comparers (#20746)

- Delete SerialDisposableValue class (zero references across codebase)
- Delete SingleOrDictionary<TKey, TValue> class (zero references across codebase)
- Remove unused CompareRoundingErrors private method from Grid.cs
- Remove unused StarDistributionOrderIndexComparer private class from Grid.cs
- Remove unused DistributionOrderIndexComparer private class from Grid.cs
- Remove unused OnGridVisibilityChanged private method from DatePicker.cs
pull/19232/merge
Evan 4 weeks ago
committed by GitHub
parent
commit
49b509f11e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 35
      src/Avalonia.Base/Reactive/SerialDisposableValue.cs
  2. 144
      src/Avalonia.Base/Utilities/SingleOrDictionary.cs
  3. 2
      src/Avalonia.Controls/DateTimePickers/DatePicker.cs
  4. 101
      src/Avalonia.Controls/Grid.cs

35
src/Avalonia.Base/Reactive/SerialDisposableValue.cs

@ -1,35 +0,0 @@
using System;
using System.Threading;
namespace Avalonia.Reactive;
/// <summary>
/// Represents a disposable resource whose underlying disposable resource can be replaced by another disposable resource, causing automatic disposal of the previous underlying disposable resource.
/// </summary>
internal sealed class SerialDisposableValue : IDisposable
{
private IDisposable? _current;
private bool _disposed;
public IDisposable? Disposable
{
get => _current;
set
{
_current?.Dispose();
_current = value;
if (_disposed)
{
_current?.Dispose();
_current = null;
}
}
}
public void Dispose()
{
_disposed = true;
_current?.Dispose();
}
}

144
src/Avalonia.Base/Utilities/SingleOrDictionary.cs

@ -1,144 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Avalonia.Utilities
{
/// <summary>
/// Stores either a single key value pair or constructs a dictionary when more than one value is stored.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
internal class SingleOrDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
where TKey : notnull
{
private KeyValuePair<TKey, TValue>? _singleValue;
private Dictionary<TKey, TValue>? dictionary;
public void Add(TKey key, TValue value)
{
if (_singleValue != null)
{
dictionary = new Dictionary<TKey, TValue>();
((ICollection<KeyValuePair<TKey, TValue>>)dictionary).Add(_singleValue.Value);
_singleValue = null;
}
if (dictionary != null)
{
dictionary.Add(key, value);
}
else
{
_singleValue = new KeyValuePair<TKey, TValue>(key, value);
}
}
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
{
if (dictionary == null)
{
if (!_singleValue.HasValue || !EqualityComparer<TKey>.Default.Equals(_singleValue.Value.Key, key))
{
value = default;
return false;
}
else
{
value = _singleValue.Value.Value;
return true;
}
}
else
{
return dictionary.TryGetValue(key, out value);
}
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
if (dictionary == null)
{
if (_singleValue.HasValue)
{
return new SingleEnumerator<KeyValuePair<TKey, TValue>>(_singleValue.Value);
}
}
else
{
return dictionary.GetEnumerator();
}
return Enumerable.Empty<KeyValuePair<TKey, TValue>>().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerable<TValue> Values
{
get
{
if(dictionary == null)
{
if (_singleValue.HasValue)
{
return new[] { _singleValue.Value.Value };
}
}
else
{
return dictionary.Values;
}
return Enumerable.Empty<TValue>();
}
}
private class SingleEnumerator<T> : IEnumerator<T>
{
private readonly T value;
private int index = -1;
public SingleEnumerator(T value)
{
this.value = value;
}
public T Current
{
get
{
if (index == 0)
{
return value;
}
else
{
throw new InvalidOperationException();
}
}
}
object? IEnumerator.Current => Current;
public void Dispose()
{
}
public bool MoveNext()
{
index++;
return index < 1;
}
public void Reset()
{
index = -1;
}
}
}
}

2
src/Avalonia.Controls/DateTimePickers/DatePicker.cs

@ -103,8 +103,6 @@ namespace Avalonia.Controls
SetCurrentValue(MaxYearProperty, new DateTimeOffset(now.Date.Year + 100, 12, 31, 0, 0, 0, now.Offset)); SetCurrentValue(MaxYearProperty, new DateTimeOffset(now.Date.Year + 100, 12, 31, 0, 0, 0, now.Offset));
} }
private static void OnGridVisibilityChanged(DatePicker sender, AvaloniaPropertyChangedEventArgs e) => sender.SetGrid();
public string DayFormat public string DayFormat
{ {
get => GetValue(DayFormatProperty); get => GetValue(DayFormatProperty);

101
src/Avalonia.Controls/Grid.cs

@ -2278,25 +2278,6 @@ namespace Avalonia.Controls
return null; return null;
} }
/// <summary>
/// Sorts row/column indices by rounding error if layout rounding is applied.
/// </summary>
/// <param name="x">Index, rounding error pair</param>
/// <param name="y">Index, rounding error pair</param>
/// <returns>1 if x.Value > y.Value, 0 if equal, -1 otherwise</returns>
private static int CompareRoundingErrors(KeyValuePair<int, double> x, KeyValuePair<int, double> y)
{
if (x.Value < y.Value)
{
return -1;
}
else if (x.Value > y.Value)
{
return 1;
}
return 0;
}
/// <summary> /// <summary>
/// Calculates final (aka arrange) size for given range. /// Calculates final (aka arrange) size for given range.
/// </summary> /// </summary>
@ -2985,88 +2966,6 @@ namespace Avalonia.Controls
} }
} }
/// <summary>
/// StarDistributionOrderIndexComparer.
/// </summary>
private class StarDistributionOrderIndexComparer : IComparer
{
private readonly IReadOnlyList<DefinitionBase> definitions;
internal StarDistributionOrderIndexComparer(IReadOnlyList<DefinitionBase> definitions)
{
this.definitions = definitions ?? throw new ArgumentNullException(nameof(definitions));
}
public int Compare(object? x, object? y)
{
int? indexX = x as int?;
int? indexY = y as int?;
DefinitionBase? definitionX = null;
DefinitionBase? definitionY = null;
if (indexX != null)
{
definitionX = definitions[indexX.Value];
}
if (indexY != null)
{
definitionY = definitions[indexY.Value];
}
int result;
if (!CompareNullRefs(definitionX, definitionY, out result))
{
result = definitionX.SizeCache.CompareTo(definitionY.SizeCache);
}
return result;
}
}
/// <summary>
/// DistributionOrderComparer.
/// </summary>
private class DistributionOrderIndexComparer : IComparer
{
private readonly IReadOnlyList<DefinitionBase> definitions;
internal DistributionOrderIndexComparer(IReadOnlyList<DefinitionBase> definitions)
{
this.definitions = definitions ?? throw new ArgumentNullException(nameof(definitions));
}
public int Compare(object? x, object? y)
{
int? indexX = x as int?;
int? indexY = y as int?;
DefinitionBase? definitionX = null;
DefinitionBase? definitionY = null;
if (indexX != null)
{
definitionX = definitions[indexX.Value];
}
if (indexY != null)
{
definitionY = definitions[indexY.Value];
}
int result;
if (!CompareNullRefs(definitionX, definitionY, out result))
{
double xprime = definitionX.SizeCache - definitionX.MinSizeForArrange;
double yprime = definitionY.SizeCache - definitionY.MinSizeForArrange;
result = xprime.CompareTo(yprime);
}
return result;
}
}
/// <summary> /// <summary>
/// RoundingErrorIndexComparer. /// RoundingErrorIndexComparer.
/// </summary> /// </summary>

Loading…
Cancel
Save