Application.Navigation.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #nullable enable
  2. using System.Diagnostics;
  3. using System.Reflection.PortableExecutable;
  4. using System.Security.Cryptography;
  5. namespace Terminal.Gui;
  6. /// <summary>
  7. /// Helper class for <see cref="Application"/> navigation.
  8. /// </summary>
  9. internal static class ApplicationNavigation
  10. {
  11. /// <summary>
  12. /// Gets the deepest focused subview of the specified <paramref name="view"/>.
  13. /// </summary>
  14. /// <param name="view"></param>
  15. /// <returns></returns>
  16. internal static View? GetDeepestFocusedSubview (View? view)
  17. {
  18. if (view is null)
  19. {
  20. return null;
  21. }
  22. foreach (View v in view.Subviews)
  23. {
  24. if (v.HasFocus)
  25. {
  26. return GetDeepestFocusedSubview (v);
  27. }
  28. }
  29. return view;
  30. }
  31. /// <summary>
  32. /// Moves the focus to the next focusable view.
  33. /// Honors <see cref="ViewArrangement.Overlapped"/> and will only move to the next subview
  34. /// if the current and next subviews are not overlapped.
  35. /// </summary>
  36. internal static void MoveNextView ()
  37. {
  38. View? old = GetDeepestFocusedSubview (Application.Current!.Focused);
  39. if (!Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop))
  40. {
  41. Application.Current.AdvanceFocus (NavigationDirection.Forward, null);
  42. }
  43. if (old != Application.Current.Focused && old != Application.Current.Focused?.Focused)
  44. {
  45. old?.SetNeedsDisplay ();
  46. Application.Current.Focused?.SetNeedsDisplay ();
  47. }
  48. else
  49. {
  50. ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes, NavigationDirection.Forward);
  51. }
  52. }
  53. /// <summary>
  54. /// Moves the focus to the next <see cref="Toplevel"/> subview or the next subview that has <see cref="ApplicationOverlapped.OverlappedTop"/> set.
  55. /// </summary>
  56. internal static void MoveNextViewOrTop ()
  57. {
  58. if (ApplicationOverlapped.OverlappedTop is null)
  59. {
  60. Toplevel? top = Application.Current!.Modal ? Application.Current : Application.Top;
  61. if (!Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabGroup))
  62. {
  63. Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  64. if (Application.Current.Focused is null)
  65. {
  66. Application.Current.RestoreFocus ();
  67. }
  68. }
  69. if (top != Application.Current.Focused && top != Application.Current.Focused?.Focused)
  70. {
  71. top?.SetNeedsDisplay ();
  72. Application.Current.Focused?.SetNeedsDisplay ();
  73. }
  74. else
  75. {
  76. ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes, NavigationDirection.Forward);
  77. }
  78. //top!.AdvanceFocus (NavigationDirection.Forward);
  79. //if (top.Focused is null)
  80. //{
  81. // top.AdvanceFocus (NavigationDirection.Forward);
  82. //}
  83. //top.SetNeedsDisplay ();
  84. ApplicationOverlapped.BringOverlappedTopToFront ();
  85. }
  86. else
  87. {
  88. ApplicationOverlapped.OverlappedMoveNext ();
  89. }
  90. }
  91. // TODO: These methods should return bool to indicate if the focus was moved or not.
  92. /// <summary>
  93. /// Moves the focus to the next view. Honors <see cref="ViewArrangement.Overlapped"/> and will only move to the next subview
  94. /// if the current and next subviews are not overlapped.
  95. /// </summary>
  96. internal static void MovePreviousView ()
  97. {
  98. View? old = GetDeepestFocusedSubview (Application.Current!.Focused);
  99. if (!Application.Current.AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop))
  100. {
  101. Application.Current.AdvanceFocus (NavigationDirection.Backward, null);
  102. }
  103. if (old != Application.Current.Focused && old != Application.Current.Focused?.Focused)
  104. {
  105. old?.SetNeedsDisplay ();
  106. Application.Current.Focused?.SetNeedsDisplay ();
  107. }
  108. else
  109. {
  110. ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes?.Reverse (), NavigationDirection.Backward);
  111. }
  112. }
  113. internal static void MovePreviousViewOrTop ()
  114. {
  115. if (ApplicationOverlapped.OverlappedTop is null)
  116. {
  117. Toplevel? top = Application.Current!.Modal ? Application.Current : Application.Top;
  118. top!.AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabGroup);
  119. if (top.Focused is null)
  120. {
  121. top.AdvanceFocus (NavigationDirection.Backward, null);
  122. }
  123. top.SetNeedsDisplay ();
  124. ApplicationOverlapped.BringOverlappedTopToFront ();
  125. }
  126. else
  127. {
  128. ApplicationOverlapped.OverlappedMovePrevious ();
  129. }
  130. }
  131. }