StatusBar.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. public View Parent { get; set; }
  82. public StatusItem [] Items { get; set; }
  83. /// <summary>
  84. /// Initializes a new instance of the <see cref="T:Terminal.Gui.StatusBar"/> class with the specified set of statusbar items.
  85. /// It will be drawn in the lowest line of the terminal.
  86. /// </summary>
  87. /// <param name="items">A list of statusbar items.</param>
  88. public StatusBar (StatusItem [] items) : base ()
  89. {
  90. Width = Dim.Fill ();
  91. Height = 1;
  92. Items = items;
  93. CanFocus = false;
  94. ColorScheme = Colors.Menu;
  95. Application.OnLoad += () => {
  96. X = 0;
  97. Height = 1;
  98. #if SNAP_TO_TOP
  99. switch (Style) {
  100. case StatusBarStyle.SnapToTop:
  101. X = 0;
  102. Y = 0;
  103. break;
  104. case StatusBarStyle.SnapToBottom:
  105. #endif
  106. if (Parent == null) {
  107. Y = Application.Driver.Rows - 1; // TODO: using internals of Application
  108. } else {
  109. Y = Pos.Bottom (Parent);
  110. }
  111. #if SNAP_TO_TOP
  112. break;
  113. }
  114. #endif
  115. };
  116. }
  117. Attribute ToggleScheme (Attribute scheme)
  118. {
  119. var result = scheme == ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
  120. Driver.SetAttribute (result);
  121. return result;
  122. }
  123. public override void Redraw (Rect region)
  124. {
  125. if (Frame.Y != Driver.Rows - 1) {
  126. Frame = new Rect (Frame.X, Driver.Rows - 1, Frame.Width, Frame.Height);
  127. Y = Driver.Rows - 1;
  128. SetNeedsDisplay ();
  129. }
  130. Move (0, 0);
  131. Driver.SetAttribute (ColorScheme.Normal);
  132. for (int i = 0; i < Frame.Width; i++)
  133. Driver.AddRune (' ');
  134. Move (1, 0);
  135. var scheme = ColorScheme.Normal;
  136. Driver.SetAttribute (scheme);
  137. for (int i = 0; i < Items.Length; i++) {
  138. var title = Items [i].Title;
  139. for (int n = 0; n < title.Length; n++) {
  140. if (title [n] == '~') {
  141. scheme = ToggleScheme (scheme);
  142. continue;
  143. }
  144. Driver.AddRune (title [n]);
  145. }
  146. Driver.AddRune (' ');
  147. }
  148. }
  149. public override bool ProcessHotKey (KeyEvent kb)
  150. {
  151. foreach (var item in Items) {
  152. if (kb.Key == item.Shortcut) {
  153. if (item.Action != null) item.Action ();
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. }
  160. }