StatusBar.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 NStack;
  11. namespace Terminal.Gui {
  12. /// <summary>
  13. /// <see cref="StatusItem"/> objects are contained by <see cref="StatusBar"/> <see cref="View"/>s.
  14. /// Each <see cref="StatusItem"/> has a title, a shortcut (hotkey), and an <see cref="Action"/> that will be invoked when the
  15. /// <see cref="StatusItem.Shortcut"/> is pressed.
  16. /// The <see cref="StatusItem.Shortcut"/> will be a global hotkey for the application in the current context of the screen.
  17. /// The colour of the <see cref="StatusItem.Title"/> will be changed after each ~.
  18. /// A <see cref="StatusItem.Title"/> set to `~F1~ Help` will render as *F1* using <see cref="ColorScheme.HotNormal"/> and
  19. /// *Help* as <see cref="ColorScheme.HotNormal"/>.
  20. /// </summary>
  21. public class StatusItem {
  22. /// <summary>
  23. /// Initializes a new <see cref="StatusItem"/>.
  24. /// </summary>
  25. /// <param name="shortcut">Shortcut to activate the <see cref="StatusItem"/>.</param>
  26. /// <param name="title">Title for the <see cref="StatusItem"/>.</param>
  27. /// <param name="action">Action to invoke when the <see cref="StatusItem"/> is activated.</param>
  28. public StatusItem (Key shortcut, ustring title, Action action)
  29. {
  30. Title = title ?? "";
  31. Shortcut = shortcut;
  32. Action = action;
  33. }
  34. /// <summary>
  35. /// Gets the global shortcut to invoke the action on the menu.
  36. /// </summary>
  37. public Key Shortcut { get; }
  38. /// <summary>
  39. /// Gets or sets the title.
  40. /// </summary>
  41. /// <value>The title.</value>
  42. /// <remarks>
  43. /// The colour of the <see cref="StatusItem.Title"/> will be changed after each ~.
  44. /// A <see cref="StatusItem.Title"/> set to `~F1~ Help` will render as *F1* using <see cref="ColorScheme.HotNormal"/> and
  45. /// *Help* as <see cref="ColorScheme.HotNormal"/>.
  46. /// </remarks>
  47. public ustring Title { get; set; }
  48. /// <summary>
  49. /// Gets or sets the action to be invoked when the statusbar item is triggered
  50. /// </summary>
  51. /// <value>Action to invoke.</value>
  52. public Action Action { get; }
  53. };
  54. /// <summary>
  55. /// 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.
  56. /// 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
  57. /// 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.
  58. /// So for each context must be a new instance of a statusbar.
  59. /// </summary>
  60. public class StatusBar : View {
  61. bool disposedValue;
  62. /// <summary>
  63. /// The items that compose the <see cref="StatusBar"/>
  64. /// </summary>
  65. public StatusItem [] Items { get; set; }
  66. /// <summary>
  67. /// Initializes a new instance of the <see cref="StatusBar"/> class.
  68. /// </summary>
  69. public StatusBar () : this (items: new StatusItem [] { }) { }
  70. /// <summary>
  71. /// Initializes a new instance of the <see cref="StatusBar"/> class with the specified set of <see cref="StatusItem"/>s.
  72. /// The <see cref="StatusBar"/> will be drawn on the lowest line of the terminal or <see cref="View.SuperView"/> (if not null).
  73. /// </summary>
  74. /// <param name="items">A list of statusbar items.</param>
  75. public StatusBar (StatusItem [] items) : base ()
  76. {
  77. Width = Dim.Fill ();
  78. Height = 1;
  79. Items = items;
  80. CanFocus = false;
  81. ColorScheme = Colors.Menu;
  82. X = 0;
  83. Width = Dim.Fill ();
  84. Height = 1;
  85. Initialized += StatusBar_Initialized;
  86. Application.Resized += Application_Resized ();
  87. }
  88. private void StatusBar_Initialized (object sender, EventArgs e)
  89. {
  90. Y = SuperView.Frame.Height - 1;
  91. }
  92. private Action<Application.ResizedEventArgs> Application_Resized ()
  93. {
  94. return delegate {
  95. X = 0;
  96. Height = 1;
  97. if (SuperView != null || SuperView is Toplevel) {
  98. Y = SuperView.Frame.Height - (Visible ? 1 : 0);
  99. } else {
  100. //Y = Pos.Bottom (SuperView);
  101. }
  102. };
  103. }
  104. Attribute ToggleScheme (Attribute scheme)
  105. {
  106. var result = scheme == ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
  107. Driver.SetAttribute (result);
  108. return result;
  109. }
  110. ///<inheritdoc/>
  111. public override void Redraw (Rect bounds)
  112. {
  113. //if (Frame.Y != Driver.Rows - 1) {
  114. // Frame = new Rect (Frame.X, Driver.Rows - 1, Frame.Width, Frame.Height);
  115. // Y = Driver.Rows - 1;
  116. // SetNeedsDisplay ();
  117. //}
  118. Move (0, 0);
  119. Driver.SetAttribute (Colors.Menu.Normal);
  120. for (int i = 0; i < Frame.Width; i++)
  121. Driver.AddRune (' ');
  122. Move (1, 0);
  123. var scheme = ColorScheme.Normal;
  124. Driver.SetAttribute (scheme);
  125. for (int i = 0; i < Items.Length; i++) {
  126. var title = Items [i].Title.ToString ();
  127. for (int n = 0; n < Items [i].Title.RuneCount; n++) {
  128. if (title [n] == '~') {
  129. scheme = ToggleScheme (scheme);
  130. continue;
  131. }
  132. Driver.AddRune (title [n]);
  133. }
  134. if (i + 1 < Items.Length) {
  135. Driver.AddRune (' ');
  136. Driver.AddRune (Driver.VLine);
  137. Driver.AddRune (' ');
  138. }
  139. }
  140. }
  141. ///<inheritdoc/>
  142. public override bool ProcessHotKey (KeyEvent kb)
  143. {
  144. foreach (var item in Items) {
  145. if (kb.Key == item.Shortcut) {
  146. item.Action?.Invoke ();
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. ///<inheritdoc/>
  153. public override bool MouseEvent (MouseEvent me)
  154. {
  155. if (me.Flags != MouseFlags.Button1Clicked)
  156. return false;
  157. int pos = 1;
  158. for (int i = 0; i < Items.Length; i++) {
  159. if (me.X >= pos && me.X < pos + GetItemTitleLength (Items [i].Title)) {
  160. Run (Items [i].Action);
  161. }
  162. pos += GetItemTitleLength (Items [i].Title) + 3;
  163. }
  164. return true;
  165. }
  166. int GetItemTitleLength (ustring title)
  167. {
  168. int len = 0;
  169. foreach (var ch in title) {
  170. if (ch == '~')
  171. continue;
  172. len++;
  173. }
  174. return len;
  175. }
  176. void Run (Action action)
  177. {
  178. if (action == null)
  179. return;
  180. Application.MainLoop.AddIdle (() => {
  181. action ();
  182. return false;
  183. });
  184. }
  185. /// <inheritdoc/>
  186. protected override void Dispose (bool disposing)
  187. {
  188. if (!disposedValue) {
  189. if (disposing) {
  190. Application.Resized -= Application_Resized ();
  191. }
  192. disposedValue = true;
  193. }
  194. }
  195. ///<inheritdoc/>
  196. public override bool OnEnter (View view)
  197. {
  198. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  199. return base.OnEnter (view);
  200. }
  201. }
  202. }