1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 12:17:26 +08:00

Make SettingSourceAttribute implement IComparable

This commit is contained in:
Bartłomiej Dach 2021-02-27 15:30:05 +01:00
parent 7b6e53680c
commit 1e56d2cbba

View File

@ -23,7 +23,7 @@ namespace osu.Game.Configuration
/// </remarks>
[MeansImplicitUse]
[AttributeUsage(AttributeTargets.Property)]
public class SettingSourceAttribute : Attribute
public class SettingSourceAttribute : Attribute, IComparable<SettingSourceAttribute>
{
public LocalisableString Label { get; }
@ -42,6 +42,21 @@ namespace osu.Game.Configuration
{
OrderPosition = orderPosition;
}
public int CompareTo(SettingSourceAttribute other)
{
if (OrderPosition == other.OrderPosition)
return 0;
// unordered items come last (are greater than any ordered items).
if (OrderPosition == null)
return 1;
if (other.OrderPosition == null)
return -1;
// ordered items are sorted by the order value.
return OrderPosition.Value.CompareTo(other.OrderPosition);
}
}
public static class SettingSourceExtensions
@ -137,17 +152,13 @@ namespace osu.Game.Configuration
}
}
public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetOrderedSettingsSourceProperties(this object obj)
public static ICollection<(SettingSourceAttribute, PropertyInfo)> GetOrderedSettingsSourceProperties(this object obj)
{
var original = obj.GetSettingsSourceProperties().ToArray();
var orderedRelative = original
.Where(attr => attr.Item1.OrderPosition != null)
.OrderBy(attr => attr.Item1.OrderPosition)
return original
.OrderBy(attr => attr.Item1)
.ToArray();
var unordered = original.Except(orderedRelative);
return orderedRelative.Concat(unordered);
}
}
}