MenuBase.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.Controls.Generators;
  5. using Avalonia.Controls.Platform;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Input;
  8. using Avalonia.Interactivity;
  9. using Avalonia.LogicalTree;
  10. #nullable enable
  11. namespace Avalonia.Controls
  12. {
  13. /// <summary>
  14. /// Base class for menu controls.
  15. /// </summary>
  16. public abstract class MenuBase : SelectingItemsControl, IFocusScope, IMenu
  17. {
  18. /// <summary>
  19. /// Defines the <see cref="IsOpen"/> property.
  20. /// </summary>
  21. public static readonly DirectProperty<Menu, bool> IsOpenProperty =
  22. AvaloniaProperty.RegisterDirect<Menu, bool>(
  23. nameof(IsOpen),
  24. o => o.IsOpen);
  25. /// <summary>
  26. /// Defines the <see cref="MenuOpened"/> event.
  27. /// </summary>
  28. public static readonly RoutedEvent<RoutedEventArgs> MenuOpenedEvent =
  29. RoutedEvent.Register<MenuBase, RoutedEventArgs>(nameof(MenuOpened), RoutingStrategies.Bubble);
  30. /// <summary>
  31. /// Defines the <see cref="MenuClosed"/> event.
  32. /// </summary>
  33. public static readonly RoutedEvent<RoutedEventArgs> MenuClosedEvent =
  34. RoutedEvent.Register<MenuBase, RoutedEventArgs>(nameof(MenuClosed), RoutingStrategies.Bubble);
  35. private bool _isOpen;
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="MenuBase"/> class.
  38. /// </summary>
  39. public MenuBase()
  40. {
  41. InteractionHandler = new DefaultMenuInteractionHandler(false);
  42. }
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="MenuBase"/> class.
  45. /// </summary>
  46. /// <param name="interactionHandler">The menu interaction handler.</param>
  47. public MenuBase(IMenuInteractionHandler interactionHandler)
  48. {
  49. InteractionHandler = interactionHandler ?? throw new ArgumentNullException(nameof(interactionHandler));
  50. }
  51. /// <summary>
  52. /// Initializes static members of the <see cref="MenuBase"/> class.
  53. /// </summary>
  54. static MenuBase()
  55. {
  56. MenuItem.SubmenuOpenedEvent.AddClassHandler<MenuBase>((x, e) => x.OnSubmenuOpened(e));
  57. }
  58. /// <summary>
  59. /// Gets a value indicating whether the menu is open.
  60. /// </summary>
  61. public bool IsOpen
  62. {
  63. get { return _isOpen; }
  64. protected set { SetAndRaise(IsOpenProperty, ref _isOpen, value); }
  65. }
  66. /// <inheritdoc/>
  67. IMenuInteractionHandler IMenu.InteractionHandler => InteractionHandler;
  68. /// <inheritdoc/>
  69. IMenuItem? IMenuElement.SelectedItem
  70. {
  71. get
  72. {
  73. var index = SelectedIndex;
  74. return (index != -1) ?
  75. (IMenuItem)ItemContainerGenerator.ContainerFromIndex(index) :
  76. null;
  77. }
  78. set
  79. {
  80. SelectedIndex = ItemContainerGenerator.IndexFromContainer(value);
  81. }
  82. }
  83. /// <inheritdoc/>
  84. IEnumerable<IMenuItem> IMenuElement.SubItems
  85. {
  86. get
  87. {
  88. return ItemContainerGenerator.Containers
  89. .Select(x => x.ContainerControl)
  90. .OfType<IMenuItem>();
  91. }
  92. }
  93. /// <summary>
  94. /// Gets the interaction handler for the menu.
  95. /// </summary>
  96. protected IMenuInteractionHandler InteractionHandler { get; }
  97. /// <summary>
  98. /// Occurs when a <see cref="Menu"/> is opened.
  99. /// </summary>
  100. public event EventHandler<RoutedEventArgs> MenuOpened
  101. {
  102. add { AddHandler(MenuOpenedEvent, value); }
  103. remove { RemoveHandler(MenuOpenedEvent, value); }
  104. }
  105. /// <summary>
  106. /// Occurs when a <see cref="Menu"/> is closed.
  107. /// </summary>
  108. public event EventHandler<RoutedEventArgs> MenuClosed
  109. {
  110. add { AddHandler(MenuClosedEvent, value); }
  111. remove { RemoveHandler(MenuClosedEvent, value); }
  112. }
  113. /// <summary>
  114. /// Closes the menu.
  115. /// </summary>
  116. public abstract void Close();
  117. /// <summary>
  118. /// Opens the menu.
  119. /// </summary>
  120. public abstract void Open();
  121. /// <inheritdoc/>
  122. bool IMenuElement.MoveSelection(NavigationDirection direction, bool wrap) => MoveSelection(direction, wrap);
  123. /// <inheritdoc/>
  124. protected override IItemContainerGenerator CreateItemContainerGenerator()
  125. {
  126. return new ItemContainerGenerator<MenuItem>(this, MenuItem.HeaderProperty, null);
  127. }
  128. /// <inheritdoc/>
  129. protected override void OnKeyDown(KeyEventArgs e)
  130. {
  131. // Don't handle here: let the interaction handler handle it.
  132. }
  133. /// <inheritdoc/>
  134. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  135. {
  136. base.OnAttachedToVisualTree(e);
  137. InteractionHandler.Attach(this);
  138. }
  139. /// <inheritdoc/>
  140. protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
  141. {
  142. base.OnDetachedFromVisualTree(e);
  143. InteractionHandler.Detach(this);
  144. }
  145. /// <summary>
  146. /// Called when a submenu opens somewhere in the menu.
  147. /// </summary>
  148. /// <param name="e">The event args.</param>
  149. protected virtual void OnSubmenuOpened(RoutedEventArgs e)
  150. {
  151. if (e.Source is MenuItem menuItem && menuItem.Parent == this)
  152. {
  153. foreach (var child in this.GetLogicalChildren().OfType<MenuItem>())
  154. {
  155. if (child != menuItem && child.IsSubMenuOpen)
  156. {
  157. child.IsSubMenuOpen = false;
  158. }
  159. }
  160. }
  161. IsOpen = true;
  162. }
  163. }
  164. }