StatusBar.cs 8.1 KB

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