123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- #nullable enable
- namespace Terminal.Gui;
- /// <summary>
- /// Provides a horizontally or vertically oriented container for <see cref="Shortcut"/>s to be used as a menu, toolbar, or status
- /// bar.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Any <see cref="View"/> can be added to a <see cref="Bar"/>. However, the <see cref="Bar"/> is designed to work with
- /// <see cref="Shortcut"/> objects. The <see cref="Shortcut"/> class provides a way to display a command, help, and key and
- /// align them in a specific order.
- /// </para>
- /// </remarks>
- public class Bar : View, IOrientation, IDesignable
- {
- private readonly OrientationHelper _orientationHelper;
- /// <inheritdoc/>
- public Bar () : this ([]) { }
- /// <inheritdoc/>
- public Bar (IEnumerable<Shortcut> shortcuts)
- {
- CanFocus = true;
- Width = Dim.Auto ();
- Height = Dim.Auto ();
- _orientationHelper = new (this);
- _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
- _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
- // Initialized += Bar_Initialized;
- MouseEvent += OnMouseEvent;
- if (shortcuts is null)
- {
- return;
- }
- foreach (Shortcut shortcut in shortcuts)
- {
- Add (shortcut);
- }
- }
- private void OnMouseEvent (object? sender, MouseEventArgs e)
- {
- NavigationDirection direction = NavigationDirection.Backward;
- if (e.Flags == MouseFlags.WheeledDown)
- {
- e.Handled = true;
- }
- if (e.Flags == MouseFlags.WheeledUp)
- {
- direction = NavigationDirection.Forward;
- e.Handled = true;
- }
- if (e.Flags == MouseFlags.WheeledRight)
- {
- e.Handled = true;
- }
- if (e.Flags == MouseFlags.WheeledLeft)
- {
- direction = NavigationDirection.Forward;
- e.Handled = true;
- }
- if (e.Handled)
- {
- e.Handled = AdvanceFocus (direction, TabBehavior.TabStop);
- }
- }
- /// <inheritdoc />
- public override void EndInit ()
- {
- base.EndInit ();
- ColorScheme = Colors.ColorSchemes ["Menu"];
- }
- /// <inheritdoc/>
- public override void SetBorderStyle (LineStyle value)
- {
- // The default changes the thickness. We don't want that. We just set the style.
- Border.LineStyle = value;
- }
- #region IOrientation members
- /// <summary>
- /// Gets or sets the <see cref="Orientation"/> for this <see cref="Bar"/>. The default is
- /// <see cref="Orientation.Horizontal"/>.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Horizontal orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from right to left
- /// Vertical orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from left to right.
- /// </para>
- /// </remarks>
- public Orientation Orientation
- {
- get => _orientationHelper.Orientation;
- set => _orientationHelper.Orientation = value;
- }
- /// <inheritdoc/>
- public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
- /// <inheritdoc/>
- public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
- /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
- /// <param name="newOrientation"></param>
- public void OnOrientationChanged (Orientation newOrientation)
- {
- // BUGBUG: this should not be SuperView.GetContentSize
- LayoutBarItems (SuperView?.GetContentSize () ?? Application.Screen.Size);
- }
- #endregion
- private AlignmentModes _alignmentModes = AlignmentModes.StartToEnd;
- /// <summary>
- /// Gets or sets the <see cref="AlignmentModes"/> for this <see cref="Bar"/>. The default is
- /// <see cref="AlignmentModes.StartToEnd"/>.
- /// </summary>
- public AlignmentModes AlignmentModes
- {
- get => _alignmentModes;
- set
- {
- _alignmentModes = value;
- SetNeedsDraw ();
- SetNeedsLayout ();
- }
- }
- // TODO: Move this to View
- /// <summary>Inserts a <see cref="Shortcut"/> in the specified index of <see cref="View.Subviews"/>.</summary>
- /// <param name="index">The zero-based index at which item should be inserted.</param>
- /// <param name="item">The item to insert.</param>
- public void AddShortcutAt (int index, Shortcut item)
- {
- List<View> savedSubViewList = Subviews.ToList ();
- int count = savedSubViewList.Count;
- RemoveAll ();
- for (var i = 0; i <= count; i++)
- {
- if (i == index)
- {
- Add (item);
- }
- if (i < count)
- {
- Add (savedSubViewList [i]);
- }
- }
- SetNeedsDraw ();
- SetNeedsLayout ();
- }
- // TODO: Move this to View
- /// <summary>Removes a <see cref="Shortcut"/> at specified index of <see cref="View.Subviews"/>.</summary>
- /// <param name="index">The zero-based index of the item to remove.</param>
- /// <returns>The <see cref="Shortcut"/> removed.</returns>
- public Shortcut? RemoveShortcut (int index)
- {
- View? toRemove = null;
- for (var i = 0; i < Subviews.Count; i++)
- {
- if (i == index)
- {
- toRemove = Subviews [i];
- }
- }
- if (toRemove is { })
- {
- Remove (toRemove);
- SetNeedsDraw ();
- SetNeedsLayout ();
- }
- return toRemove as Shortcut;
- }
- /// <inheritdoc />
- protected override void OnSubviewLayout (LayoutEventArgs args)
- {
- LayoutBarItems (args.OldContentSize);
- }
- // This is used to calculate the minimum width of the Bar when the width is NOT Dim.Auto
- private int? _minimumDimAutoWidth;
- private void LayoutBarItems (Size contentSize)
- {
- View? prevBarItem = null;
- switch (Orientation)
- {
- case Orientation.Horizontal:
- for (var index = 0; index < Subviews.Count; index++)
- {
- View barItem = Subviews [index];
- barItem.ColorScheme = ColorScheme;
- barItem.X = Pos.Align (Alignment.Start, AlignmentModes);
- barItem.Y = 0; //Pos.Center ();
- }
- break;
- case Orientation.Vertical:
- if (Width!.Has<DimAuto> (out _))
- {
- // Set the overall size of the Bar and arrange the views vertically
- var minKeyWidth = 0;
- List<Shortcut> shortcuts = Subviews.Where (s => s is Shortcut && s.Visible).Cast<Shortcut> ().ToList ();
- foreach (Shortcut shortcut in shortcuts)
- {
- // Get the largest width of all KeyView's
- minKeyWidth = int.Max (minKeyWidth, shortcut.KeyView.Text.GetColumns ());
- }
- var _maxBarItemWidth = 0;
- for (var index = 0; index < Subviews.Count; index++)
- {
- View barItem = Subviews [index];
- barItem.X = 0;
- barItem.ColorScheme = ColorScheme;
- if (!barItem.Visible)
- {
- continue;
- }
- if (barItem is Shortcut scBarItem)
- {
- scBarItem.MinimumKeyTextSize = minKeyWidth;
- scBarItem.Width = scBarItem.GetWidthDimAuto ();
- barItem.Layout (Application.Screen.Size);
- _maxBarItemWidth = Math.Max (_maxBarItemWidth, barItem.Frame.Width);
- }
- if (prevBarItem == null)
- {
- // TODO: Just use Pos.Align!
- barItem.Y = 0;
- }
- else
- {
- // TODO: Just use Pos.Align!
- // Align the view to the bottom of the previous view
- barItem.Y = Pos.Bottom (prevBarItem);
- }
- prevBarItem = barItem;
- }
- foreach (var subView in Subviews)
- {
- subView.Width = Dim.Auto (DimAutoStyle.Auto, minimumContentDim: _maxBarItemWidth);
- }
- }
- else
- {
- foreach (var subView in Subviews)
- {
- subView.Width = Dim.Fill();
- }
- }
- break;
- }
- }
- /// <inheritdoc />
- public bool 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
- {
- Text = "Czech",
- CommandView = new CheckBox ()
- {
- Title = "_Check"
- },
- Key = Key.F9,
- CanFocus = false
- };
- Add (shortcut);
- return true;
- }
- }
|