Bar.cs 9.4 KB

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