MenuBarItemv2.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// A <see cref="Shortcut"/>-derived object to be used as items in a <see cref="MenuBarv2"/>.
  5. /// MenuBarItems have a title, a hotkey, and an action to execute on activation.
  6. /// </summary>
  7. public class MenuBarItemv2 : MenuItemv2
  8. {
  9. /// <summary>
  10. /// Creates a new instance of <see cref="MenuBarItemv2"/>.
  11. /// </summary>
  12. public MenuBarItemv2 () : base (null, Command.NotBound) { }
  13. /// <summary>
  14. /// Creates a new instance of <see cref="MenuBarItemv2"/>. Each MenuBarItem typically has a <see cref="PopoverMenu"/>
  15. /// that is
  16. /// shown when the item is selected.
  17. /// </summary>
  18. /// <remarks>
  19. /// </remarks>
  20. /// <param name="targetView">
  21. /// The View that <paramref name="command"/> will be invoked on when user does something that causes the MenuBarItems's
  22. /// Accept event to be raised.
  23. /// </param>
  24. /// <param name="command">
  25. /// The Command to invoke on <paramref name="targetView"/>. The Key <paramref name="targetView"/>
  26. /// has bound to <paramref name="command"/> will be used as <see cref="Key"/>
  27. /// </param>
  28. /// <param name="commandText">The text to display for the command.</param>
  29. /// <param name="popoverMenu">The Popover Menu that will be displayed when this item is selected.</param>
  30. public MenuBarItemv2 (View? targetView, Command command, string? commandText, PopoverMenu? popoverMenu = null)
  31. : base (
  32. targetView,
  33. command,
  34. commandText)
  35. {
  36. TargetView = targetView;
  37. Command = command;
  38. PopoverMenu = popoverMenu;
  39. }
  40. /// <summary>
  41. /// Creates a new instance of <see cref="MenuBarItemv2"/> with the specified <paramref name="popoverMenu"/>. This is a
  42. /// helper for the most common MenuBar use-cases.
  43. /// </summary>
  44. /// <remarks>
  45. /// </remarks>
  46. /// <param name="commandText">The text to display for the command.</param>
  47. /// <param name="popoverMenu">The Popover Menu that will be displayed when this item is selected.</param>
  48. public MenuBarItemv2 (string commandText, PopoverMenu? popoverMenu = null)
  49. : this (
  50. null,
  51. Command.NotBound,
  52. commandText,
  53. popoverMenu)
  54. { }
  55. /// <summary>
  56. /// Creates a new instance of <see cref="MenuBarItemv2"/> with the <paramref name="menuItems"/> automatcialy added to a
  57. /// <see cref="PopoverMenu"/>.
  58. /// This is a helper for the most common MenuBar use-cases.
  59. /// </summary>
  60. /// <remarks>
  61. /// </remarks>
  62. /// <param name="commandText">The text to display for the command.</param>
  63. /// <param name="menuItems">
  64. /// The menu items that will be added to the Popover Menu that will be displayed when this item is
  65. /// selected.
  66. /// </param>
  67. public MenuBarItemv2 (string commandText, IEnumerable<View> menuItems)
  68. : this (
  69. null,
  70. Command.NotBound,
  71. commandText,
  72. new (menuItems))
  73. { }
  74. /// <summary>
  75. /// Do not use this property. MenuBarItem does not support SubMenu. Use <see cref="PopoverMenu"/> instead.
  76. /// </summary>
  77. /// <exception cref="InvalidOperationException"></exception>
  78. public new Menuv2? SubMenu
  79. {
  80. get => null;
  81. set => throw new InvalidOperationException ("MenuBarItem does not support SubMenu. Use PopoverMenu instead.");
  82. }
  83. /// <summary>
  84. /// The Popover Menu that will be displayed when this item is selected.
  85. /// </summary>
  86. public PopoverMenu? PopoverMenu { get; set; }
  87. /// <inheritdoc />
  88. protected override bool OnKeyDownNotHandled (Key key)
  89. {
  90. Logging.Trace ($"{key}");
  91. if (PopoverMenu is { Visible: true } && HotKeyBindings.TryGet (key, out _))
  92. {
  93. // If the user presses the hotkey for a menu item that is already open,
  94. // it should close the menu item (Test: MenuBarItem_HotKey_DeActivates)
  95. if (SuperView is MenuBarv2 { } menuBar)
  96. {
  97. menuBar.HideActiveItem ();
  98. }
  99. return true;
  100. }
  101. return false;
  102. }
  103. /// <inheritdoc/>
  104. protected override void Dispose (bool disposing)
  105. {
  106. if (disposing)
  107. {
  108. PopoverMenu?.Dispose ();
  109. PopoverMenu = null;
  110. }
  111. base.Dispose (disposing);
  112. }
  113. }