1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-05 11:42:55 +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> /// </remarks>
[MeansImplicitUse] [MeansImplicitUse]
[AttributeUsage(AttributeTargets.Property)] [AttributeUsage(AttributeTargets.Property)]
public class SettingSourceAttribute : Attribute public class SettingSourceAttribute : Attribute, IComparable<SettingSourceAttribute>
{ {
public LocalisableString Label { get; } public LocalisableString Label { get; }
@ -42,6 +42,21 @@ namespace osu.Game.Configuration
{ {
OrderPosition = orderPosition; 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 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 original = obj.GetSettingsSourceProperties().ToArray();
var orderedRelative = original return original
.Where(attr => attr.Item1.OrderPosition != null) .OrderBy(attr => attr.Item1)
.OrderBy(attr => attr.Item1.OrderPosition) .ToArray();
.ToArray();
var unordered = original.Except(orderedRelative);
return orderedRelative.Concat(unordered);
} }
} }
} }