StatusBar.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// A status bar is a <see cref="View"/> that snaps to the bottom of a <see cref="Toplevel"/> displaying set of
  4. /// <see cref="StatusItem"/>s. The <see cref="StatusBar"/> should be context sensitive. This means, if the main menu
  5. /// and an open text editor are visible, the items probably shown will be ~F1~ Help ~F2~ Save ~F3~ Load. While a dialog
  6. /// to ask a file to load is executed, the remaining commands will probably be ~F1~ Help. So for each context must be a
  7. /// new instance of a status bar.
  8. /// </summary>
  9. public class StatusBar : View
  10. {
  11. private static Rune _shortcutDelimiter = (Rune)'=';
  12. private StatusItem [] _items = [];
  13. /// <summary>Initializes a new instance of the <see cref="StatusBar"/> class.</summary>
  14. public StatusBar () : this (new StatusItem [] { }) { }
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="StatusBar"/> class with the specified set of
  17. /// <see cref="StatusItem"/> s. The <see cref="StatusBar"/> will be drawn on the lowest line of the terminal or
  18. /// <see cref="View.SuperView"/> (if not null).
  19. /// </summary>
  20. /// <param name="items">A list of status bar items.</param>
  21. public StatusBar (StatusItem [] items)
  22. {
  23. if (items is { })
  24. {
  25. Items = items;
  26. }
  27. CanFocus = false;
  28. ColorScheme = Colors.ColorSchemes ["Menu"];
  29. X = 0;
  30. Y = Pos.AnchorEnd ();
  31. Width = Dim.Fill ();
  32. Height = 1; // BUGBUG: Views should avoid setting Height as doing so implies Frame.Size == GetContentSize ().
  33. AddCommand (Command.Accept, ctx => InvokeItem ((StatusItem)ctx.KeyBinding?.Context));
  34. }
  35. /// <summary>The items that compose the <see cref="StatusBar"/></summary>
  36. public StatusItem [] Items
  37. {
  38. get => _items;
  39. set
  40. {
  41. foreach (StatusItem item in _items)
  42. {
  43. KeyBindings.Remove (item.Shortcut);
  44. }
  45. _items = value;
  46. foreach (StatusItem item in _items.Where (i => i.Shortcut != Key.Empty))
  47. {
  48. KeyBinding keyBinding = new (new [] { Command.Accept }, KeyBindingScope.HotKey, item);
  49. KeyBindings.Add (item.Shortcut, keyBinding);
  50. }
  51. }
  52. }
  53. /// <summary>Gets or sets shortcut delimiter separator. The default is "-".</summary>
  54. public static Rune ShortcutDelimiter
  55. {
  56. get => _shortcutDelimiter;
  57. set
  58. {
  59. if (_shortcutDelimiter != value)
  60. {
  61. _shortcutDelimiter = value == default (Rune) ? (Rune)'=' : value;
  62. }
  63. }
  64. }
  65. /// <summary>Inserts a <see cref="StatusItem"/> in the specified index of <see cref="Items"/>.</summary>
  66. /// <param name="index">The zero-based index at which item should be inserted.</param>
  67. /// <param name="item">The item to insert.</param>
  68. public void AddItemAt (int index, StatusItem item)
  69. {
  70. List<StatusItem> itemsList = new (Items);
  71. itemsList.Insert (index, item);
  72. Items = itemsList.ToArray ();
  73. SetNeedsDisplay ();
  74. }
  75. ///<inheritdoc/>
  76. protected internal override bool OnMouseEvent (MouseEvent me)
  77. {
  78. if (me.Flags != MouseFlags.Button1Clicked)
  79. {
  80. return false;
  81. }
  82. var pos = 1;
  83. for (var i = 0; i < Items.Length; i++)
  84. {
  85. if (me.Position.X >= pos && me.Position.X < pos + GetItemTitleLength (Items [i].Title))
  86. {
  87. StatusItem item = Items [i];
  88. if (item.IsEnabled ())
  89. {
  90. Run (item.Action);
  91. }
  92. break;
  93. }
  94. pos += GetItemTitleLength (Items [i].Title) + 3;
  95. }
  96. return true;
  97. }
  98. ///<inheritdoc/>
  99. public override void OnDrawContent (Rectangle viewport)
  100. {
  101. Move (0, 0);
  102. Driver.SetAttribute (GetNormalColor ());
  103. for (var i = 0; i < Frame.Width; i++)
  104. {
  105. Driver.AddRune ((Rune)' ');
  106. }
  107. Move (1, 0);
  108. Attribute scheme = GetNormalColor ();
  109. Driver.SetAttribute (scheme);
  110. for (var i = 0; i < Items.Length; i++)
  111. {
  112. string title = Items [i].Title;
  113. Driver.SetAttribute (DetermineColorSchemeFor (Items [i]));
  114. for (var n = 0; n < Items [i].Title.GetRuneCount (); n++)
  115. {
  116. if (title [n] == '~')
  117. {
  118. if (Items [i].IsEnabled ())
  119. {
  120. scheme = ToggleScheme (scheme);
  121. }
  122. continue;
  123. }
  124. Driver.AddRune ((Rune)title [n]);
  125. }
  126. if (i + 1 < Items.Length)
  127. {
  128. Driver.AddRune ((Rune)' ');
  129. Driver.AddRune (Glyphs.VLine);
  130. Driver.AddRune ((Rune)' ');
  131. }
  132. }
  133. }
  134. /// <summary>Removes a <see cref="StatusItem"/> at specified index of <see cref="Items"/>.</summary>
  135. /// <param name="index">The zero-based index of the item to remove.</param>
  136. /// <returns>The <see cref="StatusItem"/> removed.</returns>
  137. public StatusItem RemoveItem (int index)
  138. {
  139. List<StatusItem> itemsList = new (Items);
  140. StatusItem item = itemsList [index];
  141. itemsList.RemoveAt (index);
  142. Items = itemsList.ToArray ();
  143. SetNeedsDisplay ();
  144. return item;
  145. }
  146. private Attribute DetermineColorSchemeFor (StatusItem item)
  147. {
  148. if (item is { })
  149. {
  150. if (item.IsEnabled ())
  151. {
  152. return GetNormalColor ();
  153. }
  154. return ColorScheme.Disabled;
  155. }
  156. return GetNormalColor ();
  157. }
  158. private int GetItemTitleLength (string title)
  159. {
  160. var len = 0;
  161. foreach (char ch in title)
  162. {
  163. if (ch == '~')
  164. {
  165. continue;
  166. }
  167. len++;
  168. }
  169. return len;
  170. }
  171. private bool? InvokeItem (StatusItem itemToInvoke)
  172. {
  173. if (itemToInvoke is { Action: { } })
  174. {
  175. itemToInvoke.Action.Invoke ();
  176. return true;
  177. }
  178. return false;
  179. }
  180. private void Run (Action action)
  181. {
  182. if (action is null)
  183. {
  184. return;
  185. }
  186. Application.MainLoop.AddIdle (
  187. () =>
  188. {
  189. action ();
  190. return false;
  191. }
  192. );
  193. }
  194. private Attribute ToggleScheme (Attribute scheme)
  195. {
  196. Attribute result = scheme == ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
  197. Driver.SetAttribute (result);
  198. return result;
  199. }
  200. }