StatusBar.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. namespace Terminal.Gui.Views;
  2. /// <summary>
  3. /// A status bar is a <see cref="View"/> that snaps to the bottom of the Viewport displaying set of
  4. /// <see cref="Shortcut"/>s. The <see cref="StatusBar"/> should be context-sensitive. This means, if the main menu
  5. /// and an open text editor are visible, the items probably shown will be ~F1~ Help ~F2~ Save ~F3~ Load. While a dialog
  6. /// to ask a file to load is executed, the remaining commands will probably be ~F1~ Help. So for each context must be a
  7. /// new instance of a status bar.
  8. /// </summary>
  9. public class StatusBar : Bar, IDesignable
  10. {
  11. private static LineStyle _defaultSeparatorLineStyle = LineStyle.Single; // Resources/config.json overrides
  12. /// <inheritdoc/>
  13. public StatusBar () : this ([]) { }
  14. /// <inheritdoc/>
  15. public StatusBar (IEnumerable<Shortcut> shortcuts) : base (shortcuts)
  16. {
  17. TabStop = TabBehavior.NoStop;
  18. Orientation = Orientation.Horizontal;
  19. Y = Pos.AnchorEnd ();
  20. Width = Dim.Fill ();
  21. Height = Dim.Auto (DimAutoStyle.Content, 1);
  22. if (Border is { })
  23. {
  24. Border.LineStyle = DefaultSeparatorLineStyle;
  25. }
  26. SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Menu);
  27. ConfigurationManager.Applied += OnConfigurationManagerApplied;
  28. SuperViewChanged += OnSuperViewChanged;
  29. }
  30. private void OnSuperViewChanged (object? sender, SuperViewChangedEventArgs e)
  31. {
  32. if (SuperView is null)
  33. {
  34. // BUGBUG: This is a hack for avoiding a race condition in ConfigurationManager.Apply
  35. // BUGBUG: For some reason in some unit tests, when Top is disposed, MenuBar.Dispose does not get called.
  36. // BUGBUG: Yet, the MenuBar does get Removed from Top (and it's SuperView set to null).
  37. // BUGBUG: Related: https://github.com/gui-cs/Terminal.Gui/issues/4021
  38. ConfigurationManager.Applied -= OnConfigurationManagerApplied;
  39. }
  40. }
  41. private void OnConfigurationManagerApplied (object? sender, ConfigurationManagerEventArgs e)
  42. {
  43. if (Border is { })
  44. {
  45. Border.LineStyle = DefaultSeparatorLineStyle;
  46. }
  47. }
  48. /// <summary>
  49. /// Gets or sets the default Line Style for the separators between the shortcuts of the StatusBar.
  50. /// </summary>
  51. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  52. public static LineStyle DefaultSeparatorLineStyle
  53. {
  54. get => _defaultSeparatorLineStyle;
  55. set => _defaultSeparatorLineStyle = value;
  56. }
  57. /// <inheritdoc />
  58. protected override void OnSubViewLayout (LayoutEventArgs args)
  59. {
  60. for (int index = 0; index < SubViews.Count; index++)
  61. {
  62. View barItem = SubViews.ElementAt (index);
  63. barItem.BorderStyle = BorderStyle;
  64. if (barItem.Border is { })
  65. {
  66. barItem.Border!.Thickness = index == SubViews.Count - 1 ? new Thickness (0, 0, 0, 0) : new Thickness (0, 0, 1, 0);
  67. }
  68. if (barItem is Shortcut shortcut)
  69. {
  70. shortcut.Orientation = Orientation.Horizontal;
  71. }
  72. }
  73. base.OnSubViewLayout (args);
  74. }
  75. /// <inheritdoc/>
  76. protected override void OnSubViewAdded (View subView)
  77. {
  78. subView.CanFocus = false;
  79. if (subView is Shortcut shortcut)
  80. {
  81. // TODO: not happy about using AlignmentModes for this. Too implied.
  82. // TODO: instead, add a property (a style enum?) to Shortcut to control this
  83. shortcut.AlignmentModes = AlignmentModes.EndToStart;
  84. }
  85. }
  86. /// <inheritdoc />
  87. bool IDesignable.EnableForDesign ()
  88. {
  89. var shortcut = new Shortcut
  90. {
  91. Text = "Quit",
  92. Title = "Q_uit",
  93. Key = Key.Z.WithCtrl,
  94. };
  95. Add (shortcut);
  96. shortcut = new Shortcut
  97. {
  98. Text = "Help Text",
  99. Title = "Help",
  100. Key = Key.F1,
  101. };
  102. Add (shortcut);
  103. shortcut = new Shortcut
  104. {
  105. Title = "_Show/Hide",
  106. Key = Key.F10,
  107. CommandView = new CheckBox
  108. {
  109. CanFocus = false,
  110. Text = "_Show/Hide"
  111. },
  112. };
  113. Add (shortcut);
  114. var button1 = new Button
  115. {
  116. Text = "I'll Hide",
  117. // Visible = false
  118. };
  119. button1.Accepting += OnButtonClicked;
  120. Add (button1);
  121. #pragma warning disable TGUI001
  122. shortcut.Accepting += (_, e) =>
  123. {
  124. button1.Visible = !button1.Visible;
  125. button1.Enabled = button1.Visible;
  126. e.Handled = false;
  127. };
  128. #pragma warning restore TGUI001
  129. Add (new Label
  130. {
  131. HotKeySpecifier = new Rune ('_'),
  132. Text = "Fo_cusLabel",
  133. CanFocus = true
  134. });
  135. var button2 = new Button
  136. {
  137. Text = "Or me!",
  138. };
  139. button2.Accepting += (s, e) => App?.RequestStop ();
  140. Add (button2);
  141. return true;
  142. void OnButtonClicked (object? sender, EventArgs? e) { MessageBox.Query (App, "Hi", $"You clicked {sender}"); }
  143. }
  144. /// <inheritdoc />
  145. protected override void Dispose (bool disposing)
  146. {
  147. base.Dispose (disposing);
  148. SuperViewChanged -= OnSuperViewChanged;
  149. ConfigurationManager.Applied -= OnConfigurationManagerApplied;
  150. }
  151. }