NativeMenu.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using Avalonia.Collections;
  6. using Avalonia.Metadata;
  7. namespace Avalonia.Controls
  8. {
  9. public partial class NativeMenu : AvaloniaObject, IEnumerable<NativeMenuItemBase>, INativeMenuExporterEventsImplBridge
  10. {
  11. private readonly AvaloniaList<NativeMenuItemBase> _items =
  12. new AvaloniaList<NativeMenuItemBase> { ResetBehavior = ResetBehavior.Remove };
  13. private NativeMenuItem _parent;
  14. [Content]
  15. public IList<NativeMenuItemBase> Items => _items;
  16. /// <summary>
  17. /// Raised when the menu requests an update.
  18. /// </summary>
  19. /// <remarks>
  20. /// Use this event to add, remove or modify menu items before a menu is
  21. /// shown or a hotkey is pressed.
  22. /// </remarks>
  23. public event EventHandler<EventArgs> NeedsUpdate;
  24. /// <summary>
  25. /// Raised before the menu is opened.
  26. /// </summary>
  27. /// <remarks>
  28. /// Do not update the menu in this event; use <see cref="NeedsUpdate"/>.
  29. /// </remarks>
  30. public event EventHandler<EventArgs> Opening;
  31. /// <summary>
  32. /// Raised after the menu is closed.
  33. /// </summary>
  34. /// <remarks>
  35. /// Do not update the menu in this event; use <see cref="NeedsUpdate"/>.
  36. /// </remarks>
  37. public event EventHandler<EventArgs> Closed;
  38. public NativeMenu()
  39. {
  40. _items.Validate = Validator;
  41. _items.CollectionChanged += ItemsChanged;
  42. }
  43. void INativeMenuExporterEventsImplBridge.RaiseNeedsUpdate()
  44. {
  45. NeedsUpdate?.Invoke(this, EventArgs.Empty);
  46. }
  47. void INativeMenuExporterEventsImplBridge.RaiseOpening()
  48. {
  49. Opening?.Invoke(this, EventArgs.Empty);
  50. }
  51. void INativeMenuExporterEventsImplBridge.RaiseClosed()
  52. {
  53. Closed?.Invoke(this, EventArgs.Empty);
  54. }
  55. private void Validator(NativeMenuItemBase obj)
  56. {
  57. if (obj.Parent != null)
  58. throw new InvalidOperationException("NativeMenuItem already has a parent");
  59. }
  60. private void ItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
  61. {
  62. if (e.OldItems != null)
  63. foreach (NativeMenuItemBase i in e.OldItems)
  64. i.Parent = null;
  65. if (e.NewItems != null)
  66. foreach (NativeMenuItemBase i in e.NewItems)
  67. i.Parent = this;
  68. }
  69. public static readonly DirectProperty<NativeMenu, NativeMenuItem> ParentProperty =
  70. AvaloniaProperty.RegisterDirect<NativeMenu, NativeMenuItem>("Parent", o => o.Parent, (o, v) => o.Parent = v);
  71. public NativeMenuItem Parent
  72. {
  73. get => _parent;
  74. set => SetAndRaise(ParentProperty, ref _parent, value);
  75. }
  76. public void Add(NativeMenuItemBase item) => _items.Add(item);
  77. public IEnumerator<NativeMenuItemBase> GetEnumerator() => _items.GetEnumerator();
  78. IEnumerator IEnumerable.GetEnumerator()
  79. {
  80. return GetEnumerator();
  81. }
  82. }
  83. }