Application.Overlapped.cs 11 KB

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