SelectionModelSelectionChangedEventArgs.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Avalonia.Controls.Selection;
  5. #nullable enable
  6. namespace Avalonia.Controls.Selection
  7. {
  8. public abstract class SelectionModelSelectionChangedEventArgs : EventArgs
  9. {
  10. /// <summary>
  11. /// Gets the indexes of the items that were removed from the selection.
  12. /// </summary>
  13. public abstract IReadOnlyList<int> DeselectedIndexes { get; }
  14. /// <summary>
  15. /// Gets the indexes of the items that were added to the selection.
  16. /// </summary>
  17. public abstract IReadOnlyList<int> SelectedIndexes { get; }
  18. /// <summary>
  19. /// Gets the items that were removed from the selection.
  20. /// </summary>
  21. public IReadOnlyList<object?> DeselectedItems => GetUntypedDeselectedItems();
  22. /// <summary>
  23. /// Gets the items that were added to the selection.
  24. /// </summary>
  25. public IReadOnlyList<object?> SelectedItems => GetUntypedSelectedItems();
  26. protected abstract IReadOnlyList<object?> GetUntypedDeselectedItems();
  27. protected abstract IReadOnlyList<object?> GetUntypedSelectedItems();
  28. }
  29. public class SelectionModelSelectionChangedEventArgs<T> : SelectionModelSelectionChangedEventArgs
  30. {
  31. private IReadOnlyList<object?>? _deselectedItems;
  32. private IReadOnlyList<object?>? _selectedItems;
  33. public SelectionModelSelectionChangedEventArgs(
  34. IReadOnlyList<int>? deselectedIndices = null,
  35. IReadOnlyList<int>? selectedIndices = null,
  36. IReadOnlyList<T>? deselectedItems = null,
  37. IReadOnlyList<T>? selectedItems = null)
  38. {
  39. DeselectedIndexes = deselectedIndices ?? Array.Empty<int>();
  40. SelectedIndexes = selectedIndices ?? Array.Empty<int>();
  41. DeselectedItems = deselectedItems ?? Array.Empty<T>();
  42. SelectedItems = selectedItems ?? Array.Empty<T>();
  43. }
  44. /// <summary>
  45. /// Gets the indexes of the items that were removed from the selection.
  46. /// </summary>
  47. public override IReadOnlyList<int> DeselectedIndexes { get; }
  48. /// <summary>
  49. /// Gets the indexes of the items that were added to the selection.
  50. /// </summary>
  51. public override IReadOnlyList<int> SelectedIndexes { get; }
  52. /// <summary>
  53. /// Gets the items that were removed from the selection.
  54. /// </summary>
  55. public new IReadOnlyList<T> DeselectedItems { get; }
  56. /// <summary>
  57. /// Gets the items that were added to the selection.
  58. /// </summary>
  59. public new IReadOnlyList<T> SelectedItems { get; }
  60. protected override IReadOnlyList<object?> GetUntypedDeselectedItems()
  61. {
  62. return _deselectedItems ??= (DeselectedItems as IReadOnlyList<object?>) ??
  63. new SelectedItems<T>.Untyped(DeselectedItems);
  64. }
  65. protected override IReadOnlyList<object?> GetUntypedSelectedItems()
  66. {
  67. return _selectedItems ??= (SelectedItems as IReadOnlyList<object?>) ??
  68. new SelectedItems<T>.Untyped(SelectedItems);
  69. }
  70. }
  71. }