/* Copyright (C) 2014-2019 de4dot@gmail.com This file is part of dnSpy dnSpy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. dnSpy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with dnSpy. If not, see . */ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace dnSpy.Contracts.MVVM { /// /// Enum value /// public sealed class EnumVM : ViewModelBase { readonly object value; readonly string name; /// /// Gets the value /// public object Value => value; /// /// Gets the name /// public string Name => name; /// /// Constructor /// /// Initial value public EnumVM(object value) { this.value = value; name = Enum.GetName(value.GetType(), value) ?? throw new ArgumentOutOfRangeException(nameof(value)); } /// /// Constructor /// /// Initial value /// Name public EnumVM(object value, string name) { this.value = value; this.name = name; } /// /// Creates an array of s /// /// Type of enum /// Values that will be shown first /// public static EnumVM[] Create(Type enumType, params object[] values) => Create(true, enumType, values); /// /// Creates an array of s /// /// true to sort the array /// Type of enum /// Values that will be shown first /// public static EnumVM[] Create(bool sort, Type enumType, params object[] values) { var list = new List(); foreach (var value in enumType.GetEnumValues()) { Debug2.Assert(value is not null); if (values.Any(a => a.Equals(value))) continue; list.Add(new EnumVM(value)); } if (sort) list.Sort((a, b) => StringComparer.InvariantCultureIgnoreCase.Compare(a.Name, b.Name)); for (int i = 0; i < values.Length; i++) list.Insert(i, new EnumVM(values[i])); return list.ToArray(); } /// /// Gets the name /// /// public override string ToString() => name; } /// /// List of enum values /// public sealed class EnumListVM : ListVM { /// /// Gets the selected item /// public new object? SelectedItem { get { if (Index < 0 || Index >= list.Count) return null; return list[Index].Value; } set { if (value is not null && !object.Equals(SelectedItem, value)) SelectedIndex = GetIndex(value); } } /// /// Constructor /// /// Initial value public EnumListVM(IList list) : this(list, null) { } /// /// Constructor /// /// Initial value /// Called when the selected item gets changed public EnumListVM(IEnumerable list, Action? onChanged) : base(list, onChanged) { } /// /// Checks whether the list contains a value /// /// Value /// public bool Has(object value) { for (int i = 0; i < list.Count; i++) { if (list[i].Value.Equals(value)) return true; } return false; } /// /// Gets the index of the value. If it doesn't exist, it's automatically added to the list /// /// Value /// public int GetIndex(object value) { for (int i = 0; i < list.Count; i++) { if (list[i].Value.Equals(value)) return i; } list.Add(new EnumVM(value, $"0x{value:X}")); return list.Count - 1; } } }