using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace BizHawk.WinForms.Controls
{
///
/// Functions as a collection of ITrackedRadioButtons.
/// Using this in our custom GroupBoxes circumvents the direct-child restriction on radio buttons.
/// With that gone, we're free to use nested FLPs in our layouts, the cost being the complexity of this class and its related types.
///
/// Elements should have unique Names; breaking this rule is UB, not checked at runtime.
///
public sealed class RadioButtonGroupTracker : List, IRadioButtonReadOnlyTracker
{
/// The selected radio button, or if no button is selected or if the collection is empty.
public ITrackedRadioButton? Selection
{
get
{
if (Count == 0) return null;
foreach (var rb in this) if (rb.Checked) return rb;
return null;
}
}
/// The of the selected radio button, cast to ?, or if no button is selected or if the collection is empty.
public T? GetSelectionTagAs() where T : struct, Enum => (T?) Selection?.Tag;
public void UpdateDeselected(string name)
{
foreach (var rb in this) if (rb.Name != name) rb.UncheckFromTracker();
}
}
}