123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using System;
- using System.Reflection;
- namespace Terminal.Gui;
- /// <summary>
- /// A status bar is a <see cref="View"/> that snaps to the bottom of a <see cref="Toplevel"/> displaying set of
- /// <see cref="Shortcut"/>s. The <see cref="StatusBar"/> should be context sensitive. This means, if the main menu
- /// and an open text editor are visible, the items probably shown will be ~F1~ Help ~F2~ Save ~F3~ Load. While a dialog
- /// to ask a file to load is executed, the remaining commands will probably be ~F1~ Help. So for each context must be a
- /// new instance of a status bar.
- /// </summary>
- public class StatusBar : Bar, IDesignable
- {
- /// <inheritdoc/>
- public StatusBar () : this ([]) { }
- /// <inheritdoc/>
- public StatusBar (IEnumerable<Shortcut> shortcuts) : base (shortcuts)
- {
- TabStop = TabBehavior.NoStop;
- Orientation = Orientation.Horizontal;
- Y = Pos.AnchorEnd ();
- Width = Dim.Fill ();
- Height = Dim.Auto (DimAutoStyle.Content, 1);
- BorderStyle = LineStyle.Dashed;
- ColorScheme = Colors.ColorSchemes ["Menu"];
- SubviewLayout += StatusBar_LayoutStarted;
- }
- // StatusBar arranges the items horizontally.
- // The first item has no left border, the last item has no right border.
- // The Shortcuts are configured with the command, help, and key views aligned in reverse order (EndToStart).
- private void StatusBar_LayoutStarted (object sender, LayoutEventArgs e)
- {
- for (int index = 0; index < Subviews.Count; index++)
- {
- View barItem = Subviews [index];
- barItem.BorderStyle = BorderStyle;
- if (index == Subviews.Count - 1)
- {
- barItem.Border.Thickness = new Thickness (0, 0, 0, 0);
- }
- else
- {
- barItem.Border.Thickness = new Thickness (0, 0, 1, 0);
- }
- if (barItem is Shortcut shortcut)
- {
- shortcut.Orientation = Orientation.Horizontal;
- }
- }
- }
- /// <inheritdoc/>
- public override View Add (View view)
- {
- // Call base first, because otherwise it resets CanFocus to true
- base.Add (view);
- view.CanFocus = false;
- if (view is Shortcut shortcut)
- {
- // TODO: not happy about using AlignmentModes for this. Too implied.
- // TODO: instead, add a property (a style enum?) to Shortcut to control this
- shortcut.AlignmentModes = AlignmentModes.EndToStart;
- }
- return view;
- }
- /// <inheritdoc />
- bool IDesignable.EnableForDesign ()
- {
- var shortcut = new Shortcut
- {
- Text = "Quit",
- Title = "Q_uit",
- Key = Key.Z.WithCtrl,
- };
- Add (shortcut);
- shortcut = new Shortcut
- {
- Text = "Help Text",
- Title = "Help",
- Key = Key.F1,
- };
- Add (shortcut);
- shortcut = new Shortcut
- {
- Title = "_Show/Hide",
- Key = Key.F10,
- CommandView = new CheckBox
- {
- CanFocus = false,
- Text = "_Show/Hide"
- },
- };
- Add (shortcut);
- var button1 = new Button
- {
- Text = "I'll Hide",
- // Visible = false
- };
- button1.Accepting += Button_Clicked;
- Add (button1);
- shortcut.Accepting += (s, e) =>
- {
- button1.Visible = !button1.Visible;
- button1.Enabled = button1.Visible;
- e.Cancel = false;
- };
- Add (new Label
- {
- HotKeySpecifier = new Rune ('_'),
- Text = "Fo_cusLabel",
- CanFocus = true
- });
- var button2 = new Button
- {
- Text = "Or me!",
- };
- button2.Accepting += (s, e) => Application.RequestStop ();
- Add (button2);
- return true;
- void Button_Clicked (object sender, EventArgs e) { MessageBox.Query ("Hi", $"You clicked {sender}"); }
- }
- }
|