Bar.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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, IOrientation, IDesignable
  14. {
  15. private readonly OrientationHelper _orientationHelper;
  16. /// <inheritdoc/>
  17. public Bar () : this ([]) { }
  18. /// <inheritdoc/>
  19. public Bar (IEnumerable<Shortcut> shortcuts)
  20. {
  21. CanFocus = true;
  22. Width = Dim.Auto ();
  23. Height = Dim.Auto ();
  24. _orientationHelper = new (this);
  25. _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
  26. _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
  27. Initialized += Bar_Initialized;
  28. if (shortcuts is null)
  29. {
  30. return;
  31. }
  32. foreach (Shortcut shortcut in shortcuts)
  33. {
  34. Add (shortcut);
  35. }
  36. }
  37. private void Bar_Initialized (object sender, EventArgs e) { ColorScheme = Colors.ColorSchemes ["Menu"]; }
  38. /// <inheritdoc/>
  39. public override void SetBorderStyle (LineStyle value)
  40. {
  41. // The default changes the thickness. We don't want that. We just set the style.
  42. Border.LineStyle = value;
  43. }
  44. #region IOrientation members
  45. /// <summary>
  46. /// Gets or sets the <see cref="Orientation"/> for this <see cref="Bar"/>. The default is
  47. /// <see cref="Orientation.Horizontal"/>.
  48. /// </summary>
  49. /// <remarks>
  50. /// <para>
  51. /// Horizontal orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from right to left
  52. /// Vertical orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from left to right.
  53. /// </para>
  54. /// </remarks>
  55. public Orientation Orientation
  56. {
  57. get => _orientationHelper.Orientation;
  58. set => _orientationHelper.Orientation = value;
  59. }
  60. /// <inheritdoc/>
  61. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
  62. /// <inheritdoc/>
  63. public event EventHandler<EventArgs<Orientation>> OrientationChanged;
  64. /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
  65. /// <param name="newOrientation"></param>
  66. public void OnOrientationChanged (Orientation newOrientation)
  67. {
  68. SetNeedsLayout ();
  69. }
  70. #endregion
  71. private AlignmentModes _alignmentModes = AlignmentModes.StartToEnd;
  72. /// <summary>
  73. /// Gets or sets the <see cref="AlignmentModes"/> for this <see cref="Bar"/>. The default is
  74. /// <see cref="AlignmentModes.StartToEnd"/>.
  75. /// </summary>
  76. public AlignmentModes AlignmentModes
  77. {
  78. get => _alignmentModes;
  79. set
  80. {
  81. _alignmentModes = value;
  82. SetNeedsLayout ();
  83. }
  84. }
  85. // TODO: Move this to View
  86. /// <summary>Inserts a <see cref="Shortcut"/> in the specified index of <see cref="View.Subviews"/>.</summary>
  87. /// <param name="index">The zero-based index at which item should be inserted.</param>
  88. /// <param name="item">The item to insert.</param>
  89. public void AddShortcutAt (int index, Shortcut item)
  90. {
  91. List<View> savedSubViewList = Subviews.ToList ();
  92. int count = savedSubViewList.Count;
  93. RemoveAll ();
  94. for (var i = 0; i <= count; i++)
  95. {
  96. if (i == index)
  97. {
  98. Add (item);
  99. }
  100. if (i < count)
  101. {
  102. Add (savedSubViewList [i]);
  103. }
  104. }
  105. SetNeedsDisplay ();
  106. }
  107. // TODO: Move this to View
  108. /// <summary>Removes a <see cref="Shortcut"/> at specified index of <see cref="View.Subviews"/>.</summary>
  109. /// <param name="index">The zero-based index of the item to remove.</param>
  110. /// <returns>The <see cref="Shortcut"/> removed.</returns>
  111. public Shortcut RemoveShortcut (int index)
  112. {
  113. View toRemove = null;
  114. for (var i = 0; i < Subviews.Count; i++)
  115. {
  116. if (i == index)
  117. {
  118. toRemove = Subviews [i];
  119. }
  120. }
  121. if (toRemove is { })
  122. {
  123. Remove (toRemove);
  124. SetNeedsDisplay ();
  125. }
  126. return toRemove as Shortcut;
  127. }
  128. /// <inheritdoc />
  129. internal override void OnLayoutStarted (LayoutEventArgs args)
  130. {
  131. base.OnLayoutStarted (args);
  132. View prevBarItem = null;
  133. switch (Orientation)
  134. {
  135. case Orientation.Horizontal:
  136. for (var index = 0; index < Subviews.Count; index++)
  137. {
  138. View barItem = Subviews [index];
  139. barItem.ColorScheme = ColorScheme;
  140. barItem.X = Pos.Align (Alignment.Start, AlignmentModes);
  141. barItem.Y = 0; //Pos.Center ();
  142. // HACK: This should not be needed
  143. barItem.SetRelativeLayout (GetContentSize ());
  144. }
  145. break;
  146. case Orientation.Vertical:
  147. // Set the overall size of the Bar and arrange the views vertically
  148. var minKeyWidth = 0;
  149. List<Shortcut> shortcuts = Subviews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
  150. foreach (Shortcut shortcut in shortcuts)
  151. {
  152. // Let DimAuto do its thing to get the minimum width of each CommandView and HelpView
  153. //shortcut.CommandView.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
  154. minKeyWidth = int.Max (minKeyWidth, shortcut.KeyView.Text.GetColumns ());
  155. }
  156. var maxBarItemWidth = 0;
  157. var totalHeight = 0;
  158. for (var index = 0; index < Subviews.Count; index++)
  159. {
  160. View barItem = Subviews [index];
  161. barItem.ColorScheme = ColorScheme;
  162. if (!barItem.Visible)
  163. {
  164. continue;
  165. }
  166. if (barItem is Shortcut scBarItem)
  167. {
  168. scBarItem.MinimumKeyTextSize = minKeyWidth;
  169. // HACK: This should not be needed
  170. scBarItem.SetRelativeLayout (GetContentSize ());
  171. maxBarItemWidth = Math.Max (maxBarItemWidth, scBarItem.Frame.Width);
  172. }
  173. if (prevBarItem == null)
  174. {
  175. barItem.Y = 0;
  176. }
  177. else
  178. {
  179. // Align the view to the bottom of the previous view
  180. barItem.Y = Pos.Bottom (prevBarItem);
  181. }
  182. prevBarItem = barItem;
  183. barItem.X = 0;
  184. totalHeight += barItem.Frame.Height;
  185. }
  186. foreach (View barItem in Subviews)
  187. {
  188. barItem.Width = maxBarItemWidth;
  189. if (barItem is Line line)
  190. {
  191. }
  192. }
  193. Height = Dim.Auto (DimAutoStyle.Content, totalHeight);
  194. break;
  195. }
  196. }
  197. /// <inheritdoc />
  198. public bool EnableForDesign ()
  199. {
  200. var shortcut = new Shortcut
  201. {
  202. Text = "Quit",
  203. Title = "Q_uit",
  204. Key = Key.Z.WithCtrl,
  205. };
  206. Add (shortcut);
  207. shortcut = new Shortcut
  208. {
  209. Text = "Help Text",
  210. Title = "Help",
  211. Key = Key.F1,
  212. };
  213. Add (shortcut);
  214. return true;
  215. }
  216. }