StatusBar.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #nullable enable
  2. namespace Terminal.Gui.Views;
  3. /// <summary>
  4. /// A status bar is a <see cref="View"/> that snaps to the bottom of a <see cref="Toplevel"/> displaying set of
  5. /// <see cref="Shortcut"/>s. The <see cref="StatusBar"/> should be context sensitive. This means, if the main menu
  6. /// and an open text editor are visible, the items probably shown will be ~F1~ Help ~F2~ Save ~F3~ Load. While a dialog
  7. /// to ask a file to load is executed, the remaining commands will probably be ~F1~ Help. So for each context must be a
  8. /// new instance of a status bar.
  9. /// </summary>
  10. public class StatusBar : Bar, IDesignable
  11. {
  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 { get; set; } = LineStyle.Single;
  53. /// <inheritdoc />
  54. protected override void OnSubViewLayout (LayoutEventArgs args)
  55. {
  56. for (int index = 0; index < SubViews.Count; index++)
  57. {
  58. View barItem = SubViews.ElementAt (index);
  59. barItem.BorderStyle = BorderStyle;
  60. if (barItem.Border is { })
  61. {
  62. barItem.Border!.Thickness = index == SubViews.Count - 1 ? new Thickness (0, 0, 0, 0) : new Thickness (0, 0, 1, 0);
  63. }
  64. if (barItem is Shortcut shortcut)
  65. {
  66. shortcut.Orientation = Orientation.Horizontal;
  67. }
  68. }
  69. base.OnSubViewLayout (args);
  70. }
  71. /// <inheritdoc/>
  72. protected override void OnSubViewAdded (View subView)
  73. {
  74. subView.CanFocus = false;
  75. if (subView is Shortcut shortcut)
  76. {
  77. // TODO: not happy about using AlignmentModes for this. Too implied.
  78. // TODO: instead, add a property (a style enum?) to Shortcut to control this
  79. shortcut.AlignmentModes = AlignmentModes.EndToStart;
  80. }
  81. }
  82. /// <inheritdoc />
  83. bool IDesignable.EnableForDesign ()
  84. {
  85. var shortcut = new Shortcut
  86. {
  87. Text = "Quit",
  88. Title = "Q_uit",
  89. Key = Key.Z.WithCtrl,
  90. };
  91. Add (shortcut);
  92. shortcut = new Shortcut
  93. {
  94. Text = "Help Text",
  95. Title = "Help",
  96. Key = Key.F1,
  97. };
  98. Add (shortcut);
  99. shortcut = new Shortcut
  100. {
  101. Title = "_Show/Hide",
  102. Key = Key.F10,
  103. CommandView = new CheckBox
  104. {
  105. CanFocus = false,
  106. Text = "_Show/Hide"
  107. },
  108. };
  109. Add (shortcut);
  110. var button1 = new Button
  111. {
  112. Text = "I'll Hide",
  113. // Visible = false
  114. };
  115. button1.Accepting += OnButtonClicked;
  116. Add (button1);
  117. shortcut.Accepting += (s, e) =>
  118. {
  119. button1.Visible = !button1.Visible;
  120. button1.Enabled = button1.Visible;
  121. e.Handled = false;
  122. };
  123. Add (new Label
  124. {
  125. HotKeySpecifier = new Rune ('_'),
  126. Text = "Fo_cusLabel",
  127. CanFocus = true
  128. });
  129. var button2 = new Button
  130. {
  131. Text = "Or me!",
  132. };
  133. button2.Accepting += (s, e) => Application.RequestStop ();
  134. Add (button2);
  135. return true;
  136. void OnButtonClicked (object? sender, EventArgs? e) { MessageBox.Query ("Hi", $"You clicked {sender}"); }
  137. }
  138. /// <inheritdoc />
  139. protected override void Dispose (bool disposing)
  140. {
  141. base.Dispose (disposing);
  142. SuperViewChanged -= OnSuperViewChanged;
  143. ConfigurationManager.Applied -= OnConfigurationManagerApplied;
  144. }
  145. }