Bar.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Provides a horizontally or vertically oriented container for other views to be used as a menu, toolbar, or status bar.
  4. /// </summary>
  5. /// <remarks>
  6. /// </remarks>
  7. public class Bar : View
  8. {
  9. /// <inheritdoc/>
  10. public Bar ()
  11. {
  12. SetInitialProperties ();
  13. }
  14. /// <summary>
  15. /// Gets or sets the <see cref="Orientation"/> for this <see cref="Bar"/>. The default is
  16. /// <see cref="Orientation.Horizontal"/>.
  17. /// </summary>
  18. public Orientation Orientation { get; set; } = Orientation.Horizontal;
  19. public bool StatusBarStyle { get; set; } = true;
  20. public override void Add (View view)
  21. {
  22. if (Orientation == Orientation.Horizontal)
  23. {
  24. //view.AutoSize = true;
  25. }
  26. //if (StatusBarStyle)
  27. //{
  28. // // Light up right border
  29. // view.BorderStyle = LineStyle.Single;
  30. // view.Border.Thickness = new Thickness (0, 0, 1, 0);
  31. //}
  32. //if (view is not Shortcut)
  33. //{
  34. // if (StatusBarStyle)
  35. // {
  36. // view.Padding.Thickness = new Thickness (0, 0, 1, 0);
  37. // }
  38. // view.Margin.Thickness = new Thickness (1, 0, 0, 0);
  39. //}
  40. //view.ColorScheme = ColorScheme;
  41. // Add any HotKey keybindings to our bindings
  42. IEnumerable<KeyValuePair<Key, KeyBinding>> bindings = view.KeyBindings.Bindings.Where (b => b.Value.Scope == KeyBindingScope.HotKey);
  43. foreach (KeyValuePair<Key, KeyBinding> binding in bindings)
  44. {
  45. AddCommand (
  46. binding.Value.Commands [0],
  47. () =>
  48. {
  49. if (view is Shortcut shortcut)
  50. {
  51. return shortcut.CommandView.InvokeCommands (binding.Value.Commands);
  52. }
  53. return false;
  54. });
  55. KeyBindings.Add (binding.Key, binding.Value);
  56. }
  57. base.Add (view);
  58. }
  59. private void Bar_LayoutStarted (object sender, LayoutEventArgs e)
  60. {
  61. View prevBarItem = null;
  62. switch (Orientation)
  63. {
  64. case Orientation.Horizontal:
  65. for (var index = 0; index < Subviews.Count; index++)
  66. {
  67. View barItem = Subviews [index];
  68. if (!barItem.Visible)
  69. {
  70. continue;
  71. }
  72. if (prevBarItem == null)
  73. {
  74. barItem.X = 0;
  75. }
  76. else
  77. {
  78. // Make view to right be autosize
  79. //Subviews [^1].AutoSize = true;
  80. // Align the view to the right of the previous view
  81. barItem.X = Pos.Right (prevBarItem);
  82. }
  83. barItem.Y = Pos.Center ();
  84. barItem.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
  85. prevBarItem = barItem;
  86. }
  87. break;
  88. case Orientation.Vertical:
  89. // CommandView is aligned left, HelpView is aligned right, KeyView is aligned right
  90. // All CommandView's are the same width, all HelpView's are the same width,
  91. // all KeyView's are the same width
  92. int maxCommandWidth = 0;
  93. int maxHelpWidth = 0;
  94. List<Shortcut> shortcuts = Subviews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
  95. foreach (Shortcut shortcut in shortcuts)
  96. {
  97. // Let AutoSize do its thing to get the minimum width of each CommandView and HelpView
  98. shortcut.CommandView.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
  99. shortcut.KeyView.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
  100. shortcut.HelpView.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
  101. }
  102. maxCommandWidth = shortcuts.Max (s => s.CommandView.Frame.Width);
  103. maxHelpWidth = shortcuts.Max (s => s.HelpView.Frame.Width);
  104. // Set the width of all CommandView's and HelpView's to the max width
  105. foreach (Shortcut shortcut in shortcuts)
  106. {
  107. shortcut.CommandView.Width = Dim.Auto (minimumContentDim: maxCommandWidth);
  108. shortcut.KeyView.Width = Dim.Auto ();
  109. shortcut.HelpView.Width = Dim.Auto (minimumContentDim: maxHelpWidth);
  110. // shortcut.LayoutSubviews ();
  111. }
  112. // Set the overall size of the Bar and arrange the views vertically
  113. var maxBarItemWidth = 0;
  114. for (var index = 0; index < Subviews.Count; index++)
  115. {
  116. View barItem = Subviews [index];
  117. if (!barItem.Visible)
  118. {
  119. continue;
  120. }
  121. if (prevBarItem == null)
  122. {
  123. barItem.Y = 0;
  124. }
  125. else
  126. {
  127. // Align the view to the bottom of the previous view
  128. barItem.Y = index;
  129. }
  130. prevBarItem = barItem;
  131. if (barItem is Shortcut shortcut)
  132. {
  133. //shortcut.SetRelativeLayout (new (int.MaxValue, int.MaxValue));
  134. maxBarItemWidth = Math.Max (maxBarItemWidth, shortcut.Frame.Width);
  135. }
  136. else
  137. {
  138. maxBarItemWidth = Math.Max (maxBarItemWidth, barItem.Frame.Width);
  139. }
  140. barItem.X = 0;
  141. }
  142. foreach (Shortcut shortcut in shortcuts)
  143. {
  144. if (Width is DimAuto)
  145. {
  146. shortcut.Width = Dim.Auto (DimAutoStyle.Content, minimumContentDim: maxBarItemWidth);
  147. }
  148. else
  149. {
  150. //shortcut._container.Width = Dim.Fill ();
  151. // shortcut.Width = Dim.Fill ();
  152. }
  153. shortcut.LayoutSubviews ();
  154. }
  155. //for (var index = 0; index < Subviews.Count; index++)
  156. //{
  157. // var shortcut = Subviews [index] as Shortcut;
  158. // if (shortcut is { Visible: false })
  159. // {
  160. // continue;
  161. // }
  162. // if (Width is DimAuto)
  163. // {
  164. // shortcut._container.Width = Dim.Auto (DimAutoStyle.Content, minimumContentDim: maxBarItemWidth);
  165. // }
  166. // else
  167. // {
  168. // shortcut._container.Width = Dim.Fill ();
  169. // shortcut.Width = Dim.Fill ();
  170. // }
  171. // //shortcut.SetContentSize (new (maxBarItemWidth, 1));
  172. // //shortcut.Width = Dim.Auto (DimAutoStyle.Content, minimumContentDim: int.Max(maxBarItemWidth, GetContentSize().Width));
  173. //}
  174. break;
  175. }
  176. }
  177. private void SetInitialProperties ()
  178. {
  179. ColorScheme = Colors.ColorSchemes ["Menu"];
  180. CanFocus = true;
  181. Width = Dim.Auto ();
  182. Height = Dim.Auto ();
  183. LayoutStarted += Bar_LayoutStarted;
  184. }
  185. }