ApplicationNavigation.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 ()
  25. {
  26. return _focused;
  27. }
  28. /// <summary>
  29. /// Gets whether <paramref name="view"/> is in the SubView hierarchy of <paramref name="start"/>.
  30. /// </summary>
  31. /// <param name="start"></param>
  32. /// <param name="view"></param>
  33. /// <returns></returns>
  34. public static bool IsInHierarchy (View? start, View? view)
  35. {
  36. if (view is null)
  37. {
  38. return false;
  39. }
  40. if (view == start || start is null)
  41. {
  42. return true;
  43. }
  44. foreach (View subView in start.SubViews)
  45. {
  46. if (view == subView)
  47. {
  48. return true;
  49. }
  50. bool found = IsInHierarchy (subView, view);
  51. if (found)
  52. {
  53. return found;
  54. }
  55. }
  56. return false;
  57. }
  58. /// <summary>
  59. /// INTERNAL method to record the most focused <see cref="View"/> in the application.
  60. /// </summary>
  61. /// <remarks>
  62. /// Raises <see cref="FocusedChanged"/>.
  63. /// </remarks>
  64. internal void SetFocused (View? value)
  65. {
  66. if (_focused == value)
  67. {
  68. return;
  69. }
  70. Debug.Assert (value is null or { CanFocus: true, HasFocus: true });
  71. _focused = value;
  72. FocusedChanged?.Invoke (null, EventArgs.Empty);
  73. }
  74. /// <summary>
  75. /// Advances the focus to the next or previous view in the focus chain, based on
  76. /// <paramref name="direction"/>.
  77. /// </summary>
  78. /// <remarks>
  79. /// <para>
  80. /// If there is no next/previous view, the focus remains on the current view.
  81. /// </para>
  82. /// </remarks>
  83. /// <param name="direction">The direction to advance.</param>
  84. /// <param name="behavior">The tab behavior.</param>
  85. /// <returns>
  86. /// <see langword="true"/> if focus was changed to another subview (or stayed on this one), <see langword="false"/>
  87. /// otherwise.
  88. /// </returns>
  89. public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior)
  90. {
  91. return Application.Top is { } && Application.Top.AdvanceFocus (direction, behavior);
  92. }
  93. }