StatusBar.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. public StatusItem [] Items { get; set; }
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="T:Terminal.Gui.StatusBar"/> class with the specified set of statusbar items.
  56. /// It will be drawn in the lowest column of the terminal.
  57. /// </summary>
  58. /// <param name="items">A list of statusbar items.</param>
  59. public StatusBar (StatusItem [] items) : base ()
  60. {
  61. Width = Dim.Fill ();
  62. Height = 1;
  63. Items = items;
  64. CanFocus = false;
  65. ColorScheme = Colors.Menu;
  66. Application.OnResized += () => {
  67. this.X = Pos.Left (Application.Top);
  68. this.Y = Pos.Bottom (Application.Top);
  69. };
  70. }
  71. Attribute ToggleScheme (Attribute scheme)
  72. {
  73. var result = scheme == ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
  74. Driver.SetAttribute (result);
  75. return result;
  76. }
  77. public override void Redraw (Rect region)
  78. {
  79. if (Frame.Y != Driver.Rows - 1) {
  80. Frame = new Rect (Frame.X, Driver.Rows - 1, Frame.Width, Frame.Height);
  81. Y = Driver.Rows - 1;
  82. SetNeedsDisplay ();
  83. }
  84. Move (0, 0);
  85. Driver.SetAttribute (ColorScheme.Normal);
  86. for (int i = 0; i < Frame.Width; i++)
  87. Driver.AddRune (' ');
  88. Move (1, 0);
  89. var scheme = ColorScheme.Normal;
  90. Driver.SetAttribute (scheme);
  91. for (int i = 0; i < Items.Length; i++) {
  92. var title = Items [i].Title;
  93. for (int n = 0; n < title.Length; n++) {
  94. if (title [n] == '~') {
  95. scheme = ToggleScheme (scheme);
  96. continue;
  97. }
  98. Driver.AddRune (title [n]);
  99. }
  100. Driver.AddRune (' ');
  101. }
  102. }
  103. public override bool ProcessHotKey (KeyEvent kb)
  104. {
  105. foreach (var item in Items) {
  106. if (kb.Key == item.Shortcut) {
  107. if (item.Action != null) item.Action ();
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. }
  114. }