StatusBar.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 ~ (can be customized using <see cref="HotTextSpecifier"/>).
  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.Normal"/>.
  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. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  30. public StatusItem (Key shortcut, ustring title, Action action, Func<bool> canExecute = null)
  31. {
  32. Title = title ?? "";
  33. Shortcut = shortcut;
  34. Action = action;
  35. CanExecute = canExecute;
  36. }
  37. /// <summary>
  38. /// Gets the global shortcut to invoke the action on the menu.
  39. /// </summary>
  40. public Key Shortcut { get; }
  41. /// <summary>
  42. /// Gets or sets the title.
  43. /// </summary>
  44. /// <value>The title.</value>
  45. /// <remarks>
  46. /// The colour of the <see cref="StatusItem.Title"/> will be changed after each ~.
  47. /// A <see cref="StatusItem.Title"/> set to `~F1~ Help` will render as *F1* using <see cref="ColorScheme.HotNormal"/> and
  48. /// *Help* as <see cref="ColorScheme.HotNormal"/>.
  49. /// </remarks>
  50. public ustring Title { get; set; }
  51. /// <summary>
  52. /// Gets or sets the action to be invoked when the statusbar item is triggered
  53. /// </summary>
  54. /// <value>Action to invoke.</value>
  55. public Action Action { get; set; }
  56. /// <summary>
  57. /// Gets or sets the action to be invoked to determine if the <see cref="StatusItem"/> can be triggered.
  58. /// If <see cref="CanExecute"/> returns <see langword="true"/> the status item will be enabled. Otherwise, it will be disabled.
  59. /// </summary>
  60. /// <value>Function to determine if the action is can be executed or not.</value>
  61. public Func<bool> CanExecute { get; set; }
  62. /// <summary>
  63. /// Gets or sets the rune that toggles the text color between <see cref="ColorScheme.Normal"/> and <see cref="ColorScheme.HotNormal"/>.
  64. /// The default value is '~'.
  65. /// Therefore, '~F1~ Help' will be rendered as 'F1' using <see cref="ColorScheme.HotNormal"/> and 'Help' using <see cref="ColorScheme.Normal"/>.
  66. /// In order to use '~' as part of the title (e.g., to denote the home directory as a part of the current directory),
  67. /// <see cref="HotTextSpecifier"/> should be changed to a different rune.
  68. /// </summary>
  69. public Rune HotTextSpecifier { get; set; } = '~';
  70. /// <summary>
  71. /// Returns <see langword="true"/> if the status item is enabled. This method is a wrapper around <see cref="CanExecute"/>.
  72. /// </summary>
  73. public bool IsEnabled ()
  74. {
  75. return CanExecute == null ? true : CanExecute ();
  76. }
  77. /// <summary>
  78. /// Gets or sets arbitrary data for the status item.
  79. /// </summary>
  80. /// <remarks>This property is not used internally.</remarks>
  81. public object Data { get; set; }
  82. };
  83. /// <summary>
  84. /// 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.
  85. /// 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
  86. /// 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.
  87. /// So for each context must be a new instance of a statusbar.
  88. /// </summary>
  89. public class StatusBar : View {
  90. /// <summary>
  91. /// The items that compose the <see cref="StatusBar"/>
  92. /// </summary>
  93. public StatusItem [] Items { get; set; }
  94. /// <summary>
  95. /// Initializes a new instance of the <see cref="StatusBar"/> class.
  96. /// </summary>
  97. public StatusBar () : this (items: new StatusItem [] { }) { }
  98. /// <summary>
  99. /// Initializes a new instance of the <see cref="StatusBar"/> class with the specified set of <see cref="StatusItem"/>s.
  100. /// The <see cref="StatusBar"/> will be drawn on the lowest line of the terminal or <see cref="View.SuperView"/> (if not null).
  101. /// </summary>
  102. /// <param name="items">A list of statusbar items.</param>
  103. public StatusBar (StatusItem [] items) : base ()
  104. {
  105. Items = items;
  106. CanFocus = false;
  107. ColorScheme = Colors.Menu;
  108. X = 0;
  109. Y = Pos.AnchorEnd (1);
  110. Width = Dim.Fill ();
  111. Height = 1;
  112. }
  113. static ustring shortcutDelimiter = "-";
  114. /// <summary>
  115. /// Used for change the shortcut delimiter separator.
  116. /// </summary>
  117. public static ustring ShortcutDelimiter {
  118. get => shortcutDelimiter;
  119. set {
  120. if (shortcutDelimiter != value) {
  121. shortcutDelimiter = value == ustring.Empty ? " " : value;
  122. }
  123. }
  124. }
  125. Attribute ToggleScheme (Attribute scheme)
  126. {
  127. var result = scheme == ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
  128. Driver.SetAttribute (result);
  129. return result;
  130. }
  131. Attribute DetermineColorSchemeFor (StatusItem item)
  132. {
  133. if (item != null) {
  134. if (item.IsEnabled ()) {
  135. return GetNormalColor ();
  136. }
  137. return ColorScheme.Disabled;
  138. }
  139. return GetNormalColor ();
  140. }
  141. ///<inheritdoc/>
  142. public override void Redraw (Rect bounds)
  143. {
  144. Move (0, 0);
  145. Driver.SetAttribute (GetNormalColor ());
  146. for (int i = 0; i < Frame.Width; i++)
  147. Driver.AddRune (' ');
  148. Move (1, 0);
  149. var scheme = GetNormalColor ();
  150. Driver.SetAttribute (scheme);
  151. for (int i = 0; i < Items.Length; i++) {
  152. var title = Items [i].Title.ToString ();
  153. var hotTextSpecifier = Items [i].HotTextSpecifier;
  154. Driver.SetAttribute (DetermineColorSchemeFor (Items [i]));
  155. for (int n = 0; n < Items [i].Title.RuneCount; n++) {
  156. if (title [n] == hotTextSpecifier) {
  157. if (Items [i].IsEnabled ()) {
  158. scheme = ToggleScheme (scheme);
  159. }
  160. continue;
  161. }
  162. Driver.AddRune (title [n]);
  163. }
  164. if (i + 1 < Items.Length) {
  165. Driver.AddRune (' ');
  166. Driver.AddRune (Driver.VLine);
  167. Driver.AddRune (' ');
  168. }
  169. }
  170. }
  171. ///<inheritdoc/>
  172. public override bool ProcessHotKey (KeyEvent kb)
  173. {
  174. foreach (var item in Items) {
  175. if (kb.Key == item.Shortcut) {
  176. if (item.IsEnabled ()) {
  177. Run (item.Action);
  178. }
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. ///<inheritdoc/>
  185. public override bool MouseEvent (MouseEvent me)
  186. {
  187. if (me.Flags != MouseFlags.Button1Clicked)
  188. return false;
  189. int pos = 1;
  190. for (int i = 0; i < Items.Length; i++) {
  191. if (me.X >= pos && me.X < pos + GetItemTitleLength (Items [i])) {
  192. var item = Items [i];
  193. if (item.IsEnabled ()) {
  194. Run (item.Action);
  195. }
  196. break;
  197. }
  198. pos += GetItemTitleLength (Items [i]) + 3;
  199. }
  200. return true;
  201. }
  202. int GetItemTitleLength (StatusItem item)
  203. {
  204. int len = 0;
  205. foreach (var ch in item.Title) {
  206. if (ch == item.HotTextSpecifier)
  207. continue;
  208. len++;
  209. }
  210. return len;
  211. }
  212. void Run (Action action)
  213. {
  214. if (action == null)
  215. return;
  216. Application.MainLoop.AddIdle (() => {
  217. action ();
  218. return false;
  219. });
  220. }
  221. ///<inheritdoc/>
  222. public override bool OnEnter (View view)
  223. {
  224. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  225. return base.OnEnter (view);
  226. }
  227. /// <summary>
  228. /// Inserts a <see cref="StatusItem"/> in the specified index of <see cref="Items"/>.
  229. /// </summary>
  230. /// <param name="index">The zero-based index at which item should be inserted.</param>
  231. /// <param name="item">The item to insert.</param>
  232. public void AddItemAt (int index, StatusItem item)
  233. {
  234. var itemsList = new List<StatusItem> (Items);
  235. itemsList.Insert (index, item);
  236. Items = itemsList.ToArray ();
  237. SetNeedsDisplay ();
  238. }
  239. /// <summary>
  240. /// Removes a <see cref="StatusItem"/> at specified index of <see cref="Items"/>.
  241. /// </summary>
  242. /// <param name="index">The zero-based index of the item to remove.</param>
  243. /// <returns>The <see cref="StatusItem"/> removed.</returns>
  244. public StatusItem RemoveItem (int index)
  245. {
  246. var itemsList = new List<StatusItem> (Items);
  247. var item = itemsList [index];
  248. itemsList.RemoveAt (index);
  249. Items = itemsList.ToArray ();
  250. SetNeedsDisplay ();
  251. return item;
  252. }
  253. }
  254. }