using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using Avalonia.Collections; using Avalonia.Metadata; namespace Avalonia.Controls { public partial class NativeMenu : AvaloniaObject, IEnumerable, INativeMenuExporterEventsImplBridge { private readonly AvaloniaList _items = new AvaloniaList { ResetBehavior = ResetBehavior.Remove }; private NativeMenuItem _parent; [Content] public IList Items => _items; /// /// Raised when the menu requests an update. /// /// /// Use this event to add, remove or modify menu items before a menu is /// shown or a hotkey is pressed. /// public event EventHandler NeedsUpdate; /// /// Raised before the menu is opened. /// /// /// Do not update the menu in this event; use . /// public event EventHandler Opening; /// /// Raised after the menu is closed. /// /// /// Do not update the menu in this event; use . /// public event EventHandler Closed; public NativeMenu() { _items.Validate = Validator; _items.CollectionChanged += ItemsChanged; } void INativeMenuExporterEventsImplBridge.RaiseNeedsUpdate() { NeedsUpdate?.Invoke(this, EventArgs.Empty); } void INativeMenuExporterEventsImplBridge.RaiseOpening() { Opening?.Invoke(this, EventArgs.Empty); } void INativeMenuExporterEventsImplBridge.RaiseClosed() { Closed?.Invoke(this, EventArgs.Empty); } private void Validator(NativeMenuItemBase obj) { if (obj.Parent != null) throw new InvalidOperationException("NativeMenuItem already has a parent"); } private void ItemsChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.OldItems != null) foreach (NativeMenuItemBase i in e.OldItems) i.Parent = null; if (e.NewItems != null) foreach (NativeMenuItemBase i in e.NewItems) i.Parent = this; } public static readonly DirectProperty ParentProperty = AvaloniaProperty.RegisterDirect("Parent", o => o.Parent, (o, v) => o.Parent = v); public NativeMenuItem Parent { get => _parent; set => SetAndRaise(ParentProperty, ref _parent, value); } public void Add(NativeMenuItemBase item) => _items.Add(item); public IEnumerator GetEnumerator() => _items.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }