ApplicationNavigation.cs 6.8 KB

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