Bar.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /// <summary>Inserts a <see cref="Shortcut"/> in the specified index of <see cref="Items"/>.</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. Add (savedSubViewList [i]);
  90. }
  91. SetNeedsDisplay ();
  92. }
  93. /// <summary>Removes a <see cref="Shortcut"/> at specified index of <see cref="Items"/>.</summary>
  94. /// <param name="index">The zero-based index of the item to remove.</param>
  95. /// <returns>The <see cref="Shortcut"/> removed.</returns>
  96. public Shortcut RemoveShortcut (int index)
  97. {
  98. View toRemove = null;
  99. for (var i = 0; i < Subviews.Count; i++)
  100. {
  101. if (i == index)
  102. {
  103. toRemove = Subviews [i];
  104. }
  105. }
  106. if (toRemove is { })
  107. {
  108. Remove (toRemove);
  109. SetNeedsDisplay ();
  110. }
  111. return toRemove as Shortcut;
  112. }
  113. private void Bar_LayoutStarted (object sender, LayoutEventArgs e)
  114. {
  115. View prevBarItem = null;
  116. switch (Orientation)
  117. {
  118. case Orientation.Horizontal:
  119. for (var index = 0; index < Subviews.Count; index++)
  120. {
  121. View barItem = Subviews [index];
  122. barItem.ColorScheme = ColorScheme;
  123. barItem.X = Pos.Align (Alignment.Start, AlignmentModes);
  124. barItem.Y = 0; //Pos.Center ();
  125. // HACK: This should not be needed
  126. barItem.SetRelativeLayout (GetContentSize ());
  127. }
  128. break;
  129. case Orientation.Vertical:
  130. // CommandView is aligned left, HelpView is aligned right, KeyView is aligned right
  131. // All CommandView's are the same width, all HelpView's are the same width,
  132. // all KeyView's are the same width
  133. var maxCommandWidth = 0;
  134. var maxHelpWidth = 0;
  135. var minKeyWidth = 0;
  136. List<Shortcut> shortcuts = Subviews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
  137. foreach (Shortcut shortcut in shortcuts)
  138. {
  139. // Let AutoSize do its thing to get the minimum width of each CommandView and HelpView
  140. //shortcut.CommandView.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
  141. minKeyWidth = int.Max (minKeyWidth, shortcut.KeyView.Text.GetColumns ());
  142. }
  143. // Set the overall size of the Bar and arrange the views vertically
  144. var maxBarItemWidth = 0;
  145. var totalHeight = 0;
  146. for (var index = 0; index < Subviews.Count; index++)
  147. {
  148. View barItem = Subviews [index];
  149. if (barItem is Shortcut scBarItem)
  150. {
  151. scBarItem.MinimumKeyViewSize = minKeyWidth;
  152. }
  153. if (!barItem.Visible)
  154. {
  155. continue;
  156. }
  157. if (prevBarItem == null)
  158. {
  159. barItem.Y = 0;
  160. }
  161. else
  162. {
  163. // Align the view to the bottom of the previous view
  164. barItem.Y = Pos.Bottom (prevBarItem);
  165. }
  166. prevBarItem = barItem;
  167. if (barItem is Shortcut shortcut)
  168. {
  169. maxBarItemWidth = Math.Max (maxBarItemWidth, shortcut.Frame.Width);
  170. }
  171. else
  172. {
  173. maxBarItemWidth = Math.Max (maxBarItemWidth, barItem.Frame.Width);
  174. }
  175. barItem.X = 0;
  176. totalHeight += barItem.Frame.Height;
  177. }
  178. foreach (Shortcut shortcut in shortcuts)
  179. {
  180. shortcut.Width = maxBarItemWidth;
  181. }
  182. Height = Dim.Auto (DimAutoStyle.Content, totalHeight);
  183. break;
  184. }
  185. }
  186. }