LayoutElement.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.ComponentModel;
  2. using System.Runtime.CompilerServices;
  3. using Avalonia.Controls;
  4. using PixiEditor.Extensions.CommonApi.FlyUI;
  5. using PixiEditor.Extensions.CommonApi.FlyUI.Events;
  6. namespace PixiEditor.Extensions.FlyUI.Elements;
  7. public abstract class LayoutElement : ILayoutElement<Control>, INotifyPropertyChanged
  8. {
  9. public int UniqueId { get; set; }
  10. private Dictionary<string, List<ElementEventHandler>>? _events;
  11. public abstract Control BuildNative();
  12. public void AddEvent(string eventName, ElementEventHandler eventHandler)
  13. {
  14. if (_events == null)
  15. {
  16. _events = new Dictionary<string, List<ElementEventHandler>>();
  17. }
  18. if (!_events.ContainsKey(eventName))
  19. {
  20. _events.Add(eventName, new List<ElementEventHandler>());
  21. }
  22. _events[eventName].Add(eventHandler);
  23. }
  24. public void AddEvent<T>(string eventName, ElementEventHandler<T> eventHandler) where T : ElementEventArgs<T>
  25. {
  26. if (_events == null)
  27. {
  28. _events = new Dictionary<string, List<ElementEventHandler>>();
  29. }
  30. if (!_events.ContainsKey(eventName))
  31. {
  32. _events.Add(eventName, new List<ElementEventHandler>());
  33. }
  34. _events[eventName].Add((args => eventHandler((T)args)));
  35. }
  36. public void RemoveEvent(string eventName, ElementEventHandler eventHandler)
  37. {
  38. if (_events == null)
  39. {
  40. return;
  41. }
  42. if (!_events.ContainsKey(eventName))
  43. {
  44. return;
  45. }
  46. _events[eventName].Remove(eventHandler);
  47. }
  48. public void RemoveEvent<T>(string eventName, ElementEventHandler<T> eventHandler) where T : ElementEventArgs<T>
  49. {
  50. if (_events == null)
  51. {
  52. return;
  53. }
  54. if (!_events.ContainsKey(eventName))
  55. {
  56. return;
  57. }
  58. _events[eventName].Remove((args => eventHandler((T)args)));
  59. }
  60. public void RaiseEvent(string eventName, ElementEventArgs args)
  61. {
  62. if (_events == null)
  63. {
  64. return;
  65. }
  66. if (!_events.ContainsKey(eventName))
  67. {
  68. return;
  69. }
  70. foreach (ElementEventHandler eventHandler in _events[eventName])
  71. {
  72. eventHandler.Invoke(args);
  73. }
  74. }
  75. public event PropertyChangedEventHandler? PropertyChanged;
  76. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  77. {
  78. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  79. }
  80. protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
  81. {
  82. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  83. field = value;
  84. OnPropertyChanged(propertyName);
  85. return true;
  86. }
  87. }