CatStation/Content.Client/UserInterface/Controls/RadialMenuOptionComparer.cs
Whatstone eed6144042
Radial Sorting: add explicit order field (#45445)
* allow for manual radial ordering

* use order, not i

* one third of a radial menu test

* add a unit test for the radial menu comparer

* Fildrance's suggestions, remove unused compare fn

Co-authored-by: Fildrance <14752842+Fildrance@users.noreply.github.com>

* comment order, return SortOptions to private

I was planning to use this in an integration test, but settled on a unit
test.

* comments!

---------

Co-authored-by: Fildrance <14752842+Fildrance@users.noreply.github.com>
2026-08-23 20:08:30 +00:00

46 lines
1.3 KiB
C#

namespace Content.Client.UserInterface.Controls;
/// <summary>
/// A class to compare radial menu options.
/// Orders options in ascending order (null last!), then alphabetical tooltip (null last!).
/// </summary>
public sealed class RadialMenuOptionComparer : IComparer<RadialMenuOptionBase>
{
/// <summary/>
/// Compares two RadialMenuOptionBase.
/// <seealso cref="IComparer{RadialMenuOptionBase}.Compare(RadialMenuOptionBase?, RadialMenuOptionBase?)"/>
/// </summary>
public int Compare(RadialMenuOptionBase? x, RadialMenuOptionBase? y)
{
if (ReferenceEquals(x, y))
return 0;
if (y == null)
return -1;
if (x == null)
return 1;
// First sort by order, then by tooltip.
// Any non-null order value comes before null.
if (y.Order != x.Order)
{
if (y?.Order is null)
return -1;
if (x?.Order is null)
return 1;
return x.Order < y.Order ? -1 : 1;
}
// Sort by tooltip: non-null order values come before null ones.
if (y?.ToolTip is null)
return -1;
if (x?.ToolTip is null)
return 1;
return string.Compare(x.ToolTip, y.ToolTip, StringComparison.Ordinal);
}
}