Bar.cs 8.9 KB

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