RoutedEventRegistry.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Avalonia.Interactivity
  6. {
  7. /// <summary>
  8. /// Tracks registered <see cref="RoutedEvent"/>s.
  9. /// </summary>
  10. public class RoutedEventRegistry
  11. {
  12. private readonly Dictionary<Type, List<RoutedEvent>> _registeredRoutedEvents =
  13. new Dictionary<Type, List<RoutedEvent>>();
  14. /// <summary>
  15. /// Gets the <see cref="RoutedEventRegistry"/> instance.
  16. /// </summary>
  17. public static RoutedEventRegistry Instance { get; }
  18. = new RoutedEventRegistry();
  19. /// <summary>
  20. /// Registers a <see cref="RoutedEvent"/> on a type.
  21. /// </summary>
  22. /// <param name="type">The type.</param>
  23. /// <param name="event">The event.</param>
  24. /// <remarks>
  25. /// You won't usually want to call this method directly, instead use the
  26. /// <see cref="RoutedEvent.Register{TOwner, TEventArgs}(string, RoutingStrategies)"/>
  27. /// method.
  28. /// </remarks>
  29. public void Register(Type type, RoutedEvent @event)
  30. {
  31. type = type ?? throw new ArgumentNullException(nameof(type));
  32. @event = @event ?? throw new ArgumentNullException(nameof(@event));
  33. if (!_registeredRoutedEvents.TryGetValue(type, out var list))
  34. {
  35. list = new List<RoutedEvent>();
  36. _registeredRoutedEvents.Add(type, list);
  37. }
  38. list.Add(@event);
  39. }
  40. /// <summary>
  41. /// Returns all routed events, that are currently registered in the event registry.
  42. /// </summary>
  43. /// <returns>All routed events, that are currently registered in the event registry.</returns>
  44. public IEnumerable<RoutedEvent> GetAllRegistered()
  45. {
  46. foreach (var events in _registeredRoutedEvents.Values)
  47. {
  48. foreach (var e in events)
  49. {
  50. yield return e;
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// Returns all routed events registered with the provided type.
  56. /// If the type is not found or does not provide any routed events, an empty list is returned.
  57. /// </summary>
  58. /// <param name="type">The type.</param>
  59. /// <returns>All routed events registered with the provided type.</returns>
  60. public IReadOnlyList<RoutedEvent> GetRegistered(Type type)
  61. {
  62. type = type ?? throw new ArgumentNullException(nameof(type));
  63. if (_registeredRoutedEvents.TryGetValue(type, out var events))
  64. {
  65. return events;
  66. }
  67. return Array.Empty<RoutedEvent>();
  68. }
  69. /// <summary>
  70. /// Returns all routed events registered with the provided type.
  71. /// If the type is not found or does not provide any routed events, an empty list is returned.
  72. /// </summary>
  73. /// <typeparam name="TOwner">The type.</typeparam>
  74. /// <returns>All routed events registered with the provided type.</returns>
  75. public IReadOnlyList<RoutedEvent> GetRegistered<TOwner>()
  76. {
  77. return GetRegistered(typeof(TOwner));
  78. }
  79. }
  80. }