Menuv2.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.ComponentModel;
  3. using System.Reflection;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// </summary>
  7. public class Menuv2 : Bar
  8. {
  9. /// <inheritdoc/>
  10. public Menuv2 () : this ([]) { }
  11. /// <inheritdoc/>
  12. public Menuv2 (IEnumerable<Shortcut> shortcuts) : base (shortcuts)
  13. {
  14. Orientation = Orientation.Vertical;
  15. Width = Dim.Auto ();
  16. Height = Dim.Auto (DimAutoStyle.Content, 1);
  17. ColorScheme = Colors.ColorSchemes ["Menu"];
  18. Initialized += Menuv2_Initialized;
  19. }
  20. private void Menuv2_Initialized (object sender, EventArgs e)
  21. {
  22. Border.Thickness = new Thickness (1, 1, 1, 1);
  23. }
  24. // Menuv2 arranges the items horizontally.
  25. // The first item has no left border, the last item has no right border.
  26. // The Shortcuts are configured with the command, help, and key views aligned in reverse order (EndToStart).
  27. internal override void OnLayoutStarted (LayoutEventArgs args)
  28. {
  29. for (int index = 0; index < Subviews.Count; index++)
  30. {
  31. View barItem = Subviews [index];
  32. if (!barItem.Visible)
  33. {
  34. continue;
  35. }
  36. }
  37. base.OnLayoutStarted (args);
  38. }
  39. /// <inheritdoc/>
  40. public override View Add (View view)
  41. {
  42. base.Add (view);
  43. if (view is Shortcut shortcut)
  44. {
  45. shortcut.CanFocus = true;
  46. shortcut.KeyBindingScope = KeyBindingScope.Application;
  47. shortcut.Orientation = Orientation.Vertical;
  48. shortcut.HighlightStyle |= HighlightStyle.Hover;
  49. // TODO: not happy about using AlignmentModes for this. Too implied.
  50. // TODO: instead, add a property (a style enum?) to Shortcut to control this
  51. //shortcut.AlignmentModes = AlignmentModes.EndToStart;
  52. shortcut.Accept += ShortcutOnAccept;
  53. void ShortcutOnAccept (object sender, HandledEventArgs e)
  54. {
  55. if (Arrangement.HasFlag(ViewArrangement.Overlapped) && Visible)
  56. {
  57. Visible = false;
  58. e.Handled = true;
  59. return;
  60. //Enabled = Visible;
  61. }
  62. if (!e.Handled)
  63. {
  64. OnAccept ();
  65. }
  66. }
  67. }
  68. return view;
  69. }
  70. }