Application.Navigation.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. namespace Terminal.Gui;
  2. public static partial class Application
  3. {
  4. /// <summary>
  5. /// Gets the list of the Overlapped children which are not modal <see cref="Toplevel"/> from the
  6. /// <see cref="OverlappedTop"/>.
  7. /// </summary>
  8. public static List<Toplevel> OverlappedChildren
  9. {
  10. get
  11. {
  12. if (OverlappedTop is { })
  13. {
  14. List<Toplevel> _overlappedChildren = new ();
  15. foreach (Toplevel top in _topLevels)
  16. {
  17. if (top != OverlappedTop && !top.Modal)
  18. {
  19. _overlappedChildren.Add (top);
  20. }
  21. }
  22. return _overlappedChildren;
  23. }
  24. return null;
  25. }
  26. }
  27. #nullable enable
  28. /// <summary>
  29. /// The <see cref="Toplevel"/> object used for the application on startup which
  30. /// <see cref="Toplevel.IsOverlappedContainer"/> is true.
  31. /// </summary>
  32. public static Toplevel? OverlappedTop
  33. {
  34. get
  35. {
  36. if (Top is { IsOverlappedContainer: true })
  37. {
  38. return Top;
  39. }
  40. return null;
  41. }
  42. }
  43. #nullable restore
  44. /// <summary>Brings the superview of the most focused overlapped view is on front.</summary>
  45. public static void BringOverlappedTopToFront ()
  46. {
  47. if (OverlappedTop is { })
  48. {
  49. return;
  50. }
  51. View top = FindTopFromView (Top?.MostFocused);
  52. if (top is Toplevel && Top.Subviews.Count > 1 && Top.Subviews [^1] != top)
  53. {
  54. Top.BringSubviewToFront (top);
  55. }
  56. }
  57. /// <summary>Gets the current visible Toplevel overlapped child that matches the arguments pattern.</summary>
  58. /// <param name="type">The type.</param>
  59. /// <param name="exclude">The strings to exclude.</param>
  60. /// <returns>The matched view.</returns>
  61. public static Toplevel GetTopOverlappedChild (Type type = null, string [] exclude = null)
  62. {
  63. if (OverlappedTop is null)
  64. {
  65. return null;
  66. }
  67. foreach (Toplevel top in OverlappedChildren)
  68. {
  69. if (type is { } && top.GetType () == type && exclude?.Contains (top.Data.ToString ()) == false)
  70. {
  71. return top;
  72. }
  73. if ((type is { } && top.GetType () != type) || exclude?.Contains (top.Data.ToString ()) == true)
  74. {
  75. continue;
  76. }
  77. return top;
  78. }
  79. return null;
  80. }
  81. /// <summary>
  82. /// Move to the next Overlapped child from the <see cref="OverlappedTop"/> and set it as the <see cref="Top"/> if
  83. /// it is not already.
  84. /// </summary>
  85. /// <param name="top"></param>
  86. /// <returns></returns>
  87. public static bool MoveToOverlappedChild (Toplevel top)
  88. {
  89. if (top.Visible && OverlappedTop is { } && Current?.Modal == false)
  90. {
  91. lock (_topLevels)
  92. {
  93. _topLevels.MoveTo (top, 0, new ToplevelEqualityComparer ());
  94. Current = top;
  95. }
  96. return true;
  97. }
  98. return false;
  99. }
  100. /// <summary>Move to the next Overlapped child from the <see cref="OverlappedTop"/>.</summary>
  101. public static void OverlappedMoveNext ()
  102. {
  103. if (OverlappedTop is { } && !Current.Modal)
  104. {
  105. lock (_topLevels)
  106. {
  107. _topLevels.MoveNext ();
  108. var isOverlapped = false;
  109. while (_topLevels.Peek () == OverlappedTop || !_topLevels.Peek ().Visible)
  110. {
  111. if (!isOverlapped && _topLevels.Peek () == OverlappedTop)
  112. {
  113. isOverlapped = true;
  114. }
  115. else if (isOverlapped && _topLevels.Peek () == OverlappedTop)
  116. {
  117. MoveCurrent (Top);
  118. break;
  119. }
  120. _topLevels.MoveNext ();
  121. }
  122. Current = _topLevels.Peek ();
  123. }
  124. }
  125. }
  126. /// <summary>Move to the previous Overlapped child from the <see cref="OverlappedTop"/>.</summary>
  127. public static void OverlappedMovePrevious ()
  128. {
  129. if (OverlappedTop is { } && !Current.Modal)
  130. {
  131. lock (_topLevels)
  132. {
  133. _topLevels.MovePrevious ();
  134. var isOverlapped = false;
  135. while (_topLevels.Peek () == OverlappedTop || !_topLevels.Peek ().Visible)
  136. {
  137. if (!isOverlapped && _topLevels.Peek () == OverlappedTop)
  138. {
  139. isOverlapped = true;
  140. }
  141. else if (isOverlapped && _topLevels.Peek () == OverlappedTop)
  142. {
  143. MoveCurrent (Top);
  144. break;
  145. }
  146. _topLevels.MovePrevious ();
  147. }
  148. Current = _topLevels.Peek ();
  149. }
  150. }
  151. }
  152. private static bool OverlappedChildNeedsDisplay ()
  153. {
  154. if (OverlappedTop is null)
  155. {
  156. return false;
  157. }
  158. foreach (Toplevel top in _topLevels)
  159. {
  160. if (top != Current && top.Visible && (top.NeedsDisplay || top.SubViewNeedsDisplay || top.LayoutNeeded))
  161. {
  162. OverlappedTop.SetSubViewNeedsDisplay ();
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. private static bool SetCurrentOverlappedAsTop ()
  169. {
  170. if (OverlappedTop is null && Current != Top && Current?.SuperView is null && Current?.Modal == false)
  171. {
  172. Top = Current;
  173. return true;
  174. }
  175. return false;
  176. }
  177. }