Bar.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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)
  66. {
  67. ColorScheme = Colors.ColorSchemes ["Menu"];
  68. LayoutBarItems (GetContentSize ());
  69. }
  70. /// <inheritdoc/>
  71. public override void SetBorderStyle (LineStyle value)
  72. {
  73. // The default changes the thickness. We don't want that. We just set the style.
  74. Border.LineStyle = value;
  75. }
  76. #region IOrientation members
  77. /// <summary>
  78. /// Gets or sets the <see cref="Orientation"/> for this <see cref="Bar"/>. The default is
  79. /// <see cref="Orientation.Horizontal"/>.
  80. /// </summary>
  81. /// <remarks>
  82. /// <para>
  83. /// Horizontal orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from right to left
  84. /// Vertical orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from left to right.
  85. /// </para>
  86. /// </remarks>
  87. public Orientation Orientation
  88. {
  89. get => _orientationHelper.Orientation;
  90. set => _orientationHelper.Orientation = value;
  91. }
  92. /// <inheritdoc/>
  93. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  94. /// <inheritdoc/>
  95. public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
  96. /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
  97. /// <param name="newOrientation"></param>
  98. public void OnOrientationChanged (Orientation newOrientation)
  99. {
  100. SetNeedsLayout ();
  101. }
  102. #endregion
  103. private AlignmentModes _alignmentModes = AlignmentModes.StartToEnd;
  104. /// <summary>
  105. /// Gets or sets the <see cref="AlignmentModes"/> for this <see cref="Bar"/>. The default is
  106. /// <see cref="AlignmentModes.StartToEnd"/>.
  107. /// </summary>
  108. public AlignmentModes AlignmentModes
  109. {
  110. get => _alignmentModes;
  111. set
  112. {
  113. _alignmentModes = value;
  114. SetNeedsLayout ();
  115. }
  116. }
  117. // TODO: Move this to View
  118. /// <summary>Inserts a <see cref="Shortcut"/> in the specified index of <see cref="View.Subviews"/>.</summary>
  119. /// <param name="index">The zero-based index at which item should be inserted.</param>
  120. /// <param name="item">The item to insert.</param>
  121. public void AddShortcutAt (int index, Shortcut item)
  122. {
  123. List<View> savedSubViewList = Subviews.ToList ();
  124. int count = savedSubViewList.Count;
  125. RemoveAll ();
  126. for (var i = 0; i <= count; i++)
  127. {
  128. if (i == index)
  129. {
  130. Add (item);
  131. }
  132. if (i < count)
  133. {
  134. Add (savedSubViewList [i]);
  135. }
  136. }
  137. SetNeedsDisplay ();
  138. }
  139. // TODO: Move this to View
  140. /// <summary>Removes a <see cref="Shortcut"/> at specified index of <see cref="View.Subviews"/>.</summary>
  141. /// <param name="index">The zero-based index of the item to remove.</param>
  142. /// <returns>The <see cref="Shortcut"/> removed.</returns>
  143. public Shortcut? RemoveShortcut (int index)
  144. {
  145. View? toRemove = null;
  146. for (var i = 0; i < Subviews.Count; i++)
  147. {
  148. if (i == index)
  149. {
  150. toRemove = Subviews [i];
  151. }
  152. }
  153. if (toRemove is { })
  154. {
  155. Remove (toRemove);
  156. SetNeedsDisplay ();
  157. }
  158. return toRemove as Shortcut;
  159. }
  160. /// <inheritdoc />
  161. internal override void OnLayoutStarted (LayoutEventArgs args)
  162. {
  163. base.OnLayoutStarted (args);
  164. LayoutBarItems (args.OldContentSize);
  165. }
  166. private void LayoutBarItems (Size contentSize)
  167. {
  168. View? prevBarItem = null;
  169. switch (Orientation)
  170. {
  171. case Orientation.Horizontal:
  172. for (var index = 0; index < Subviews.Count; index++)
  173. {
  174. View barItem = Subviews [index];
  175. barItem.ColorScheme = ColorScheme;
  176. barItem.X = Pos.Align (Alignment.Start, AlignmentModes);
  177. barItem.Y = 0; //Pos.Center ();
  178. }
  179. break;
  180. case Orientation.Vertical:
  181. // Set the overall size of the Bar and arrange the views vertically
  182. var minKeyWidth = 0;
  183. List<Shortcut> shortcuts = Subviews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
  184. foreach (Shortcut shortcut in shortcuts)
  185. {
  186. // Let DimAuto do its thing to get the minimum width of each CommandView and HelpView
  187. //shortcut.CommandView.SetRelativeLayout (new Size (int.MaxValue, int.MaxValue));
  188. minKeyWidth = int.Max (minKeyWidth, shortcut.KeyView.Text.GetColumns ());
  189. }
  190. var maxBarItemWidth = 0;
  191. var totalHeight = 0;
  192. for (var index = 0; index < Subviews.Count; index++)
  193. {
  194. View barItem = Subviews [index];
  195. barItem.ColorScheme = ColorScheme;
  196. if (!barItem.Visible)
  197. {
  198. continue;
  199. }
  200. if (barItem is Shortcut scBarItem)
  201. {
  202. scBarItem.MinimumKeyTextSize = minKeyWidth;
  203. maxBarItemWidth = Math.Max (maxBarItemWidth, scBarItem.Frame.Width);
  204. }
  205. if (prevBarItem == null)
  206. {
  207. barItem.Y = 0;
  208. }
  209. else
  210. {
  211. // Align the view to the bottom of the previous view
  212. barItem.Y = Pos.Bottom (prevBarItem);
  213. }
  214. prevBarItem = barItem;
  215. barItem.X = 0;
  216. totalHeight += barItem.Frame.Height;
  217. }
  218. foreach (View barItem in Subviews)
  219. {
  220. barItem.Width = maxBarItemWidth;
  221. }
  222. Height = Dim.Auto (DimAutoStyle.Content, totalHeight);
  223. break;
  224. }
  225. }
  226. /// <inheritdoc />
  227. public bool EnableForDesign ()
  228. {
  229. var shortcut = new Shortcut
  230. {
  231. Text = "Quit",
  232. Title = "Q_uit",
  233. Key = Key.Z.WithCtrl,
  234. };
  235. Add (shortcut);
  236. shortcut = new Shortcut
  237. {
  238. Text = "Help Text",
  239. Title = "Help",
  240. Key = Key.F1,
  241. };
  242. Add (shortcut);
  243. shortcut = new Shortcut
  244. {
  245. Text = "Czech",
  246. CommandView = new CheckBox ()
  247. {
  248. Title = "_Check"
  249. },
  250. Key = Key.F9,
  251. CanFocus = false
  252. };
  253. Add (shortcut);
  254. return true;
  255. }
  256. }