MenuBarv2.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Reflection;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// A menu bar is a <see cref="View"/> that snaps to the top of a <see cref="Toplevel"/> displaying set of
  6. /// <see cref="Shortcut"/>s.
  7. /// </summary>
  8. public class MenuBarv2 : Bar
  9. {
  10. /// <inheritdoc/>
  11. public MenuBarv2 () : this ([]) { }
  12. /// <inheritdoc/>
  13. public MenuBarv2 (IEnumerable<Shortcut> shortcuts) : base (shortcuts)
  14. {
  15. Y = 0;
  16. Width = Dim.Fill ();
  17. Height = Dim.Auto (DimAutoStyle.Content, 1);
  18. BorderStyle = LineStyle.Dashed;
  19. ColorScheme = Colors.ColorSchemes ["Menu"];
  20. Orientation = Orientation.Horizontal;
  21. LayoutStarted += MenuBarv2_LayoutStarted;
  22. }
  23. // MenuBarv2 arranges the items horizontally.
  24. // The first item has no left border, the last item has no right border.
  25. // The Shortcuts are configured with the command, help, and key views aligned in reverse order (EndToStart).
  26. private void MenuBarv2_LayoutStarted (object sender, LayoutEventArgs e)
  27. {
  28. var minKeyWidth = 0;
  29. List<Shortcut> shortcuts = Subviews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
  30. foreach (Shortcut shortcut in shortcuts)
  31. {
  32. // Let AutoSize do its thing to get the minimum width of each CommandView and HelpView
  33. //shortcut.CommandView.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
  34. minKeyWidth = int.Max (minKeyWidth, shortcut.KeyView.Text.GetColumns ());
  35. }
  36. View prevBarItem = null;
  37. var maxBarItemWidth = 0;
  38. for (int index = 0; index < Subviews.Count; index++)
  39. {
  40. View barItem = Subviews [index];
  41. if (!barItem.Visible)
  42. {
  43. continue;
  44. }
  45. if (barItem is Shortcut scBarItem)
  46. {
  47. scBarItem.MinimumKeyViewSize = minKeyWidth;
  48. }
  49. if (index == Subviews.Count - 1)
  50. {
  51. barItem.Border.Thickness = new Thickness (0, 0, 0, 0);
  52. }
  53. else
  54. {
  55. barItem.Border.Thickness = new Thickness (0, 0, 0, 0);
  56. }
  57. if (barItem is Shortcut shortcut)
  58. {
  59. // shortcut.Min
  60. // shortcut.Orientation = Orientation.Vertical;
  61. }
  62. maxBarItemWidth = Math.Max (maxBarItemWidth, barItem.Frame.Width);
  63. }
  64. foreach (Shortcut shortcut in shortcuts)
  65. {
  66. shortcut.Width = maxBarItemWidth;
  67. }
  68. }
  69. /// <inheritdoc/>
  70. public override View Add (View view)
  71. {
  72. // Call base first, because otherwise it resets CanFocus to true
  73. base.Add (view);
  74. view.CanFocus = true;
  75. if (view is Shortcut shortcut)
  76. {
  77. shortcut.KeyBindingScope = KeyBindingScope.Application;
  78. // TODO: not happy about using AlignmentModes for this. Too implied.
  79. // TODO: instead, add a property (a style enum?) to Shortcut to control this
  80. //shortcut.AlignmentModes = AlignmentModes.EndToStart;
  81. shortcut.KeyView.Visible = false;
  82. shortcut.HelpView.Visible = false;
  83. }
  84. return view;
  85. }
  86. }