ApplicationNavigation.cs 3.1 KB

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