Application.Navigation.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // TODO: Support Application canceling the HasFocus change
  33. /// <summary>
  34. /// Gets whether <paramref name="view"/> is in the Subview hierarchy of <paramref name="start"/>.
  35. /// </summary>
  36. /// <param name="start"></param>
  37. /// <param name="view"></param>
  38. /// <returns></returns>
  39. public static bool IsInHierarchy (View start, View? view)
  40. {
  41. if (view is null)
  42. {
  43. return false;
  44. }
  45. if (view == start)
  46. {
  47. return true;
  48. }
  49. foreach (View subView in start.Subviews)
  50. {
  51. if (view == subView)
  52. {
  53. return true;
  54. }
  55. var found = IsInHierarchy (subView, view);
  56. if (found)
  57. {
  58. return found;
  59. }
  60. }
  61. return false;
  62. }
  63. /// <summary>
  64. /// Raised when the most focused <see cref="View"/> in the application has changed.
  65. /// </summary>
  66. public static event EventHandler<EventArgs>? FocusedChanged;
  67. /// <summary>
  68. /// Gets the deepest focused subview of the specified <paramref name="view"/>.
  69. /// </summary>
  70. /// <param name="view"></param>
  71. /// <returns></returns>
  72. internal static View? GetDeepestFocusedSubview (View? view)
  73. {
  74. if (view is null)
  75. {
  76. return null;
  77. }
  78. foreach (View v in view.Subviews)
  79. {
  80. if (v.HasFocus)
  81. {
  82. return GetDeepestFocusedSubview (v);
  83. }
  84. }
  85. return view;
  86. }
  87. /// <summary>
  88. /// Moves the focus to the next focusable view.
  89. /// Honors <see cref="ViewArrangement.Overlapped"/> and will only move to the next subview
  90. /// if the current and next subviews are not overlapped.
  91. /// </summary>
  92. internal static void MoveNextView ()
  93. {
  94. View? old = GetDeepestFocusedSubview (Application.Current!.Focused);
  95. if (!Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop))
  96. {
  97. Application.Current.AdvanceFocus (NavigationDirection.Forward, null);
  98. }
  99. if (old != Application.Current.Focused && old != Application.Current.Focused?.Focused)
  100. {
  101. old?.SetNeedsDisplay ();
  102. Application.Current.Focused?.SetNeedsDisplay ();
  103. }
  104. else
  105. {
  106. ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes, NavigationDirection.Forward);
  107. }
  108. }
  109. /// <summary>
  110. /// Moves the focus to the next <see cref="Toplevel"/> subview or the next subview that has <see cref="ApplicationOverlapped.OverlappedTop"/> set.
  111. /// </summary>
  112. internal static void MoveNextViewOrTop ()
  113. {
  114. if (ApplicationOverlapped.OverlappedTop is null)
  115. {
  116. Toplevel? top = Application.Current!.Modal ? Application.Current : Application.Top;
  117. if (!Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabGroup))
  118. {
  119. Application.Current.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  120. if (Application.Current.Focused is null)
  121. {
  122. Application.Current.RestoreFocus (TabBehavior.TabGroup);
  123. }
  124. }
  125. if (top != Application.Current.Focused && top != Application.Current.Focused?.Focused)
  126. {
  127. top?.SetNeedsDisplay ();
  128. Application.Current.Focused?.SetNeedsDisplay ();
  129. }
  130. else
  131. {
  132. ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes, NavigationDirection.Forward);
  133. }
  134. //top!.AdvanceFocus (NavigationDirection.Forward);
  135. //if (top.Focused is null)
  136. //{
  137. // top.AdvanceFocus (NavigationDirection.Forward);
  138. //}
  139. //top.SetNeedsDisplay ();
  140. ApplicationOverlapped.BringOverlappedTopToFront ();
  141. }
  142. else
  143. {
  144. ApplicationOverlapped.OverlappedMoveNext ();
  145. }
  146. }
  147. // TODO: These methods should return bool to indicate if the focus was moved or not.
  148. /// <summary>
  149. /// Moves the focus to the next view. Honors <see cref="ViewArrangement.Overlapped"/> and will only move to the next subview
  150. /// if the current and next subviews are not overlapped.
  151. /// </summary>
  152. internal static void MovePreviousView ()
  153. {
  154. View? old = GetDeepestFocusedSubview (Application.Current!.Focused);
  155. if (!Application.Current.AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop))
  156. {
  157. Application.Current.AdvanceFocus (NavigationDirection.Backward, null);
  158. }
  159. if (old != Application.Current.Focused && old != Application.Current.Focused?.Focused)
  160. {
  161. old?.SetNeedsDisplay ();
  162. Application.Current.Focused?.SetNeedsDisplay ();
  163. }
  164. else
  165. {
  166. ApplicationOverlapped.SetFocusToNextViewWithWrap (Application.Current.SuperView?.TabIndexes?.Reverse (), NavigationDirection.Backward);
  167. }
  168. }
  169. internal static void MovePreviousViewOrTop ()
  170. {
  171. if (ApplicationOverlapped.OverlappedTop is null)
  172. {
  173. Toplevel? top = Application.Current!.Modal ? Application.Current : Application.Top;
  174. top!.AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabGroup);
  175. if (top.Focused is null)
  176. {
  177. top.AdvanceFocus (NavigationDirection.Backward, null);
  178. }
  179. top.SetNeedsDisplay ();
  180. ApplicationOverlapped.BringOverlappedTopToFront ();
  181. }
  182. else
  183. {
  184. ApplicationOverlapped.OverlappedMovePrevious ();
  185. }
  186. }
  187. public static void ResetState ()
  188. {
  189. _focused?.Dispose ();
  190. _focused = null;
  191. FocusedChanged = null;
  192. }
  193. }