Application.Navigation.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. public static partial class Application
  4. {
  5. /// <summary>
  6. /// Gets the list of the Overlapped children which are not modal <see cref="Toplevel"/> from the
  7. /// <see cref="OverlappedTop"/>.
  8. /// </summary>
  9. public static List<Toplevel>? OverlappedChildren
  10. {
  11. get
  12. {
  13. if (OverlappedTop is { })
  14. {
  15. List<Toplevel> overlappedChildren = new ();
  16. lock (_topLevels)
  17. {
  18. foreach (Toplevel top in _topLevels)
  19. {
  20. if (top != OverlappedTop && !top.Modal)
  21. {
  22. overlappedChildren.Add (top);
  23. }
  24. }
  25. }
  26. return overlappedChildren;
  27. }
  28. return null;
  29. }
  30. }
  31. /// <summary>
  32. /// The <see cref="Toplevel"/> object used for the application on startup which
  33. /// <see cref="Toplevel.IsOverlappedContainer"/> is true.
  34. /// </summary>
  35. public static Toplevel? OverlappedTop
  36. {
  37. get
  38. {
  39. if (Top is { IsOverlappedContainer: true })
  40. {
  41. return Top;
  42. }
  43. return null;
  44. }
  45. }
  46. /// <summary>Brings the superview of the most focused overlapped view is on front.</summary>
  47. public static void BringOverlappedTopToFront ()
  48. {
  49. if (OverlappedTop is { })
  50. {
  51. return;
  52. }
  53. View? top = FindTopFromView (Top?.MostFocused);
  54. if (top is Toplevel && Top?.Subviews.Count > 1 && Top.Subviews [^1] != top)
  55. {
  56. Top.BringSubviewToFront (top);
  57. }
  58. }
  59. /// <summary>Gets the current visible Toplevel overlapped child that matches the arguments pattern.</summary>
  60. /// <param name="type">The type.</param>
  61. /// <param name="exclude">The strings to exclude.</param>
  62. /// <returns>The matched view.</returns>
  63. public static Toplevel? GetTopOverlappedChild (Type? type = null, string []? exclude = null)
  64. {
  65. if (OverlappedChildren is null || OverlappedTop is null)
  66. {
  67. return null;
  68. }
  69. foreach (Toplevel top in OverlappedChildren)
  70. {
  71. if (type is { } && top.GetType () == type && exclude?.Contains (top.Data.ToString ()) == false)
  72. {
  73. return top;
  74. }
  75. if ((type is { } && top.GetType () != type) || exclude?.Contains (top.Data.ToString ()) == true)
  76. {
  77. continue;
  78. }
  79. return top;
  80. }
  81. return null;
  82. }
  83. /// <summary>
  84. /// Move to the next Overlapped child from the <see cref="OverlappedTop"/> and set it as the <see cref="Top"/> if
  85. /// it is not already.
  86. /// </summary>
  87. /// <param name="top"></param>
  88. /// <returns></returns>
  89. public static bool MoveToOverlappedChild (Toplevel top)
  90. {
  91. if (top.Visible && OverlappedTop is { } && Current?.Modal == false)
  92. {
  93. lock (_topLevels)
  94. {
  95. _topLevels.MoveTo (top, 0, new ToplevelEqualityComparer ());
  96. Current = top;
  97. }
  98. return true;
  99. }
  100. return false;
  101. }
  102. /// <summary>Move to the next Overlapped child from the <see cref="OverlappedTop"/>.</summary>
  103. public static void OverlappedMoveNext ()
  104. {
  105. if (OverlappedTop is { } && !Current!.Modal)
  106. {
  107. lock (_topLevels)
  108. {
  109. _topLevels.MoveNext ();
  110. var isOverlapped = false;
  111. while (_topLevels.Peek () == OverlappedTop || !_topLevels.Peek ().Visible)
  112. {
  113. if (!isOverlapped && _topLevels.Peek () == OverlappedTop)
  114. {
  115. isOverlapped = true;
  116. }
  117. else if (isOverlapped && _topLevels.Peek () == OverlappedTop)
  118. {
  119. MoveCurrent (Top!);
  120. break;
  121. }
  122. _topLevels.MoveNext ();
  123. }
  124. Current = _topLevels.Peek ();
  125. }
  126. }
  127. }
  128. /// <summary>Move to the previous Overlapped child from the <see cref="OverlappedTop"/>.</summary>
  129. public static void OverlappedMovePrevious ()
  130. {
  131. if (OverlappedTop is { } && !Current!.Modal)
  132. {
  133. lock (_topLevels)
  134. {
  135. _topLevels.MovePrevious ();
  136. var isOverlapped = false;
  137. while (_topLevels.Peek () == OverlappedTop || !_topLevels.Peek ().Visible)
  138. {
  139. if (!isOverlapped && _topLevels.Peek () == OverlappedTop)
  140. {
  141. isOverlapped = true;
  142. }
  143. else if (isOverlapped && _topLevels.Peek () == OverlappedTop)
  144. {
  145. MoveCurrent (Top!);
  146. break;
  147. }
  148. _topLevels.MovePrevious ();
  149. }
  150. Current = _topLevels.Peek ();
  151. }
  152. }
  153. }
  154. private static bool OverlappedChildNeedsDisplay ()
  155. {
  156. if (OverlappedTop is null)
  157. {
  158. return false;
  159. }
  160. lock (_topLevels)
  161. {
  162. foreach (Toplevel top in _topLevels)
  163. {
  164. if (top != Current && top.Visible && (top.NeedsDisplay || top.SubViewNeedsDisplay || top.LayoutNeeded))
  165. {
  166. OverlappedTop.SetSubViewNeedsDisplay ();
  167. return true;
  168. }
  169. }
  170. }
  171. return false;
  172. }
  173. private static bool SetCurrentOverlappedAsTop ()
  174. {
  175. if (OverlappedTop is null && Current != Top && Current?.SuperView is null && Current?.Modal == false)
  176. {
  177. Top = Current;
  178. return true;
  179. }
  180. return false;
  181. }
  182. /// <summary>
  183. /// Finds the first Toplevel in the stack that is Visible and who's Frame contains the <paramref name="location"/>.
  184. /// </summary>
  185. /// <param name="start"></param>
  186. /// <param name="location"></param>
  187. /// <returns></returns>
  188. private static Toplevel? FindDeepestTop (Toplevel start, in Point location)
  189. {
  190. if (!start.Frame.Contains (location))
  191. {
  192. return null;
  193. }
  194. lock (_topLevels)
  195. {
  196. if (_topLevels is not { Count: > 0 })
  197. {
  198. return start;
  199. }
  200. int rx = location.X - start.Frame.X;
  201. int ry = location.Y - start.Frame.Y;
  202. foreach (Toplevel t in _topLevels)
  203. {
  204. if (t == Current)
  205. {
  206. continue;
  207. }
  208. if (t != start && t.Visible && t.Frame.Contains (rx, ry))
  209. {
  210. start = t;
  211. break;
  212. }
  213. }
  214. }
  215. return start;
  216. }
  217. /// <summary>
  218. /// Given <paramref name="view"/>, returns the first Superview up the chain that is <see cref="Top"/>.
  219. /// </summary>
  220. private static View? FindTopFromView (View? view)
  221. {
  222. if (view is null)
  223. {
  224. return null;
  225. }
  226. View top = view.SuperView is { } && view.SuperView != Top
  227. ? view.SuperView
  228. : view;
  229. while (top?.SuperView is { } && top?.SuperView != Top)
  230. {
  231. top = top!.SuperView;
  232. }
  233. return top;
  234. }
  235. /// <summary>
  236. /// If the <see cref="Current"/> is not the <paramref name="top"/> then <paramref name="top"/> is moved to the top of
  237. /// the Toplevel stack and made Current.
  238. /// </summary>
  239. /// <param name="top"></param>
  240. /// <returns></returns>
  241. private static bool MoveCurrent (Toplevel top)
  242. {
  243. // The Current is modal and the top is not modal Toplevel then
  244. // the Current must be moved above the first not modal Toplevel.
  245. if (OverlappedTop is { }
  246. && top != OverlappedTop
  247. && top != Current
  248. && Current?.Modal == true
  249. && !_topLevels.Peek ().Modal)
  250. {
  251. lock (_topLevels)
  252. {
  253. _topLevels.MoveTo (Current, 0, new ToplevelEqualityComparer ());
  254. }
  255. var index = 0;
  256. Toplevel [] savedToplevels = _topLevels.ToArray ();
  257. foreach (Toplevel t in savedToplevels)
  258. {
  259. if (!t!.Modal && t != Current && t != top && t != savedToplevels [index])
  260. {
  261. lock (_topLevels)
  262. {
  263. _topLevels.MoveTo (top, index, new ToplevelEqualityComparer ());
  264. }
  265. }
  266. index++;
  267. }
  268. return false;
  269. }
  270. // The Current and the top are both not running Toplevel then
  271. // the top must be moved above the first not running Toplevel.
  272. if (OverlappedTop is { }
  273. && top != OverlappedTop
  274. && top != Current
  275. && Current?.Running == false
  276. && top?.Running == false)
  277. {
  278. lock (_topLevels)
  279. {
  280. _topLevels.MoveTo (Current, 0, new ToplevelEqualityComparer ());
  281. }
  282. var index = 0;
  283. foreach (Toplevel t in _topLevels.ToArray ())
  284. {
  285. if (!t.Running && t != Current && index > 0)
  286. {
  287. lock (_topLevels)
  288. {
  289. _topLevels.MoveTo (top, index - 1, new ToplevelEqualityComparer ());
  290. }
  291. }
  292. index++;
  293. }
  294. return false;
  295. }
  296. if ((OverlappedTop is { } && top?.Modal == true && _topLevels.Peek () != top)
  297. || (OverlappedTop is { } && Current != OverlappedTop && Current?.Modal == false && top == OverlappedTop)
  298. || (OverlappedTop is { } && Current?.Modal == false && top != Current)
  299. || (OverlappedTop is { } && Current?.Modal == true && top == OverlappedTop))
  300. {
  301. lock (_topLevels)
  302. {
  303. _topLevels.MoveTo (top, 0, new ToplevelEqualityComparer ());
  304. Current = top;
  305. }
  306. }
  307. return true;
  308. }
  309. }