namespace Terminal.Gui.Views;
///
/// Provides a user interface for displaying and selecting a single item from a list of options in a type-safe way.
/// Each option is represented by a checkbox, but only one can be selected at a time.
/// provides a non-type-safe version.
///
public sealed class OptionSelector : OptionSelector where TEnum : struct, Enum
{
///
/// Initializes a new instance of the class.
///
public OptionSelector ()
{
base.Labels = Enum.GetValues ().Select (f => f.ToString ()).ToArray (); ;
}
///
/// Gets or sets the value of the selected option.
///
public new TEnum? Value
{
get => base.Value.HasValue ? (TEnum)Enum.ToObject (typeof (TEnum), base.Value.Value) : (TEnum?)null;
set => base.Value = value.HasValue ? Convert.ToInt32 (value.Value) : null;
}
///
/// Prevents calling the base Values property setter with arbitrary values.
///
public override IReadOnlyList? Values
{
get => base.Values;
set => throw new InvalidOperationException ("Setting Values directly is not allowed.");
}
///
/// Raised when has changed. Provides the new value as ?.
///
public new event EventHandler>? ValueChanged;
///
/// Called when has changed. Raises the generic event.
///
protected override void OnValueChanged (int? value, int? previousValue)
{
base.OnValueChanged (value, previousValue);
TEnum? newValue = value.HasValue ? (TEnum)Enum.ToObject (typeof (TEnum), value.Value) : null;
ValueChanged?.Invoke (this, new (newValue));
}
}