StatusBar.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. TabStop = TabBehavior.NoStop;
  19. Orientation = Orientation.Horizontal;
  20. Y = Pos.AnchorEnd ();
  21. Width = Dim.Fill ();
  22. Height = Dim.Auto (DimAutoStyle.Content, 1);
  23. BorderStyle = LineStyle.Dashed;
  24. ColorScheme = Colors.ColorSchemes ["Menu"];
  25. LayoutStarted += StatusBar_LayoutStarted;
  26. }
  27. // StatusBar arranges the items horizontally.
  28. // The first item has no left border, the last item has no right border.
  29. // The Shortcuts are configured with the command, help, and key views aligned in reverse order (EndToStart).
  30. private void StatusBar_LayoutStarted (object sender, LayoutEventArgs e)
  31. {
  32. for (int index = 0; index < Subviews.Count; index++)
  33. {
  34. View barItem = Subviews [index];
  35. barItem.BorderStyle = BorderStyle;
  36. if (index == Subviews.Count - 1)
  37. {
  38. barItem.Border.Thickness = new Thickness (0, 0, 0, 0);
  39. }
  40. else
  41. {
  42. barItem.Border.Thickness = new Thickness (0, 0, 1, 0);
  43. }
  44. if (barItem is Shortcut shortcut)
  45. {
  46. shortcut.Orientation = Orientation.Horizontal;
  47. }
  48. }
  49. }
  50. /// <inheritdoc/>
  51. public override View Add (View view)
  52. {
  53. // Call base first, because otherwise it resets CanFocus to true
  54. base.Add (view);
  55. view.CanFocus = false;
  56. if (view is Shortcut shortcut)
  57. {
  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. }