StatusBar.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. /// <summary>
  55. /// Gets or sets arbitrary data for the status item.
  56. /// </summary>
  57. /// <remarks>This property is not used internally.</remarks>
  58. public object Data { get; set; }
  59. };
  60. /// <summary>
  61. /// 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.
  62. /// 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
  63. /// 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.
  64. /// So for each context must be a new instance of a statusbar.
  65. /// </summary>
  66. public class StatusBar : View {
  67. bool disposedValue;
  68. /// <summary>
  69. /// The items that compose the <see cref="StatusBar"/>
  70. /// </summary>
  71. public StatusItem [] Items { get; set; }
  72. /// <summary>
  73. /// Initializes a new instance of the <see cref="StatusBar"/> class.
  74. /// </summary>
  75. public StatusBar () : this (items: new StatusItem [] { }) { }
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="StatusBar"/> class with the specified set of <see cref="StatusItem"/>s.
  78. /// The <see cref="StatusBar"/> will be drawn on the lowest line of the terminal or <see cref="View.SuperView"/> (if not null).
  79. /// </summary>
  80. /// <param name="items">A list of statusbar items.</param>
  81. public StatusBar (StatusItem [] items) : base ()
  82. {
  83. Items = items;
  84. CanFocus = false;
  85. ColorScheme = Colors.Menu;
  86. X = 0;
  87. Width = Dim.Fill ();
  88. Height = 1;
  89. Initialized += StatusBar_Initialized;
  90. Application.Resized += Application_Resized ();
  91. }
  92. private void StatusBar_Initialized (object sender, EventArgs e)
  93. {
  94. if (SuperView.Frame == Rect.Empty) {
  95. ((Toplevel)SuperView).Loaded += StatusBar_Loaded;
  96. } else {
  97. Y = Math.Max (SuperView.Frame.Height - (Visible ? 1 : 0), 0);
  98. }
  99. }
  100. private void StatusBar_Loaded ()
  101. {
  102. Y = Math.Max (SuperView.Frame.Height - (Visible ? 1 : 0), 0);
  103. ((Toplevel)SuperView).Loaded -= StatusBar_Loaded;
  104. }
  105. private Action<Application.ResizedEventArgs> Application_Resized ()
  106. {
  107. return delegate {
  108. X = 0;
  109. Height = 1;
  110. if (SuperView != null || SuperView is Toplevel) {
  111. if (Frame.Y != SuperView.Frame.Height - (Visible ? 1 : 0)) {
  112. Y = SuperView.Frame.Height - (Visible ? 1 : 0);
  113. }
  114. }
  115. };
  116. }
  117. static ustring shortcutDelimiter = "-";
  118. /// <summary>
  119. /// Used for change the shortcut delimiter separator.
  120. /// </summary>
  121. public static ustring ShortcutDelimiter {
  122. get => shortcutDelimiter;
  123. set {
  124. if (shortcutDelimiter != value) {
  125. shortcutDelimiter = value == ustring.Empty ? " " : value;
  126. }
  127. }
  128. }
  129. Attribute ToggleScheme (Attribute scheme)
  130. {
  131. var result = scheme == ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
  132. Driver.SetAttribute (result);
  133. return result;
  134. }
  135. ///<inheritdoc/>
  136. public override void Redraw (Rect bounds)
  137. {
  138. //if (Frame.Y != Driver.Rows - 1) {
  139. // Frame = new Rect (Frame.X, Driver.Rows - 1, Frame.Width, Frame.Height);
  140. // Y = Driver.Rows - 1;
  141. // SetNeedsDisplay ();
  142. //}
  143. Move (0, 0);
  144. Driver.SetAttribute (GetNormalColor ());
  145. for (int i = 0; i < Frame.Width; i++)
  146. Driver.AddRune (' ');
  147. Move (1, 0);
  148. var scheme = GetNormalColor ();
  149. Driver.SetAttribute (scheme);
  150. for (int i = 0; i < Items.Length; i++) {
  151. var title = Items [i].Title.ToString ();
  152. for (int n = 0; n < Items [i].Title.RuneCount; n++) {
  153. if (title [n] == '~') {
  154. scheme = ToggleScheme (scheme);
  155. continue;
  156. }
  157. Driver.AddRune (title [n]);
  158. }
  159. if (i + 1 < Items.Length) {
  160. Driver.AddRune (' ');
  161. Driver.AddRune (Driver.VLine);
  162. Driver.AddRune (' ');
  163. }
  164. }
  165. }
  166. ///<inheritdoc/>
  167. public override bool ProcessHotKey (KeyEvent kb)
  168. {
  169. foreach (var item in Items) {
  170. if (kb.Key == item.Shortcut) {
  171. Run (item.Action);
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. ///<inheritdoc/>
  178. public override bool MouseEvent (MouseEvent me)
  179. {
  180. if (me.Flags != MouseFlags.Button1Clicked)
  181. return false;
  182. int pos = 1;
  183. for (int i = 0; i < Items.Length; i++) {
  184. if (me.X >= pos && me.X < pos + GetItemTitleLength (Items [i].Title)) {
  185. Run (Items [i].Action);
  186. break;
  187. }
  188. pos += GetItemTitleLength (Items [i].Title) + 3;
  189. }
  190. return true;
  191. }
  192. int GetItemTitleLength (ustring title)
  193. {
  194. int len = 0;
  195. foreach (var ch in title) {
  196. if (ch == '~')
  197. continue;
  198. len++;
  199. }
  200. return len;
  201. }
  202. void Run (Action action)
  203. {
  204. if (action == null)
  205. return;
  206. Application.MainLoop.AddIdle (() => {
  207. action ();
  208. return false;
  209. });
  210. }
  211. /// <inheritdoc/>
  212. protected override void Dispose (bool disposing)
  213. {
  214. if (!disposedValue) {
  215. if (disposing) {
  216. Application.Resized -= Application_Resized ();
  217. }
  218. disposedValue = true;
  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. }