Application.Navigation.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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))
  40. {
  41. Application.Current.AdvanceFocus (NavigationDirection.Forward);
  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, true))
  62. {
  63. Application.Current.AdvanceFocus (NavigationDirection.Forward, true);
  64. }
  65. if (top != Application.Current.Focused && top != Application.Current.Focused?.Focused)
  66. {
  67. top?.SetNeedsDisplay ();
  68. Application.Current.Focused?.SetNeedsDisplay ();
  69. }
  70. else
  71. {
  72. ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes, NavigationDirection.Forward);
  73. }
  74. //top!.AdvanceFocus (NavigationDirection.Forward);
  75. //if (top.Focused is null)
  76. //{
  77. // top.AdvanceFocus (NavigationDirection.Forward);
  78. //}
  79. //top.SetNeedsDisplay ();
  80. ApplicationOverlapped.BringOverlappedTopToFront ();
  81. }
  82. else
  83. {
  84. ApplicationOverlapped.OverlappedMoveNext ();
  85. }
  86. }
  87. /// <summary>
  88. /// Moves the focus to the next view. Honors <see cref="ViewArrangement.Overlapped"/> and will only move to the next subview
  89. /// if the current and next subviews are not overlapped.
  90. /// </summary>
  91. internal static void MovePreviousView ()
  92. {
  93. View? old = GetDeepestFocusedSubview (Application.Current!.Focused);
  94. if (!Application.Current.AdvanceFocus (NavigationDirection.Backward))
  95. {
  96. Application.Current.AdvanceFocus (NavigationDirection.Backward);
  97. }
  98. if (old != Application.Current.Focused && old != Application.Current.Focused?.Focused)
  99. {
  100. old?.SetNeedsDisplay ();
  101. Application.Current.Focused?.SetNeedsDisplay ();
  102. }
  103. else
  104. {
  105. ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes?.Reverse (), NavigationDirection.Backward);
  106. }
  107. }
  108. internal static void MovePreviousViewOrTop ()
  109. {
  110. if (ApplicationOverlapped.OverlappedTop is null)
  111. {
  112. Toplevel? top = Application.Current!.Modal ? Application.Current : Application.Top;
  113. top!.AdvanceFocus (NavigationDirection.Backward, true);
  114. if (top.Focused is null)
  115. {
  116. top.AdvanceFocus (NavigationDirection.Backward, true);
  117. }
  118. top.SetNeedsDisplay ();
  119. ApplicationOverlapped.BringOverlappedTopToFront ();
  120. }
  121. else
  122. {
  123. ApplicationOverlapped.OverlappedMovePrevious ();
  124. }
  125. }
  126. }