Bar.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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<View>? 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 (View 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 lineStyle)
  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 = lineStyle;
  75. }
  76. //base.SetBorderStyle(lineStyle);
  77. }
  78. #region IOrientation members
  79. /// <summary>
  80. /// Gets or sets the <see cref="Orientation"/> for this <see cref="Bar"/>. The default is
  81. /// <see cref="Orientation.Horizontal"/>.
  82. /// </summary>
  83. /// <remarks>
  84. /// <para>
  85. /// Horizontal orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from right to left
  86. /// Vertical orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from left to right.
  87. /// </para>
  88. /// </remarks>
  89. public Orientation Orientation
  90. {
  91. get => _orientationHelper.Orientation;
  92. set => _orientationHelper.Orientation = value;
  93. }
  94. #pragma warning disable CS0067 // The event is never used
  95. /// <inheritdoc/>
  96. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  97. /// <inheritdoc/>
  98. public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
  99. #pragma warning restore CS0067 // The event is never used
  100. /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
  101. /// <param name="newOrientation"></param>
  102. public void OnOrientationChanged (Orientation newOrientation)
  103. {
  104. // BUGBUG: this should not be SuperView.GetContentSize
  105. LayoutBarItems (SuperView?.GetContentSize () ?? Application.Screen.Size);
  106. }
  107. #endregion
  108. private AlignmentModes _alignmentModes = AlignmentModes.StartToEnd;
  109. /// <summary>
  110. /// Gets or sets the <see cref="AlignmentModes"/> for this <see cref="Bar"/>. The default is
  111. /// <see cref="AlignmentModes.StartToEnd"/>.
  112. /// </summary>
  113. public AlignmentModes AlignmentModes
  114. {
  115. get => _alignmentModes;
  116. set
  117. {
  118. _alignmentModes = value;
  119. //SetNeedsDraw ();
  120. SetNeedsLayout ();
  121. }
  122. }
  123. // TODO: Move this to View
  124. /// <summary>Inserts a <see cref="Shortcut"/> in the specified index of <see cref="View.SubViews"/>.</summary>
  125. /// <param name="index">The zero-based index at which item should be inserted.</param>
  126. /// <param name="item">The item to insert.</param>
  127. public void AddShortcutAt (int index, Shortcut item)
  128. {
  129. List<View> savedSubViewList = SubViews.ToList ();
  130. int count = savedSubViewList.Count;
  131. RemoveAll ();
  132. for (var i = 0; i <= count; i++)
  133. {
  134. if (i == index)
  135. {
  136. Add (item);
  137. }
  138. if (i < count)
  139. {
  140. Add (savedSubViewList [i]);
  141. }
  142. }
  143. //SetNeedsDraw ();
  144. SetNeedsLayout ();
  145. }
  146. // TODO: Move this to View
  147. /// <summary>Removes a <see cref="Shortcut"/> at specified index of <see cref="View.SubViews"/>.</summary>
  148. /// <param name="index">The zero-based index of the item to remove.</param>
  149. /// <returns>The <see cref="Shortcut"/> removed.</returns>
  150. public Shortcut? RemoveShortcut (int index)
  151. {
  152. View? toRemove = null;
  153. for (var i = 0; i < SubViews.Count; i++)
  154. {
  155. if (i == index)
  156. {
  157. toRemove = SubViews.ElementAt (i);
  158. }
  159. }
  160. if (toRemove is { })
  161. {
  162. Remove (toRemove);
  163. //SetNeedsDraw ();
  164. SetNeedsLayout ();
  165. }
  166. return toRemove as Shortcut;
  167. }
  168. /// <inheritdoc />
  169. protected override void OnSubViewLayout (LayoutEventArgs args)
  170. {
  171. LayoutBarItems (args.OldContentSize);
  172. }
  173. private void LayoutBarItems (Size contentSize)
  174. {
  175. View? prevBarItem = null;
  176. switch (Orientation)
  177. {
  178. case Orientation.Horizontal:
  179. for (var index = 0; index < SubViews.Count; index++)
  180. {
  181. View barItem = SubViews.ElementAt (index);
  182. barItem.ColorScheme = ColorScheme;
  183. barItem.X = Pos.Align (Alignment.Start, AlignmentModes);
  184. barItem.Y = 0; //Pos.Center ();
  185. if (barItem is Shortcut sc)
  186. {
  187. sc.Width = sc.GetWidthDimAuto ();
  188. }
  189. }
  190. break;
  191. case Orientation.Vertical:
  192. if (Width!.Has<DimAuto> (out _))
  193. {
  194. // Set the overall size of the Bar and arrange the views vertically
  195. var minKeyWidth = 0;
  196. List<Shortcut> shortcuts = SubViews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
  197. foreach (Shortcut shortcut in shortcuts)
  198. {
  199. // Get the largest width of all KeyView's
  200. minKeyWidth = int.Max (minKeyWidth, shortcut.KeyView.Text.GetColumns ());
  201. }
  202. var maxBarItemWidth = 0;
  203. for (var index = 0; index < SubViews.Count; index++)
  204. {
  205. View barItem = SubViews.ElementAt (index);
  206. barItem.ColorScheme = ColorScheme;
  207. if (!barItem.Visible)
  208. {
  209. continue;
  210. }
  211. if (barItem is Shortcut scBarItem)
  212. {
  213. barItem.X = 0;
  214. scBarItem.MinimumKeyTextSize = minKeyWidth;
  215. scBarItem.Width = scBarItem.GetWidthDimAuto ();
  216. barItem.Layout (Application.Screen.Size);
  217. maxBarItemWidth = Math.Max (maxBarItemWidth, barItem.Frame.Width);
  218. }
  219. if (prevBarItem == null)
  220. {
  221. // TODO: Just use Pos.Align!
  222. barItem.Y = 0;
  223. }
  224. else
  225. {
  226. // TODO: Just use Pos.Align!
  227. // Align the view to the bottom of the previous view
  228. barItem.Y = Pos.Bottom (prevBarItem);
  229. }
  230. prevBarItem = barItem;
  231. }
  232. foreach (var subView in SubViews)
  233. {
  234. if (subView is not Line)
  235. {
  236. subView.Width = Dim.Auto (DimAutoStyle.Auto, minimumContentDim: maxBarItemWidth, maximumContentDim: maxBarItemWidth);
  237. }
  238. }
  239. }
  240. else
  241. {
  242. foreach (var subView in SubViews)
  243. {
  244. if (subView is not Line)
  245. {
  246. subView.Width = Dim.Fill ();
  247. }
  248. }
  249. }
  250. break;
  251. }
  252. }
  253. /// <inheritdoc />
  254. public virtual bool EnableForDesign ()
  255. {
  256. var shortcut = new Shortcut
  257. {
  258. Text = "Quit",
  259. Title = "Q_uit",
  260. Key = Key.Z.WithCtrl,
  261. };
  262. Add (shortcut);
  263. shortcut = new Shortcut
  264. {
  265. Text = "Help Text",
  266. Title = "Help",
  267. Key = Key.F1,
  268. };
  269. Add (shortcut);
  270. shortcut = new Shortcut
  271. {
  272. Text = "Czech",
  273. CommandView = new CheckBox ()
  274. {
  275. Title = "_Check"
  276. },
  277. Key = Key.F9,
  278. CanFocus = false
  279. };
  280. Add (shortcut);
  281. return true;
  282. }
  283. }