Bar.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. // Initialized += Bar_Initialized;
  27. MouseEvent += OnMouseEvent;
  28. if (shortcuts is { })
  29. {
  30. foreach (Shortcut shortcut in shortcuts)
  31. {
  32. Add (shortcut);
  33. }
  34. }
  35. }
  36. private void OnMouseEvent (object? sender, MouseEventArgs e)
  37. {
  38. NavigationDirection direction = NavigationDirection.Backward;
  39. if (e.Flags == MouseFlags.WheeledDown)
  40. {
  41. e.Handled = true;
  42. }
  43. if (e.Flags == MouseFlags.WheeledUp)
  44. {
  45. direction = NavigationDirection.Forward;
  46. e.Handled = true;
  47. }
  48. if (e.Flags == MouseFlags.WheeledRight)
  49. {
  50. e.Handled = true;
  51. }
  52. if (e.Flags == MouseFlags.WheeledLeft)
  53. {
  54. direction = NavigationDirection.Forward;
  55. e.Handled = true;
  56. }
  57. if (e.Handled)
  58. {
  59. e.Handled = AdvanceFocus (direction, TabBehavior.TabStop);
  60. }
  61. }
  62. /// <inheritdoc />
  63. public override void EndInit ()
  64. {
  65. base.EndInit ();
  66. ColorScheme = Colors.ColorSchemes ["Menu"];
  67. }
  68. /// <inheritdoc/>
  69. public override void SetBorderStyle (LineStyle value)
  70. {
  71. if (Border is { })
  72. {
  73. // The default changes the thickness. We don't want that. We just set the style.
  74. Border.LineStyle = value;
  75. }
  76. }
  77. #region IOrientation members
  78. /// <summary>
  79. /// Gets or sets the <see cref="Orientation"/> for this <see cref="Bar"/>. The default is
  80. /// <see cref="Orientation.Horizontal"/>.
  81. /// </summary>
  82. /// <remarks>
  83. /// <para>
  84. /// Horizontal orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from right to left
  85. /// Vertical orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from left to right.
  86. /// </para>
  87. /// </remarks>
  88. public Orientation Orientation
  89. {
  90. get => _orientationHelper.Orientation;
  91. set => _orientationHelper.Orientation = value;
  92. }
  93. /// <inheritdoc/>
  94. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  95. /// <inheritdoc/>
  96. public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
  97. /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
  98. /// <param name="newOrientation"></param>
  99. public void OnOrientationChanged (Orientation newOrientation)
  100. {
  101. // BUGBUG: this should not be SuperView.GetContentSize
  102. LayoutBarItems (SuperView?.GetContentSize () ?? Application.Screen.Size);
  103. }
  104. #endregion
  105. private AlignmentModes _alignmentModes = AlignmentModes.StartToEnd;
  106. /// <summary>
  107. /// Gets or sets the <see cref="AlignmentModes"/> for this <see cref="Bar"/>. The default is
  108. /// <see cref="AlignmentModes.StartToEnd"/>.
  109. /// </summary>
  110. public AlignmentModes AlignmentModes
  111. {
  112. get => _alignmentModes;
  113. set
  114. {
  115. _alignmentModes = value;
  116. //SetNeedsDraw ();
  117. SetNeedsLayout ();
  118. }
  119. }
  120. // TODO: Move this to View
  121. /// <summary>Inserts a <see cref="Shortcut"/> in the specified index of <see cref="View.Subviews"/>.</summary>
  122. /// <param name="index">The zero-based index at which item should be inserted.</param>
  123. /// <param name="item">The item to insert.</param>
  124. public void AddShortcutAt (int index, Shortcut item)
  125. {
  126. List<View> savedSubViewList = Subviews.ToList ();
  127. int count = savedSubViewList.Count;
  128. RemoveAll ();
  129. for (var i = 0; i <= count; i++)
  130. {
  131. if (i == index)
  132. {
  133. Add (item);
  134. }
  135. if (i < count)
  136. {
  137. Add (savedSubViewList [i]);
  138. }
  139. }
  140. //SetNeedsDraw ();
  141. SetNeedsLayout ();
  142. }
  143. // TODO: Move this to View
  144. /// <summary>Removes a <see cref="Shortcut"/> at specified index of <see cref="View.Subviews"/>.</summary>
  145. /// <param name="index">The zero-based index of the item to remove.</param>
  146. /// <returns>The <see cref="Shortcut"/> removed.</returns>
  147. public Shortcut? RemoveShortcut (int index)
  148. {
  149. View? toRemove = null;
  150. for (var i = 0; i < Subviews.Count; i++)
  151. {
  152. if (i == index)
  153. {
  154. toRemove = Subviews [i];
  155. }
  156. }
  157. if (toRemove is { })
  158. {
  159. Remove (toRemove);
  160. //SetNeedsDraw ();
  161. SetNeedsLayout ();
  162. }
  163. return toRemove as Shortcut;
  164. }
  165. /// <inheritdoc />
  166. protected override void OnSubviewLayout (LayoutEventArgs args)
  167. {
  168. LayoutBarItems (args.OldContentSize);
  169. }
  170. private void LayoutBarItems (Size contentSize)
  171. {
  172. View? prevBarItem = null;
  173. switch (Orientation)
  174. {
  175. case Orientation.Horizontal:
  176. for (var index = 0; index < Subviews.Count; index++)
  177. {
  178. View barItem = Subviews [index];
  179. barItem.ColorScheme = ColorScheme;
  180. barItem.X = Pos.Align (Alignment.Start, AlignmentModes);
  181. barItem.Y = 0; //Pos.Center ();
  182. }
  183. break;
  184. case Orientation.Vertical:
  185. if (Width!.Has<DimAuto> (out _))
  186. {
  187. // Set the overall size of the Bar and arrange the views vertically
  188. var minKeyWidth = 0;
  189. List<Shortcut> shortcuts = Subviews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
  190. foreach (Shortcut shortcut in shortcuts)
  191. {
  192. // Get the largest width of all KeyView's
  193. minKeyWidth = int.Max (minKeyWidth, shortcut.KeyView.Text.GetColumns ());
  194. }
  195. var _maxBarItemWidth = 0;
  196. for (var index = 0; index < Subviews.Count; index++)
  197. {
  198. View barItem = Subviews [index];
  199. barItem.ColorScheme = ColorScheme;
  200. if (!barItem.Visible)
  201. {
  202. continue;
  203. }
  204. if (barItem is Shortcut scBarItem)
  205. {
  206. barItem.X = 0;
  207. scBarItem.MinimumKeyTextSize = minKeyWidth;
  208. scBarItem.Width = scBarItem.GetWidthDimAuto ();
  209. barItem.Layout (Application.Screen.Size);
  210. _maxBarItemWidth = Math.Max (_maxBarItemWidth, barItem.Frame.Width);
  211. }
  212. if (prevBarItem == null)
  213. {
  214. // TODO: Just use Pos.Align!
  215. barItem.Y = 0;
  216. }
  217. else
  218. {
  219. // TODO: Just use Pos.Align!
  220. // Align the view to the bottom of the previous view
  221. barItem.Y = Pos.Bottom (prevBarItem);
  222. }
  223. prevBarItem = barItem;
  224. }
  225. foreach (var subView in Subviews)
  226. {
  227. if (subView is not Line)
  228. {
  229. subView.Width = Dim.Auto (DimAutoStyle.Auto, minimumContentDim: _maxBarItemWidth);
  230. }
  231. }
  232. }
  233. else
  234. {
  235. foreach (var subView in Subviews)
  236. {
  237. if (subView is not Line)
  238. {
  239. subView.Width = Dim.Fill ();
  240. }
  241. }
  242. }
  243. break;
  244. }
  245. }
  246. /// <inheritdoc />
  247. public bool EnableForDesign ()
  248. {
  249. var shortcut = new Shortcut
  250. {
  251. Text = "Quit",
  252. Title = "Q_uit",
  253. Key = Key.Z.WithCtrl,
  254. };
  255. Add (shortcut);
  256. shortcut = new Shortcut
  257. {
  258. Text = "Help Text",
  259. Title = "Help",
  260. Key = Key.F1,
  261. };
  262. Add (shortcut);
  263. shortcut = new Shortcut
  264. {
  265. Text = "Czech",
  266. CommandView = new CheckBox ()
  267. {
  268. Title = "_Check"
  269. },
  270. Key = Key.F9,
  271. CanFocus = false
  272. };
  273. Add (shortcut);
  274. return true;
  275. }
  276. }