StatusBar.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// A status bar is a <see cref="View"/> that snaps to the bottom of a <see cref="Toplevel"/> 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
  10. {
  11. public StatusBar ()
  12. {
  13. Orientation = Orientation.Horizontal;
  14. Y = Pos.AnchorEnd ();
  15. Width = Dim.Fill ();
  16. }
  17. /// <inheritdoc />
  18. public override void Add (View view)
  19. {
  20. view.CanFocus = false;
  21. base.Add (view);
  22. }
  23. /// <summary>Inserts a <see cref="Shortcut"/> in the specified index of <see cref="Items"/>.</summary>
  24. /// <param name="index">The zero-based index at which item should be inserted.</param>
  25. /// <param name="item">The item to insert.</param>
  26. public void AddShortcutAt (int index, Shortcut item)
  27. {
  28. List<View> savedSubViewList = Subviews.ToList ();
  29. int count = savedSubViewList.Count;
  30. RemoveAll ();
  31. for (int i = 0; i < count; i++)
  32. {
  33. if (i == index)
  34. {
  35. Add (item);
  36. }
  37. Add (savedSubViewList [i]);
  38. }
  39. SetNeedsDisplay ();
  40. }
  41. /// <summary>Removes a <see cref="Shortcut"/> at specified index of <see cref="Items"/>.</summary>
  42. /// <param name="index">The zero-based index of the item to remove.</param>
  43. /// <returns>The <see cref="Shortcut"/> removed.</returns>
  44. public Shortcut RemoveItem (int index)
  45. {
  46. View toRemove = null;
  47. for (int i = 0; i < Subviews.Count; i++)
  48. {
  49. if (i == index)
  50. {
  51. toRemove = Subviews [i];
  52. }
  53. }
  54. if (toRemove is { })
  55. {
  56. Remove (toRemove);
  57. SetNeedsDisplay ();
  58. }
  59. return toRemove as Shortcut;
  60. }
  61. }