No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenericItemContentProvider.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace UnityEditor.TestTools.TestRunner.GUI.Controls
  5. {
  6. /// <summary>
  7. /// A generic type content provider to be used with the <see cref="SelectionDropDown" /> control.
  8. /// </summary>
  9. /// <typeparam name="T">The type of values represented by content elements.</typeparam>
  10. internal class GenericItemContentProvider<T> : ISelectionDropDownContentProvider where T : IEquatable<T>
  11. {
  12. private readonly ISelectableItem<T>[] m_Items;
  13. private readonly Action<T> m_ValueChangedCallback;
  14. private T m_CurrentValue;
  15. /// <summary>
  16. /// Creates a new instance of the <see cref="GenericItemContentProvider{T}" /> class.
  17. /// </summary>
  18. /// <param name="initialValue">The initial selection value.</param>
  19. /// <param name="items">The set of selectable items.</param>
  20. /// <param name="separatorIndices">The indices of items which should be followed by separator lines.</param>
  21. /// <param name="valueChangedCallback"></param>
  22. /// <exception cref="ArgumentNullException">Thrown if any of the provided arguments is null, except for the separator indices.</exception>
  23. /// <exception cref="ArgumentException">Thrown if the set of items is empty or does not contain the initial selection value.</exception>
  24. public GenericItemContentProvider(T initialValue, ISelectableItem<T>[] items, int[] separatorIndices, Action<T> valueChangedCallback)
  25. {
  26. if (initialValue == null)
  27. {
  28. throw new ArgumentNullException(nameof(initialValue), "The initial selection value must not be null.");
  29. }
  30. if (items == null)
  31. {
  32. throw new ArgumentNullException(nameof(items), "The set of items must not be null.");
  33. }
  34. if (valueChangedCallback == null)
  35. {
  36. throw new ArgumentNullException(nameof(valueChangedCallback), "The value change callback must not be null.");
  37. }
  38. if (items.Length == 0)
  39. {
  40. throw new ArgumentException("The set of items must not be empty.", nameof(items));
  41. }
  42. if (!items.Any(i => i.Value.Equals(initialValue)))
  43. {
  44. throw new ArgumentException("The initial selection value must be in the items set.", nameof(items));
  45. }
  46. m_CurrentValue = initialValue;
  47. m_Items = items;
  48. SeparatorIndices = separatorIndices ?? new int[0];
  49. m_ValueChangedCallback = valueChangedCallback;
  50. }
  51. public int Count => m_Items.Length;
  52. public bool IsMultiSelection => false;
  53. public string GetName(int index)
  54. {
  55. return ValidateIndexBounds(index) ? m_Items[index].DisplayName : string.Empty;
  56. }
  57. public int[] SeparatorIndices { get; }
  58. public void SelectItem(int index)
  59. {
  60. if (!ValidateIndexBounds(index))
  61. {
  62. return;
  63. }
  64. if (IsSelected(index))
  65. {
  66. return;
  67. }
  68. m_CurrentValue = m_Items[index].Value;
  69. m_ValueChangedCallback(m_CurrentValue);
  70. }
  71. public bool IsSelected(int index)
  72. {
  73. return ValidateIndexBounds(index) && m_Items[index].Value.Equals(m_CurrentValue);
  74. }
  75. private bool ValidateIndexBounds(int index)
  76. {
  77. if (index < 0 || index >= Count)
  78. {
  79. Debug.LogError($"Requesting item index {index} from a collection of size {Count}");
  80. return false;
  81. }
  82. return true;
  83. }
  84. }
  85. }