Menuv2.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. Initialized += Menuv2_Initialized;
  18. VisibleChanged += OnVisibleChanged;
  19. }
  20. private void OnVisibleChanged (object sender, EventArgs e)
  21. {
  22. if (Visible)
  23. {
  24. //Application.GrabMouse(this);
  25. }
  26. else
  27. {
  28. if (Application.MouseGrabView == this)
  29. {
  30. //Application.UngrabMouse ();
  31. }
  32. }
  33. }
  34. private void Menuv2_Initialized (object sender, EventArgs e)
  35. {
  36. Border.Thickness = new Thickness (1, 1, 1, 1);
  37. Border.LineStyle = LineStyle.Single;
  38. ColorScheme = Colors.ColorSchemes ["Menu"];
  39. }
  40. // Menuv2 arranges the items horizontally.
  41. // The first item has no left border, the last item has no right border.
  42. // The Shortcuts are configured with the command, help, and key views aligned in reverse order (EndToStart).
  43. /// <inheritdoc />
  44. protected override void OnSubviewLayout (LayoutEventArgs args)
  45. {
  46. for (int index = 0; index < Subviews.Count; index++)
  47. {
  48. View barItem = Subviews [index];
  49. if (!barItem.Visible)
  50. {
  51. continue;
  52. }
  53. }
  54. base.OnSubviewLayout (args);
  55. }
  56. /// <inheritdoc/>
  57. public override View Add (View view)
  58. {
  59. base.Add (view);
  60. if (view is Shortcut shortcut)
  61. {
  62. shortcut.CanFocus = true;
  63. shortcut.Orientation = Orientation.Vertical;
  64. shortcut.HighlightStyle |= HighlightStyle.Hover;
  65. // TODO: not happy about using AlignmentModes for this. Too implied.
  66. // TODO: instead, add a property (a style enum?) to Shortcut to control this
  67. //shortcut.AlignmentModes = AlignmentModes.EndToStart;
  68. shortcut.Accepting += ShortcutOnAccept;
  69. void ShortcutOnAccept (object sender, CommandEventArgs e)
  70. {
  71. if (Arrangement.HasFlag (ViewArrangement.Overlapped) && Visible)
  72. {
  73. Visible = false;
  74. e.Cancel = true;
  75. return;
  76. }
  77. //if (!e.Handled)
  78. //{
  79. // RaiseAcceptEvent ();
  80. //}
  81. }
  82. }
  83. return view;
  84. }
  85. }