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. // Uses internals of Application
  10. using System;
  11. using NStack;
  12. namespace Terminal.Gui
  13. {
  14. /// <summary>
  15. /// A statusbar item has a title, a shortcut aka hotkey, and an action to execute on activation.
  16. /// 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.
  17. /// 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
  18. /// *Help* as standard text.
  19. /// </summary>
  20. public class StatusItem
  21. {
  22. /// <summary>
  23. /// Initializes a new <see cref="T:Terminal.Gui.StatusItem"/>.
  24. /// </summary>
  25. /// <param name="shortcut">Shortcut to activate the item.</param>
  26. /// <param name="title">Title for the statusbar item.</param>
  27. /// <param name="action">Action to invoke when the staturbar item 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. /// This is the global setting that can be used as a 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. public ustring Title { get; }
  43. /// <summary>
  44. /// Gets or sets the action to be invoked when the statusbar item is triggered
  45. /// </summary>
  46. /// <value>Method to invoke.</value>
  47. public Action Action { get; }
  48. };
  49. /// <summary>
  50. /// A statusbar for your application.
  51. /// The statusbar should be context sensitive. This means, if the main menu and an open text editor are visible, the items probably shown will
  52. /// 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.
  53. /// So for each context must be a new instance of a statusbar.
  54. /// </summary>
  55. public class StatusBar : View
  56. {
  57. public StatusItem [] Items { get; set; }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="T:Terminal.Gui.StatusBar"/> class with the specified set of statusbar items.
  60. /// It will be drawn in the lowest column of the terminal.
  61. /// </summary>
  62. /// <param name="items">A list of statusbar items.</param>
  63. public StatusBar(StatusItem [] items) : base()
  64. {
  65. X = 0;
  66. Y = Application.Driver.Rows - 1; // TODO: using internals of Application
  67. Width = Dim.Fill ();
  68. Height = 1;
  69. Items = items;
  70. CanFocus = false;
  71. ColorScheme = Colors.Menu;
  72. }
  73. Attribute ToggleScheme(Attribute scheme)
  74. {
  75. var result = scheme==ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
  76. Driver.SetAttribute(result);
  77. return result;
  78. }
  79. public override void Redraw(Rect region) {
  80. if (Frame.Y != Driver.Rows - 1) {
  81. Frame = new Rect (Frame.X, Driver.Rows - 1, Frame.Width, Frame.Height);
  82. Y = Driver.Rows - 1;
  83. SetNeedsDisplay ();
  84. }
  85. Move (0, 0);
  86. Driver.SetAttribute (ColorScheme.Normal);
  87. for (int i = 0; i < Frame.Width; i++)
  88. Driver.AddRune (' ');
  89. Move (1, 0);
  90. var scheme = ColorScheme.Normal;
  91. Driver.SetAttribute(scheme);
  92. for(int i=0; i<Items.Length; i++) {
  93. var title = Items[i].Title;
  94. for(int n=0; n<title.Length; n++) {
  95. if(title[n]=='~') {
  96. scheme = ToggleScheme(scheme);
  97. continue;
  98. }
  99. Driver.AddRune(title[n]);
  100. }
  101. Driver.AddRune (' ');
  102. }
  103. }
  104. public override bool ProcessHotKey (KeyEvent kb)
  105. {
  106. foreach(var item in Items) {
  107. if(kb.Key==item.Shortcut) {
  108. if( item.Action!=null ) item.Action();
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. }
  115. }