Application.Navigation.cs 6.6 KB

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