StatusBar.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /// <summary>
  62. /// The items that compose the <see cref="StatusBar"/>
  63. /// </summary>
  64. public StatusItem [] Items { get; set; }
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="StatusBar"/> class.
  67. /// </summary>
  68. public StatusBar () : this (items: new StatusItem [] { }) { }
  69. /// <summary>
  70. /// Initializes a new instance of the <see cref="StatusBar"/> class with the specified set of <see cref="StatusItem"/>s.
  71. /// The <see cref="StatusBar"/> will be drawn on the lowest line of the terminal or <see cref="View.SuperView"/> (if not null).
  72. /// </summary>
  73. /// <param name="items">A list of statusbar items.</param>
  74. public StatusBar (StatusItem [] items) : base ()
  75. {
  76. Width = Dim.Fill ();
  77. Height = 1;
  78. Items = items;
  79. CanFocus = false;
  80. ColorScheme = Colors.Menu;
  81. X = 0;
  82. Y = Driver.Rows - 1;
  83. Width = Dim.Fill ();
  84. Height = 1;
  85. Application.Resized += (e) => {
  86. X = 0;
  87. Height = 1;
  88. if (SuperView == null || SuperView == Application.Top) {
  89. Y = Driver.Rows - 1;
  90. } else {
  91. //Y = Pos.Bottom (SuperView);
  92. }
  93. };
  94. }
  95. Attribute ToggleScheme (Attribute scheme)
  96. {
  97. var result = scheme == ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
  98. Driver.SetAttribute (result);
  99. return result;
  100. }
  101. ///<inheritdoc/>
  102. public override void Redraw (Rect bounds)
  103. {
  104. //if (Frame.Y != Driver.Rows - 1) {
  105. // Frame = new Rect (Frame.X, Driver.Rows - 1, Frame.Width, Frame.Height);
  106. // Y = Driver.Rows - 1;
  107. // SetNeedsDisplay ();
  108. //}
  109. Move (0, 0);
  110. Driver.SetAttribute (ColorScheme.Normal);
  111. for (int i = 0; i < Frame.Width; i++)
  112. Driver.AddRune (' ');
  113. Move (1, 0);
  114. var scheme = ColorScheme.Normal;
  115. Driver.SetAttribute (scheme);
  116. for (int i = 0; i < Items.Length; i++) {
  117. var title = Items [i].Title;
  118. for (int n = 0; n < title.Length; n++) {
  119. if (title [n] == '~') {
  120. scheme = ToggleScheme (scheme);
  121. continue;
  122. }
  123. Driver.AddRune (title [n]);
  124. }
  125. if (i + 1 < Items.Length) {
  126. Driver.AddRune (' ');
  127. Driver.AddRune (Driver.VLine);
  128. Driver.AddRune (' ');
  129. }
  130. }
  131. }
  132. ///<inheritdoc/>
  133. public override bool ProcessHotKey (KeyEvent kb)
  134. {
  135. foreach (var item in Items) {
  136. if (kb.Key == item.Shortcut) {
  137. item.Action?.Invoke ();
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. ///<inheritdoc/>
  144. public override bool MouseEvent (MouseEvent me)
  145. {
  146. if (me.Flags != MouseFlags.Button1Clicked)
  147. return false;
  148. int pos = 1;
  149. for (int i = 0; i < Items.Length; i++) {
  150. if (me.X >= pos && me.X < pos + GetItemTitleLength (Items [i].Title)) {
  151. Run (Items [i].Action);
  152. }
  153. pos += GetItemTitleLength (Items [i].Title) + 3;
  154. }
  155. return true;
  156. }
  157. int GetItemTitleLength (ustring title)
  158. {
  159. int len = 0;
  160. foreach (var ch in title) {
  161. if (ch == '~')
  162. continue;
  163. len++;
  164. }
  165. return len;
  166. }
  167. void Run (Action action)
  168. {
  169. if (action == null)
  170. return;
  171. Application.MainLoop.AddIdle (() => {
  172. action ();
  173. return false;
  174. });
  175. }
  176. }
  177. }