Bar.cs 7.7 KB

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