StatusBar.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, IDesignable
  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. SubViewLayout += 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.ElementAt (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. protected override void OnSubViewAdded (View subView)
  52. {
  53. subView.CanFocus = false;
  54. if (subView is Shortcut shortcut)
  55. {
  56. // TODO: not happy about using AlignmentModes for this. Too implied.
  57. // TODO: instead, add a property (a style enum?) to Shortcut to control this
  58. shortcut.AlignmentModes = AlignmentModes.EndToStart;
  59. }
  60. }
  61. /// <inheritdoc />
  62. bool IDesignable.EnableForDesign ()
  63. {
  64. var shortcut = new Shortcut
  65. {
  66. Text = "Quit",
  67. Title = "Q_uit",
  68. Key = Key.Z.WithCtrl,
  69. };
  70. Add (shortcut);
  71. shortcut = new Shortcut
  72. {
  73. Text = "Help Text",
  74. Title = "Help",
  75. Key = Key.F1,
  76. };
  77. Add (shortcut);
  78. shortcut = new Shortcut
  79. {
  80. Title = "_Show/Hide",
  81. Key = Key.F10,
  82. CommandView = new CheckBox
  83. {
  84. CanFocus = false,
  85. Text = "_Show/Hide"
  86. },
  87. };
  88. Add (shortcut);
  89. var button1 = new Button
  90. {
  91. Text = "I'll Hide",
  92. // Visible = false
  93. };
  94. button1.Accepting += Button_Clicked;
  95. Add (button1);
  96. shortcut.Accepting += (s, e) =>
  97. {
  98. button1.Visible = !button1.Visible;
  99. button1.Enabled = button1.Visible;
  100. e.Cancel = false;
  101. };
  102. Add (new Label
  103. {
  104. HotKeySpecifier = new Rune ('_'),
  105. Text = "Fo_cusLabel",
  106. CanFocus = true
  107. });
  108. var button2 = new Button
  109. {
  110. Text = "Or me!",
  111. };
  112. button2.Accepting += (s, e) => Application.RequestStop ();
  113. Add (button2);
  114. return true;
  115. void Button_Clicked (object sender, EventArgs e) { MessageBox.Query ("Hi", $"You clicked {sender}"); }
  116. }
  117. }