ApplicationNavigation.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. //if (start.Border is { })
  54. //{
  55. // return IsInHierarchy (start.Border, view);
  56. //}
  57. return false;
  58. }
  59. /// <summary>
  60. /// INTERNAL method to record the most focused <see cref="View"/> in the application.
  61. /// </summary>
  62. /// <remarks>
  63. /// Raises <see cref="FocusedChanged"/>.
  64. /// </remarks>
  65. internal void SetFocused (View? value)
  66. {
  67. if (_focused == value)
  68. {
  69. return;
  70. }
  71. _focused = value;
  72. //if (_focused is { } && Application.PositionCursor ())
  73. //{
  74. // Application.Driver?.UpdateCursor ();
  75. //}
  76. FocusedChanged?.Invoke (null, EventArgs.Empty);
  77. }
  78. /// <summary>
  79. /// Advances the focus to the next or previous view in the focus chain, based on
  80. /// <paramref name="direction"/>.
  81. /// </summary>
  82. /// <remarks>
  83. /// <para>
  84. /// If there is no next/previous view, the focus remains on the current view.
  85. /// </para>
  86. /// </remarks>
  87. /// <param name="direction">The direction to advance.</param>
  88. /// <param name="behavior">The tab behavior.</param>
  89. /// <returns>
  90. /// <see langword="true"/> if focus was changed to another subview (or stayed on this one), <see langword="false"/>
  91. /// otherwise.
  92. /// </returns>
  93. public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior)
  94. {
  95. return Application.Top is { } && Application.Top.AdvanceFocus (direction, behavior);
  96. }
  97. }