StatusBar.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 System.Text;
  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, string 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; set; }
  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 string 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 string shortcutDelimiter = "-";
  106. /// <summary>
  107. /// Used for change the shortcut delimiter separator.
  108. /// </summary>
  109. public static string ShortcutDelimiter {
  110. get => shortcutDelimiter;
  111. set {
  112. if (shortcutDelimiter != value) {
  113. shortcutDelimiter = value == string.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 OnDrawContent (Rect contentArea)
  135. {
  136. Move (0, 0);
  137. Driver.SetAttribute (GetNormalColor ());
  138. for (int i = 0; i < Frame.Width; i++) {
  139. Driver.AddRune ((Rune)' ');
  140. }
  141. Move (1, 0);
  142. var scheme = GetNormalColor ();
  143. Driver.SetAttribute (scheme);
  144. for (int i = 0; i < Items.Length; i++) {
  145. var title = Items [i].Title;
  146. Driver.SetAttribute (DetermineColorSchemeFor (Items [i]));
  147. for (int n = 0; n < Items [i].Title.GetRuneCount (); n++) {
  148. if (title [n] == '~') {
  149. if (Items [i].IsEnabled ()) {
  150. scheme = ToggleScheme (scheme);
  151. }
  152. continue;
  153. }
  154. Driver.AddRune ((Rune)title [n]);
  155. }
  156. if (i + 1 < Items.Length) {
  157. Driver.AddRune ((Rune)' ');
  158. Driver.AddRune (CM.Glyphs.VLine);
  159. Driver.AddRune ((Rune)' ');
  160. }
  161. }
  162. }
  163. ///<inheritdoc/>
  164. public override bool ProcessHotKey (KeyEvent kb)
  165. {
  166. foreach (var item in Items) {
  167. if (kb.Key == item.Shortcut) {
  168. if (item.IsEnabled ()) {
  169. Run (item.Action);
  170. }
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. ///<inheritdoc/>
  177. public override bool MouseEvent (MouseEvent me)
  178. {
  179. if (me.Flags != MouseFlags.Button1Clicked)
  180. return false;
  181. int pos = 1;
  182. for (int i = 0; i < Items.Length; i++) {
  183. if (me.X >= pos && me.X < pos + GetItemTitleLength (Items [i].Title)) {
  184. var item = Items [i];
  185. if (item.IsEnabled ()) {
  186. Run (item.Action);
  187. }
  188. break;
  189. }
  190. pos += GetItemTitleLength (Items [i].Title) + 3;
  191. }
  192. return true;
  193. }
  194. int GetItemTitleLength (string title)
  195. {
  196. int len = 0;
  197. foreach (var ch in title) {
  198. if (ch == '~')
  199. continue;
  200. len++;
  201. }
  202. return len;
  203. }
  204. void Run (Action action)
  205. {
  206. if (action == null)
  207. return;
  208. Application.MainLoop.AddIdle (() => {
  209. action ();
  210. return false;
  211. });
  212. }
  213. ///<inheritdoc/>
  214. public override bool OnEnter (View view)
  215. {
  216. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  217. return base.OnEnter (view);
  218. }
  219. /// <summary>
  220. /// Inserts a <see cref="StatusItem"/> in the specified index of <see cref="Items"/>.
  221. /// </summary>
  222. /// <param name="index">The zero-based index at which item should be inserted.</param>
  223. /// <param name="item">The item to insert.</param>
  224. public void AddItemAt (int index, StatusItem item)
  225. {
  226. var itemsList = new List<StatusItem> (Items);
  227. itemsList.Insert (index, item);
  228. Items = itemsList.ToArray ();
  229. SetNeedsDisplay ();
  230. }
  231. /// <summary>
  232. /// Removes a <see cref="StatusItem"/> at specified index of <see cref="Items"/>.
  233. /// </summary>
  234. /// <param name="index">The zero-based index of the item to remove.</param>
  235. /// <returns>The <see cref="StatusItem"/> removed.</returns>
  236. public StatusItem RemoveItem (int index)
  237. {
  238. var itemsList = new List<StatusItem> (Items);
  239. var item = itemsList [index];
  240. itemsList.RemoveAt (index);
  241. Items = itemsList.ToArray ();
  242. SetNeedsDisplay ();
  243. return item;
  244. }
  245. }
  246. }