Bar.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Provides a horizontally or vertically oriented container for <see cref="Shortcut"/>s to be used as a menu, toolbar, or status
  4. /// bar.
  5. /// </summary>
  6. /// <remarks>
  7. /// <para>
  8. /// Any <see cref="View"/> can be added to a <see cref="Bar"/>. However, the <see cref="Bar"/> is designed to work with
  9. /// <see cref="Shortcut"/> objects. The <see cref="Shortcut"/> class provides a way to display a command, help, and key and
  10. /// align them in a specific order.
  11. /// </para>
  12. /// </remarks>
  13. public class Bar : View
  14. {
  15. /// <inheritdoc/>
  16. public Bar () : this ([]) { }
  17. /// <inheritdoc/>
  18. public Bar (IEnumerable<Shortcut> shortcuts)
  19. {
  20. CanFocus = true;
  21. Width = Dim.Auto ();
  22. Height = Dim.Auto ();
  23. Initialized += Bar_Initialized;
  24. if (shortcuts is null)
  25. {
  26. return;
  27. }
  28. foreach (Shortcut shortcut in shortcuts)
  29. {
  30. Add (shortcut);
  31. }
  32. }
  33. private void Bar_Initialized (object sender, EventArgs e) { ColorScheme = Colors.ColorSchemes ["Menu"]; }
  34. /// <inheritdoc/>
  35. public override void SetBorderStyle (LineStyle value)
  36. {
  37. // The default changes the thickness. We don't want that. We just set the style.
  38. Border.LineStyle = value;
  39. }
  40. private Orientation _orientation = Orientation.Horizontal;
  41. /// <summary>
  42. /// Gets or sets the <see cref="Orientation"/> for this <see cref="Bar"/>. The default is
  43. /// <see cref="Orientation.Horizontal"/>.
  44. /// </summary>
  45. /// <remarks>
  46. /// <para>
  47. /// Horizontal orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from right to left
  48. /// Vertical orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from left to right.
  49. /// </para>
  50. /// </remarks>
  51. public Orientation Orientation
  52. {
  53. get => _orientation;
  54. set
  55. {
  56. _orientation = value;
  57. SetNeedsLayout ();
  58. }
  59. }
  60. private AlignmentModes _alignmentModes = AlignmentModes.StartToEnd;
  61. /// <summary>
  62. /// Gets or sets the <see cref="AlignmentModes"/> for this <see cref="Bar"/>. The default is
  63. /// <see cref="AlignmentModes.StartToEnd"/>.
  64. /// </summary>
  65. public AlignmentModes AlignmentModes
  66. {
  67. get => _alignmentModes;
  68. set
  69. {
  70. _alignmentModes = value;
  71. SetNeedsLayout ();
  72. }
  73. }
  74. // TODO: Move this to View
  75. /// <summary>Inserts a <see cref="Shortcut"/> in the specified index of <see cref="View.Subviews"/>.</summary>
  76. /// <param name="index">The zero-based index at which item should be inserted.</param>
  77. /// <param name="item">The item to insert.</param>
  78. public void AddShortcutAt (int index, Shortcut item)
  79. {
  80. List<View> savedSubViewList = Subviews.ToList ();
  81. int count = savedSubViewList.Count;
  82. RemoveAll ();
  83. for (var i = 0; i <= count; i++)
  84. {
  85. if (i == index)
  86. {
  87. Add (item);
  88. }
  89. if (i < count)
  90. {
  91. Add (savedSubViewList [i]);
  92. }
  93. }
  94. SetNeedsDisplay ();
  95. }
  96. // TODO: Move this to View
  97. /// <summary>Removes a <see cref="Shortcut"/> at specified index of <see cref="View.Subviews"/>.</summary>
  98. /// <param name="index">The zero-based index of the item to remove.</param>
  99. /// <returns>The <see cref="Shortcut"/> removed.</returns>
  100. public Shortcut RemoveShortcut (int index)
  101. {
  102. View toRemove = null;
  103. for (var i = 0; i < Subviews.Count; i++)
  104. {
  105. if (i == index)
  106. {
  107. toRemove = Subviews [i];
  108. }
  109. }
  110. if (toRemove is { })
  111. {
  112. Remove (toRemove);
  113. SetNeedsDisplay ();
  114. }
  115. return toRemove as Shortcut;
  116. }
  117. /// <inheritdoc />
  118. internal override void OnLayoutStarted (LayoutEventArgs args)
  119. {
  120. base.OnLayoutStarted (args);
  121. View prevBarItem = null;
  122. switch (Orientation)
  123. {
  124. case Orientation.Horizontal:
  125. for (var index = 0; index < Subviews.Count; index++)
  126. {
  127. View barItem = Subviews [index];
  128. barItem.ColorScheme = ColorScheme;
  129. barItem.X = Pos.Align (Alignment.Start, AlignmentModes);
  130. barItem.Y = 0; //Pos.Center ();
  131. // HACK: This should not be needed
  132. barItem.SetRelativeLayout (GetContentSize ());
  133. }
  134. break;
  135. case Orientation.Vertical:
  136. // Set the overall size of the Bar and arrange the views vertically
  137. var minKeyWidth = 0;
  138. List<Shortcut> shortcuts = Subviews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
  139. foreach (Shortcut shortcut in shortcuts)
  140. {
  141. // Let DimAuto do its thing to get the minimum width of each CommandView and HelpView
  142. //shortcut.CommandView.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
  143. minKeyWidth = int.Max (minKeyWidth, shortcut.KeyView.Text.GetColumns ());
  144. }
  145. var maxBarItemWidth = 0;
  146. var totalHeight = 0;
  147. for (var index = 0; index < Subviews.Count; index++)
  148. {
  149. View barItem = Subviews [index];
  150. barItem.ColorScheme = ColorScheme;
  151. if (!barItem.Visible)
  152. {
  153. continue;
  154. }
  155. if (barItem is Shortcut scBarItem)
  156. {
  157. scBarItem.MinimumKeyTextSize = minKeyWidth;
  158. // HACK: This should not be needed
  159. scBarItem.SetRelativeLayout (GetContentSize ());
  160. maxBarItemWidth = Math.Max (maxBarItemWidth, scBarItem.Frame.Width);
  161. }
  162. if (prevBarItem == null)
  163. {
  164. barItem.Y = 0;
  165. }
  166. else
  167. {
  168. // Align the view to the bottom of the previous view
  169. barItem.Y = Pos.Bottom (prevBarItem);
  170. }
  171. prevBarItem = barItem;
  172. barItem.X = 0;
  173. totalHeight += barItem.Frame.Height;
  174. }
  175. foreach (View barItem in Subviews)
  176. {
  177. barItem.Width = maxBarItemWidth;
  178. if (barItem is Line line)
  179. {
  180. }
  181. }
  182. Height = Dim.Auto (DimAutoStyle.Content, totalHeight);
  183. break;
  184. }
  185. }
  186. }