StatusBar.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /// A statusbar item has a title, a shortcut aka hotkey, and an action to execute on activation.
  14. /// Such an item is ment to be as part of the global hotkeys of the application, which are available in the current context of the screen.
  15. /// The colour of the text will be changed after each ~. Having an statusbar item with a text of `~F1~ Help` will draw *F1* as shortcut and
  16. /// *Help* as standard text.
  17. /// </summary>
  18. public class StatusItem {
  19. /// <summary>
  20. /// Initializes a new <see cref="T:Terminal.Gui.StatusItem"/>.
  21. /// </summary>
  22. /// <param name="shortcut">Shortcut to activate the item.</param>
  23. /// <param name="title">Title for the statusbar item.</param>
  24. /// <param name="action">Action to invoke when the staturbar item is activated.</param>
  25. public StatusItem (Key shortcut, ustring title, Action action)
  26. {
  27. Title = title ?? "";
  28. Shortcut = shortcut;
  29. Action = action;
  30. }
  31. /// <summary>
  32. /// This is the global setting that can be used as a global shortcut to invoke the action on the menu.
  33. /// </summary>
  34. public Key Shortcut { get; }
  35. /// <summary>
  36. /// Gets or sets the title.
  37. /// </summary>
  38. /// <value>The title.</value>
  39. public ustring Title { get; }
  40. /// <summary>
  41. /// Gets or sets the action to be invoked when the statusbar item is triggered
  42. /// </summary>
  43. /// <value>Method to invoke.</value>
  44. public Action Action { get; }
  45. };
  46. /// <summary>
  47. /// A statusbar for your application.
  48. /// The statusbar should be context sensitive. This means, if the main menu and an open text editor are visible, the items probably shown will
  49. /// 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.
  50. /// So for each context must be a new instance of a statusbar.
  51. /// </summary>
  52. public class StatusBar : View {
  53. // After attempting to implement this, I noticed that there are hard dependencies
  54. // on StatusBar and MenuBars within core. They will need to be refactored for having the
  55. // StatusBar work at the top
  56. #if SNAP_TO_TOP
  57. /// <summary>
  58. /// The style supported by StatusBar
  59. /// </summary>
  60. public enum StatusBarStyle {
  61. Default = 0,
  62. /// <summary>
  63. /// The StatusBar will snap at the the bottom line of the Parent view.
  64. /// If the console window is made larger while the app is runing, the StatusBar
  65. /// will continue to snap to the bottom line of the Parent, staying visible.
  66. /// On consoles that support resizing of console apps (e.g. Windows Terminal and ConEmu),
  67. /// if the console window is subsequently made shorter, the status bar will remain visible
  68. /// as the Parent view resizes. If Parent is null, the StatusBar will snap to the bottom line
  69. /// of the console window.
  70. /// This is the default.
  71. /// </summary>
  72. SnapToBottom = Default,
  73. /// <summary>
  74. /// The StatusBar will act identically to MenuBar, snapping to the first line of the
  75. /// console window.
  76. /// </summary>
  77. SnapToTop = 1,
  78. }
  79. public StatusBarStyle Style { get; set; } = StatusBarStyle.Default;
  80. #endif
  81. /// <summary>
  82. /// The parent view of the StatusBar.
  83. /// </summary>
  84. public View Parent { get; set; }
  85. /// <summary>
  86. /// The items that compose the StatusBar
  87. /// </summary>
  88. public StatusItem [] Items { get; set; }
  89. /// <summary>
  90. /// Initializes a new instance of the <see cref="T:Terminal.Gui.StatusBar"/> class with the specified set of statusbar items.
  91. /// It will be drawn in the lowest line of the terminal.
  92. /// </summary>
  93. /// <param name="items">A list of statusbar items.</param>
  94. public StatusBar (StatusItem [] items) : base ()
  95. {
  96. Width = Dim.Fill ();
  97. Height = 1;
  98. Items = items;
  99. CanFocus = false;
  100. ColorScheme = Colors.Menu;
  101. Application.Loaded += (sender, e) => {
  102. X = 0;
  103. Height = 1;
  104. #if SNAP_TO_TOP
  105. switch (Style) {
  106. case StatusBarStyle.SnapToTop:
  107. X = 0;
  108. Y = 0;
  109. break;
  110. case StatusBarStyle.SnapToBottom:
  111. #endif
  112. if (Parent == null) {
  113. Y = e.Rows - 1;
  114. } else {
  115. Y = Pos.Bottom (Parent);
  116. }
  117. #if SNAP_TO_TOP
  118. break;
  119. }
  120. #endif
  121. };
  122. }
  123. Attribute ToggleScheme (Attribute scheme)
  124. {
  125. var result = scheme == ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
  126. Driver.SetAttribute (result);
  127. return result;
  128. }
  129. ///<inheritdoc cref="Redraw"/>
  130. public override void Redraw (Rect region)
  131. {
  132. if (Frame.Y != Driver.Rows - 1) {
  133. Frame = new Rect (Frame.X, Driver.Rows - 1, Frame.Width, Frame.Height);
  134. Y = Driver.Rows - 1;
  135. SetNeedsDisplay ();
  136. }
  137. Move (0, 0);
  138. Driver.SetAttribute (ColorScheme.Normal);
  139. for (int i = 0; i < Frame.Width; i++)
  140. Driver.AddRune (' ');
  141. Move (1, 0);
  142. var scheme = ColorScheme.Normal;
  143. Driver.SetAttribute (scheme);
  144. for (int i = 0; i < Items.Length; i++) {
  145. var title = Items [i].Title;
  146. for (int n = 0; n < title.Length; n++) {
  147. if (title [n] == '~') {
  148. scheme = ToggleScheme (scheme);
  149. continue;
  150. }
  151. Driver.AddRune (title [n]);
  152. }
  153. Driver.AddRune (' ');
  154. }
  155. }
  156. ///<inheritdoc cref="ProcessHotKey"/>
  157. public override bool ProcessHotKey (KeyEvent kb)
  158. {
  159. foreach (var item in Items) {
  160. if (kb.Key == item.Shortcut) {
  161. if (item.Action != null) item.Action ();
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. }
  168. }