StatusBar.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Reflection;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// A status bar is a <see cref="View"/> that snaps to the bottom of a <see cref="Toplevel"/> displaying set of
  6. /// <see cref="Shortcut"/>s. The <see cref="StatusBar"/> should be context sensitive. This means, if the main menu
  7. /// and an open text editor are visible, the items probably shown will be ~F1~ Help ~F2~ Save ~F3~ Load. While a dialog
  8. /// to ask a file to load is executed, the remaining commands will probably be ~F1~ Help. So for each context must be a
  9. /// new instance of a status bar.
  10. /// </summary>
  11. public class StatusBar : Bar
  12. {
  13. /// <inheritdoc/>
  14. public StatusBar () : this ([]) { }
  15. /// <inheritdoc/>
  16. public StatusBar (IEnumerable<Shortcut> shortcuts) : base (shortcuts)
  17. {
  18. Orientation = Orientation.Horizontal;
  19. Y = Pos.AnchorEnd ();
  20. Width = Dim.Fill ();
  21. Height = Dim.Auto (DimAutoStyle.Content, 1);
  22. BorderStyle = LineStyle.Dashed;
  23. ColorScheme = Colors.ColorSchemes ["Menu"];
  24. LayoutStarted += StatusBar_LayoutStarted;
  25. }
  26. // StatusBar arranges the items horizontally.
  27. // The first item has no left border, the last item has no right border.
  28. // The Shortcuts are configured with the command, help, and key views aligned in reverse order (EndToStart).
  29. private void StatusBar_LayoutStarted (object sender, LayoutEventArgs e)
  30. {
  31. for (int index = 0; index < Subviews.Count; index++)
  32. {
  33. View barItem = Subviews [index];
  34. barItem.BorderStyle = BorderStyle;
  35. if (index == Subviews.Count - 1)
  36. {
  37. barItem.Border.Thickness = new Thickness (0, 0, 0, 0);
  38. }
  39. else
  40. {
  41. barItem.Border.Thickness = new Thickness (0, 0, 1, 0);
  42. }
  43. if (barItem is Shortcut shortcut)
  44. {
  45. shortcut.Orientation = Orientation.Horizontal;
  46. }
  47. }
  48. }
  49. /// <inheritdoc/>
  50. public override View Add (View view)
  51. {
  52. // Call base first, because otherwise it resets CanFocus to true
  53. base.Add (view);
  54. view.CanFocus = false;
  55. if (view is Shortcut shortcut)
  56. {
  57. shortcut.KeyBindingScope = KeyBindingScope.Application;
  58. // TODO: not happy about using AlignmentModes for this. Too implied.
  59. // TODO: instead, add a property (a style enum?) to Shortcut to control this
  60. shortcut.AlignmentModes = AlignmentModes.EndToStart;
  61. }
  62. return view;
  63. }
  64. }