StatusBar.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. Y = SuperView != null ? SuperView.Frame.Height - 1 : Pos.AnchorEnd (1);
  84. Width = Dim.Fill ();
  85. Height = 1;
  86. Application.Resized += Application_Resized ();
  87. }
  88. private Action<Application.ResizedEventArgs> Application_Resized ()
  89. {
  90. return delegate {
  91. X = 0;
  92. Height = 1;
  93. if (SuperView == null || SuperView is Toplevel) {
  94. Y = SuperView.Frame.Height - 1;
  95. } else {
  96. //Y = Pos.Bottom (SuperView);
  97. }
  98. };
  99. }
  100. Attribute ToggleScheme (Attribute scheme)
  101. {
  102. var result = scheme == ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
  103. Driver.SetAttribute (result);
  104. return result;
  105. }
  106. ///<inheritdoc/>
  107. public override void Redraw (Rect bounds)
  108. {
  109. //if (Frame.Y != Driver.Rows - 1) {
  110. // Frame = new Rect (Frame.X, Driver.Rows - 1, Frame.Width, Frame.Height);
  111. // Y = Driver.Rows - 1;
  112. // SetNeedsDisplay ();
  113. //}
  114. Move (0, 0);
  115. Driver.SetAttribute (Colors.Menu.Normal);
  116. for (int i = 0; i < Frame.Width; i++)
  117. Driver.AddRune (' ');
  118. Move (1, 0);
  119. var scheme = ColorScheme.Normal;
  120. Driver.SetAttribute (scheme);
  121. for (int i = 0; i < Items.Length; i++) {
  122. var title = Items [i].Title;
  123. for (int n = 0; n < title.RuneCount; n++) {
  124. if (title [n] == '~') {
  125. scheme = ToggleScheme (scheme);
  126. continue;
  127. }
  128. Driver.AddRune (title [n]);
  129. }
  130. if (i + 1 < Items.Length) {
  131. Driver.AddRune (' ');
  132. Driver.AddRune (Driver.VLine);
  133. Driver.AddRune (' ');
  134. }
  135. }
  136. }
  137. ///<inheritdoc/>
  138. public override bool ProcessHotKey (KeyEvent kb)
  139. {
  140. foreach (var item in Items) {
  141. if (kb.Key == item.Shortcut) {
  142. item.Action?.Invoke ();
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. ///<inheritdoc/>
  149. public override bool MouseEvent (MouseEvent me)
  150. {
  151. if (me.Flags != MouseFlags.Button1Clicked)
  152. return false;
  153. int pos = 1;
  154. for (int i = 0; i < Items.Length; i++) {
  155. if (me.X >= pos && me.X < pos + GetItemTitleLength (Items [i].Title)) {
  156. Run (Items [i].Action);
  157. }
  158. pos += GetItemTitleLength (Items [i].Title) + 3;
  159. }
  160. return true;
  161. }
  162. int GetItemTitleLength (ustring title)
  163. {
  164. int len = 0;
  165. foreach (var ch in title) {
  166. if (ch == '~')
  167. continue;
  168. len++;
  169. }
  170. return len;
  171. }
  172. void Run (Action action)
  173. {
  174. if (action == null)
  175. return;
  176. Application.MainLoop.AddIdle (() => {
  177. action ();
  178. return false;
  179. });
  180. }
  181. /// <inheritdoc/>
  182. protected override void Dispose (bool disposing)
  183. {
  184. if (!disposedValue) {
  185. if (disposing) {
  186. Application.Resized -= Application_Resized ();
  187. }
  188. disposedValue = true;
  189. }
  190. }
  191. }
  192. }