Menuv2.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. protected override void OnSubviewLayout (LayoutEventArgs args)
  44. {
  45. for (int index = 0; index < Subviews.Count; index++)
  46. {
  47. View barItem = Subviews [index];
  48. if (!barItem.Visible)
  49. {
  50. continue;
  51. }
  52. }
  53. base.OnSubviewLayout (args);
  54. }
  55. /// <inheritdoc/>
  56. public override View Add (View view)
  57. {
  58. base.Add (view);
  59. if (view is Shortcut shortcut)
  60. {
  61. shortcut.CanFocus = true;
  62. shortcut.Orientation = Orientation.Vertical;
  63. shortcut.HighlightStyle |= HighlightStyle.Hover;
  64. // TODO: not happy about using AlignmentModes for this. Too implied.
  65. // TODO: instead, add a property (a style enum?) to Shortcut to control this
  66. //shortcut.AlignmentModes = AlignmentModes.EndToStart;
  67. shortcut.Accepting += ShortcutOnAccept;
  68. void ShortcutOnAccept (object sender, CommandEventArgs e)
  69. {
  70. if (Arrangement.HasFlag (ViewArrangement.Overlapped) && Visible)
  71. {
  72. Visible = false;
  73. e.Cancel = true;
  74. return;
  75. }
  76. //if (!e.Handled)
  77. //{
  78. // RaiseAcceptEvent ();
  79. //}
  80. }
  81. }
  82. return view;
  83. }
  84. }