Bar.cs 7.1 KB

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