Application.Navigation.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #nullable enable
  2. using System.Diagnostics;
  3. using System.Reflection.PortableExecutable;
  4. using System.Security.Cryptography;
  5. namespace Terminal.Gui;
  6. /// <summary>
  7. /// Static helper class for <see cref="Application"/> navigation.
  8. /// </summary>
  9. public static class ApplicationNavigation
  10. {
  11. private static View? _focused = null;
  12. /// <summary>
  13. /// Gets the most focused <see cref="View"/> in the application, if there is one.
  14. /// </summary>
  15. public static View? GetFocused () { return _focused; }
  16. /// <summary>
  17. /// INTERNAL method to record the most focused <see cref="View"/> in the application.
  18. /// </summary>
  19. /// <remarks>
  20. /// Raises <see cref="FocusedChanged"/>.
  21. /// </remarks>
  22. internal static void SetFocused (View? value)
  23. {
  24. if (_focused == value)
  25. {
  26. return;
  27. }
  28. _focused = value;
  29. FocusedChanged?.Invoke (null, EventArgs.Empty);
  30. return;
  31. }
  32. /// <summary>
  33. /// Gets whether <paramref name="view"/> is in the Subview hierarchy of <paramref name="start"/>.
  34. /// </summary>
  35. /// <param name="start"></param>
  36. /// <param name="view"></param>
  37. /// <returns></returns>
  38. public static bool IsInHierarchy (View start, View? view)
  39. {
  40. if (view is null)
  41. {
  42. return false;
  43. }
  44. if (view == start)
  45. {
  46. return true;
  47. }
  48. foreach (View subView in start.Subviews)
  49. {
  50. if (view == subView)
  51. {
  52. return true;
  53. }
  54. var found = IsInHierarchy (subView, view);
  55. if (found)
  56. {
  57. return found;
  58. }
  59. }
  60. return false;
  61. }
  62. /// <summary>
  63. /// Raised when the most focused <see cref="View"/> in the application has changed.
  64. /// </summary>
  65. public static event EventHandler<EventArgs>? FocusedChanged;
  66. /// <summary>
  67. /// Gets the deepest focused subview of the specified <paramref name="view"/>.
  68. /// </summary>
  69. /// <param name="view"></param>
  70. /// <returns></returns>
  71. internal static View? GetDeepestFocusedSubview (View? view)
  72. {
  73. if (view is null)
  74. {
  75. return null;
  76. }
  77. foreach (View v in view.Subviews)
  78. {
  79. if (v.HasFocus)
  80. {
  81. return GetDeepestFocusedSubview (v);
  82. }
  83. }
  84. return view;
  85. }
  86. /// <summary>
  87. /// Moves the focus to the next focusable view.
  88. /// 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 MoveNextView ()
  92. {
  93. View? old = GetDeepestFocusedSubview (Application.Current!.Focused);
  94. if (!Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop))
  95. {
  96. Application.Current.AdvanceFocus (NavigationDirection.Forward, null);
  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, NavigationDirection.Forward);
  106. }
  107. }
  108. /// <summary>
  109. /// Moves the focus to the next <see cref="Toplevel"/> subview or the next subview that has <see cref="ApplicationOverlapped.OverlappedTop"/> set.
  110. /// </summary>
  111. internal static void MoveNextViewOrTop ()
  112. {
  113. if (ApplicationOverlapped.OverlappedTop is null)
  114. {
  115. Toplevel? top = Application.Current!.Modal ? Application.Current : Application.Top;
  116. if (!Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabGroup))
  117. {
  118. Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  119. if (Application.Current.Focused is null)
  120. {
  121. Application.Current.RestoreFocus (TabBehavior.TabGroup);
  122. }
  123. }
  124. if (top != Application.Current.Focused && top != Application.Current.Focused?.Focused)
  125. {
  126. top?.SetNeedsDisplay ();
  127. Application.Current.Focused?.SetNeedsDisplay ();
  128. }
  129. else
  130. {
  131. ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes, NavigationDirection.Forward);
  132. }
  133. //top!.AdvanceFocus (NavigationDirection.Forward);
  134. //if (top.Focused is null)
  135. //{
  136. // top.AdvanceFocus (NavigationDirection.Forward);
  137. //}
  138. //top.SetNeedsDisplay ();
  139. ApplicationOverlapped.BringOverlappedTopToFront ();
  140. }
  141. else
  142. {
  143. ApplicationOverlapped.OverlappedMoveNext ();
  144. }
  145. }
  146. // TODO: These methods should return bool to indicate if the focus was moved or not.
  147. /// <summary>
  148. /// Moves the focus to the next view. Honors <see cref="ViewArrangement.Overlapped"/> and will only move to the next subview
  149. /// if the current and next subviews are not overlapped.
  150. /// </summary>
  151. internal static void MovePreviousView ()
  152. {
  153. View? old = GetDeepestFocusedSubview (Application.Current!.Focused);
  154. if (!Application.Current.AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop))
  155. {
  156. Application.Current.AdvanceFocus (NavigationDirection.Backward, null);
  157. }
  158. if (old != Application.Current.Focused && old != Application.Current.Focused?.Focused)
  159. {
  160. old?.SetNeedsDisplay ();
  161. Application.Current.Focused?.SetNeedsDisplay ();
  162. }
  163. else
  164. {
  165. ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes?.Reverse (), NavigationDirection.Backward);
  166. }
  167. }
  168. internal static void MovePreviousViewOrTop ()
  169. {
  170. if (ApplicationOverlapped.OverlappedTop is null)
  171. {
  172. Toplevel? top = Application.Current!.Modal ? Application.Current : Application.Top;
  173. top!.AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabGroup);
  174. if (top.Focused is null)
  175. {
  176. top.AdvanceFocus (NavigationDirection.Backward, null);
  177. }
  178. top.SetNeedsDisplay ();
  179. ApplicationOverlapped.BringOverlappedTopToFront ();
  180. }
  181. else
  182. {
  183. ApplicationOverlapped.OverlappedMovePrevious ();
  184. }
  185. }
  186. public static void ResetState ()
  187. {
  188. _focused?.Dispose ();
  189. _focused = null;
  190. FocusedChanged = null;
  191. }
  192. }