ApplicationNavigation.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Helper class for <see cref="Application"/> navigation. Held by <see cref="Application.Navigation"/>
  6. /// </summary>
  7. public class ApplicationNavigation
  8. {
  9. /// <summary>
  10. /// Initializes a new instance of the <see cref="ApplicationNavigation"/> class.
  11. /// </summary>
  12. public ApplicationNavigation ()
  13. {
  14. // TODO: Move navigation key bindings here from AddApplicationKeyBindings
  15. }
  16. private View? _focused;
  17. /// <summary>
  18. /// Raised when the most focused <see cref="View"/> in the application has changed.
  19. /// </summary>
  20. public event EventHandler<EventArgs>? FocusedChanged;
  21. /// <summary>
  22. /// Gets the most focused <see cref="View"/> in the application, if there is one.
  23. /// </summary>
  24. public View? GetFocused () { return _focused; }
  25. /// <summary>
  26. /// Gets whether <paramref name="view"/> is in the Subview hierarchy of <paramref name="start"/>.
  27. /// </summary>
  28. /// <param name="start"></param>
  29. /// <param name="view"></param>
  30. /// <returns></returns>
  31. public static bool IsInHierarchy (View? start, View? view)
  32. {
  33. if (view is null)
  34. {
  35. return false;
  36. }
  37. if (view == start || start is null)
  38. {
  39. return true;
  40. }
  41. foreach (View subView in start.Subviews)
  42. {
  43. if (view == subView)
  44. {
  45. return true;
  46. }
  47. bool found = IsInHierarchy (subView, view);
  48. if (found)
  49. {
  50. return found;
  51. }
  52. }
  53. return false;
  54. }
  55. /// <summary>
  56. /// INTERNAL method to record the most focused <see cref="View"/> in the application.
  57. /// </summary>
  58. /// <remarks>
  59. /// Raises <see cref="FocusedChanged"/>.
  60. /// </remarks>
  61. internal void SetFocused (View? value)
  62. {
  63. if (_focused == value)
  64. {
  65. return;
  66. }
  67. Debug.Assert (value is null or { CanFocus: true, HasFocus: true });
  68. _focused = value;
  69. FocusedChanged?.Invoke (null, EventArgs.Empty);
  70. }
  71. /// <summary>
  72. /// Advances the focus to the next or previous view in the focus chain, based on
  73. /// <paramref name="direction"/>.
  74. /// </summary>
  75. /// <remarks>
  76. /// <para>
  77. /// If there is no next/previous view, the focus remains on the current view.
  78. /// </para>
  79. /// </remarks>
  80. /// <param name="direction">The direction to advance.</param>
  81. /// <param name="behavior">The tab behavior.</param>
  82. /// <returns>
  83. /// <see langword="true"/> if focus was changed to another subview (or stayed on this one), <see langword="false"/>
  84. /// otherwise.
  85. /// </returns>
  86. public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior)
  87. {
  88. return Application.Top is { } && Application.Top.AdvanceFocus (direction, behavior);
  89. }
  90. }