Files
MPF/MPF.Frontend/ComboBoxItems/Element.cs

69 lines
1.9 KiB
C#
Raw Permalink Normal View History

2021-03-02 09:08:56 -08:00
using System;
using System.Collections.Generic;
using System.Linq;
2024-05-23 15:40:12 -04:00
namespace MPF.Frontend.ComboBoxItems
2021-03-02 09:08:56 -08:00
{
/// <summary>
/// A generic combo box element
/// </summary>
/// <typeparam name="T">Enum type representing the possible values</typeparam>
2023-10-07 22:40:51 -04:00
public class Element<T> : IEquatable<Element<T>>, IElement where T : struct, Enum
2021-03-02 09:08:56 -08:00
{
private readonly T Data;
public Element(T data) => Data = data;
2021-03-02 09:08:56 -08:00
/// <summary>
/// Allow elements to be used as their internal enum type
/// </summary>
/// <param name="item"></param>
2021-03-06 21:49:33 -08:00
public static implicit operator T? (Element<T> item) => item?.Data;
2021-03-02 09:08:56 -08:00
/// <inheritdoc/>
2024-05-23 13:29:31 -04:00
public string Name => EnumExtensions.GetLongName(Data);
2021-03-02 09:08:56 -08:00
2021-03-09 16:38:15 -08:00
public override string ToString() => Name;
2021-03-02 09:08:56 -08:00
/// <summary>
/// Internal enum value
/// </summary>
public T Value => Data;
/// <summary>
/// Determine if the item is selected or not
/// </summary>
/// <remarks>Only applies to CheckBox type</remarks>
public bool IsChecked { get; set; }
/// <summary>
/// Generate all elements associated with the data enum type
2021-03-02 09:08:56 -08:00
/// </summary>
/// <returns></returns>
public static IEnumerable<Element<T>> GenerateElements()
{
return Enum.GetValues(typeof(T))
.OfType<T>()
.Select(e => new Element<T>(e));
}
2023-10-07 22:40:51 -04:00
2023-11-06 23:06:11 -05:00
/// <inheritdoc/>
public override bool Equals(object? obj)
{
return Equals(obj as Element<T>);
}
2023-10-07 22:40:51 -04:00
/// <inheritdoc/>
public bool Equals(Element<T>? other)
{
if (other == null)
return false;
return Name == other.Name;
}
2023-11-06 23:06:11 -05:00
/// <inheritdoc/>
public override int GetHashCode() => base.GetHashCode();
2021-03-02 09:08:56 -08:00
}
}