Menuv2.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.ElementAt (index);
  49. if (!barItem.Visible)
  50. {
  51. continue;
  52. }
  53. }
  54. base.OnSubViewLayout (args);
  55. }
  56. /// <inheritdoc/>
  57. ///
  58. protected override void OnSubViewAdded (View subView)
  59. {
  60. if (subView 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. }
  84. }