StatusBar.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // StatusBar.cs: a statusbar for an application
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // TODO:
  8. // Add mouse support
  9. using System;
  10. using System.Collections.Generic;
  11. using NStack;
  12. namespace Terminal.Gui {
  13. /// <summary>
  14. /// <see cref="StatusItem"/> objects are contained by <see cref="StatusBar"/> <see cref="View"/>s.
  15. /// Each <see cref="StatusItem"/> has a title, a shortcut (hotkey), and an <see cref="Action"/> that will be invoked when the
  16. /// <see cref="StatusItem.Shortcut"/> is pressed.
  17. /// The <see cref="StatusItem.Shortcut"/> will be a global hotkey for the application in the current context of the screen.
  18. /// The colour of the <see cref="StatusItem.Title"/> will be changed after each ~.
  19. /// A <see cref="StatusItem.Title"/> set to `~F1~ Help` will render as *F1* using <see cref="ColorScheme.HotNormal"/> and
  20. /// *Help* as <see cref="ColorScheme.HotNormal"/>.
  21. /// </summary>
  22. public class StatusItem {
  23. /// <summary>
  24. /// Initializes a new <see cref="StatusItem"/>.
  25. /// </summary>
  26. /// <param name="shortcut">Shortcut to activate the <see cref="StatusItem"/>.</param>
  27. /// <param name="title">Title for the <see cref="StatusItem"/>.</param>
  28. /// <param name="action">Action to invoke when the <see cref="StatusItem"/> is activated.</param>
  29. public StatusItem (Key shortcut, ustring title, Action action)
  30. {
  31. Title = title ?? "";
  32. Shortcut = shortcut;
  33. Action = action;
  34. }
  35. /// <summary>
  36. /// Gets the global shortcut to invoke the action on the menu.
  37. /// </summary>
  38. public Key Shortcut { get; }
  39. /// <summary>
  40. /// Gets or sets the title.
  41. /// </summary>
  42. /// <value>The title.</value>
  43. /// <remarks>
  44. /// The colour of the <see cref="StatusItem.Title"/> will be changed after each ~.
  45. /// A <see cref="StatusItem.Title"/> set to `~F1~ Help` will render as *F1* using <see cref="ColorScheme.HotNormal"/> and
  46. /// *Help* as <see cref="ColorScheme.HotNormal"/>.
  47. /// </remarks>
  48. public ustring Title { get; set; }
  49. /// <summary>
  50. /// Gets or sets the action to be invoked when the statusbar item is triggered
  51. /// </summary>
  52. /// <value>Action to invoke.</value>
  53. public Action Action { get; }
  54. };
  55. /// <summary>
  56. /// A status bar is a <see cref="View"/> that snaps to the bottom of a <see cref="Toplevel"/> displaying set of <see cref="StatusItem"/>s.
  57. /// The <see cref="StatusBar"/> should be context sensitive. This means, if the main menu and an open text editor are visible, the items probably shown will
  58. /// be ~F1~ Help ~F2~ Save ~F3~ Load. While a dialog to ask a file to load is executed, the remaining commands will probably be ~F1~ Help.
  59. /// So for each context must be a new instance of a statusbar.
  60. /// </summary>
  61. public class StatusBar : View {
  62. bool disposedValue;
  63. /// <summary>
  64. /// The items that compose the <see cref="StatusBar"/>
  65. /// </summary>
  66. public StatusItem [] Items { get; set; }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="StatusBar"/> class.
  69. /// </summary>
  70. public StatusBar () : this (items: new StatusItem [] { }) { }
  71. /// <summary>
  72. /// Initializes a new instance of the <see cref="StatusBar"/> class with the specified set of <see cref="StatusItem"/>s.
  73. /// The <see cref="StatusBar"/> will be drawn on the lowest line of the terminal or <see cref="View.SuperView"/> (if not null).
  74. /// </summary>
  75. /// <param name="items">A list of statusbar items.</param>
  76. public StatusBar (StatusItem [] items) : base ()
  77. {
  78. Items = items;
  79. CanFocus = false;
  80. ColorScheme = Colors.Menu;
  81. X = 0;
  82. Y = Pos.AnchorEnd (1);
  83. Width = Dim.Fill ();
  84. Height = 1;
  85. }
  86. static ustring shortcutDelimiter = "-";
  87. /// <summary>
  88. /// Used for change the shortcut delimiter separator.
  89. /// </summary>
  90. public static ustring ShortcutDelimiter {
  91. get => shortcutDelimiter;
  92. set {
  93. if (shortcutDelimiter != value) {
  94. shortcutDelimiter = value == ustring.Empty ? " " : value;
  95. }
  96. }
  97. }
  98. Attribute ToggleScheme (Attribute scheme)
  99. {
  100. var result = scheme == ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
  101. Driver.SetAttribute (result);
  102. return result;
  103. }
  104. ///<inheritdoc/>
  105. public override void Redraw (Rect bounds)
  106. {
  107. Move (0, 0);
  108. Driver.SetAttribute (GetNormalColor ());
  109. for (int i = 0; i < Frame.Width; i++)
  110. Driver.AddRune (' ');
  111. Move (1, 0);
  112. var scheme = GetNormalColor ();
  113. Driver.SetAttribute (scheme);
  114. for (int i = 0; i < Items.Length; i++) {
  115. var title = Items [i].Title.ToString ();
  116. for (int n = 0; n < Items [i].Title.RuneCount; n++) {
  117. if (title [n] == '~') {
  118. scheme = ToggleScheme (scheme);
  119. continue;
  120. }
  121. Driver.AddRune (title [n]);
  122. }
  123. if (i + 1 < Items.Length) {
  124. Driver.AddRune (' ');
  125. Driver.AddRune (Driver.VLine);
  126. Driver.AddRune (' ');
  127. }
  128. }
  129. }
  130. ///<inheritdoc/>
  131. public override bool ProcessHotKey (KeyEvent kb)
  132. {
  133. foreach (var item in Items) {
  134. if (kb.Key == item.Shortcut) {
  135. Run (item.Action);
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. ///<inheritdoc/>
  142. public override bool MouseEvent (MouseEvent me)
  143. {
  144. if (me.Flags != MouseFlags.Button1Clicked)
  145. return false;
  146. int pos = 1;
  147. for (int i = 0; i < Items.Length; i++) {
  148. if (me.X >= pos && me.X < pos + GetItemTitleLength (Items [i].Title)) {
  149. Run (Items [i].Action);
  150. break;
  151. }
  152. pos += GetItemTitleLength (Items [i].Title) + 3;
  153. }
  154. return true;
  155. }
  156. int GetItemTitleLength (ustring title)
  157. {
  158. int len = 0;
  159. foreach (var ch in title) {
  160. if (ch == '~')
  161. continue;
  162. len++;
  163. }
  164. return len;
  165. }
  166. void Run (Action action)
  167. {
  168. if (action == null)
  169. return;
  170. Application.MainLoop.AddIdle (() => {
  171. action ();
  172. return false;
  173. });
  174. }
  175. ///<inheritdoc/>
  176. public override bool OnEnter (View view)
  177. {
  178. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  179. return base.OnEnter (view);
  180. }
  181. /// <summary>
  182. /// Inserts a <see cref="StatusItem"/> in the specified index of <see cref="Items"/>.
  183. /// </summary>
  184. /// <param name="index">The zero-based index at which item should be inserted.</param>
  185. /// <param name="item">The item to insert.</param>
  186. public void AddItemAt (int index, StatusItem item)
  187. {
  188. var itemsList = new List<StatusItem> (Items);
  189. itemsList.Insert (index, item);
  190. Items = itemsList.ToArray ();
  191. SetNeedsDisplay ();
  192. }
  193. /// <summary>
  194. /// Removes a <see cref="StatusItem"/> at specified index of <see cref="Items"/>.
  195. /// </summary>
  196. /// <param name="index">The zero-based index of the item to remove.</param>
  197. /// <returns>The <see cref="StatusItem"/> removed.</returns>
  198. public StatusItem RemoveItem (int index)
  199. {
  200. var itemsList = new List<StatusItem> (Items);
  201. var item = itemsList [index];
  202. itemsList.RemoveAt (index);
  203. Items = itemsList.ToArray ();
  204. SetNeedsDisplay ();
  205. return item;
  206. }
  207. }
  208. }