Menuv2.cs 3.2 KB

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