Application.Overlapped.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. ArgumentNullException.ThrowIfNull (top);
  95. if (top.Visible && OverlappedTop is { } && Application.Current?.Modal == false)
  96. {
  97. lock (Application.TopLevels)
  98. {
  99. Application.TopLevels.MoveTo (top, 0, new ToplevelEqualityComparer ());
  100. Application.Current = top;
  101. }
  102. return true;
  103. }
  104. return false;
  105. }
  106. /// <summary>Move to the next Overlapped child from the <see cref="OverlappedTop"/>.</summary>
  107. public static void OverlappedMoveNext ()
  108. {
  109. if (OverlappedTop is { } && !Application.Current!.Modal)
  110. {
  111. lock (Application.TopLevels)
  112. {
  113. Application.TopLevels.MoveNext ();
  114. var isOverlapped = false;
  115. while (Application.TopLevels.Peek () == OverlappedTop || !Application.TopLevels.Peek ().Visible)
  116. {
  117. if (!isOverlapped && Application.TopLevels.Peek () == OverlappedTop)
  118. {
  119. isOverlapped = true;
  120. }
  121. else if (isOverlapped && Application.TopLevels.Peek () == OverlappedTop)
  122. {
  123. MoveCurrent (Application.Top!);
  124. break;
  125. }
  126. Application.TopLevels.MoveNext ();
  127. }
  128. Application.Current = Application.TopLevels.Peek ();
  129. }
  130. }
  131. }
  132. /// <summary>Move to the previous Overlapped child from the <see cref="OverlappedTop"/>.</summary>
  133. public static void OverlappedMovePrevious ()
  134. {
  135. if (OverlappedTop is { } && !Application.Current!.Modal)
  136. {
  137. lock (Application.TopLevels)
  138. {
  139. Application.TopLevels.MovePrevious ();
  140. var isOverlapped = false;
  141. while (Application.TopLevels.Peek () == OverlappedTop || !Application.TopLevels.Peek ().Visible)
  142. {
  143. if (!isOverlapped && Application.TopLevels.Peek () == OverlappedTop)
  144. {
  145. isOverlapped = true;
  146. }
  147. else if (isOverlapped && Application.TopLevels.Peek () == OverlappedTop)
  148. {
  149. MoveCurrent (Application.Top!);
  150. break;
  151. }
  152. Application.TopLevels.MovePrevious ();
  153. }
  154. Application.Current = Application.TopLevels.Peek ();
  155. }
  156. }
  157. }
  158. internal static bool OverlappedChildNeedsDisplay ()
  159. {
  160. if (OverlappedTop is null)
  161. {
  162. return false;
  163. }
  164. lock (Application.TopLevels)
  165. {
  166. foreach (Toplevel top in Application.TopLevels)
  167. {
  168. if (top != Application.Current && top.Visible && (top.NeedsDisplay || top.SubViewNeedsDisplay || top.LayoutNeeded))
  169. {
  170. OverlappedTop.SetSubViewNeedsDisplay ();
  171. return true;
  172. }
  173. }
  174. }
  175. return false;
  176. }
  177. internal static bool SetCurrentOverlappedAsTop ()
  178. {
  179. if (OverlappedTop is null && Application.Current != Application.Top && Application.Current?.SuperView is null && Application.Current?.Modal == false)
  180. {
  181. Application.Top = Application.Current;
  182. return true;
  183. }
  184. return false;
  185. }
  186. /// <summary>
  187. /// Finds the first Toplevel in the stack that is Visible and who's Frame contains the <paramref name="location"/>.
  188. /// </summary>
  189. /// <param name="start"></param>
  190. /// <param name="location"></param>
  191. /// <returns></returns>
  192. internal static Toplevel? FindDeepestTop (Toplevel start, in Point location)
  193. {
  194. if (!start.Frame.Contains (location))
  195. {
  196. return null;
  197. }
  198. lock (Application.TopLevels)
  199. {
  200. if (Application.TopLevels is not { Count: > 0 })
  201. {
  202. return start;
  203. }
  204. int rx = location.X - start.Frame.X;
  205. int ry = location.Y - start.Frame.Y;
  206. foreach (Toplevel t in Application.TopLevels)
  207. {
  208. if (t == Application.Current)
  209. {
  210. continue;
  211. }
  212. if (t != start && t.Visible && t.Frame.Contains (rx, ry))
  213. {
  214. start = t;
  215. break;
  216. }
  217. }
  218. }
  219. return start;
  220. }
  221. /// <summary>
  222. /// Given <paramref name="view"/>, returns the first Superview up the chain that is <see cref="Top"/>.
  223. /// </summary>
  224. internal static View? FindTopFromView (View? view)
  225. {
  226. if (view is null)
  227. {
  228. return null;
  229. }
  230. View top = view.SuperView is { } && view.SuperView != Application.Top
  231. ? view.SuperView
  232. : view;
  233. while (top?.SuperView is { } && top?.SuperView != Application.Top)
  234. {
  235. top = top!.SuperView;
  236. }
  237. return top;
  238. }
  239. /// <summary>
  240. /// If the <see cref="Current"/> is not the <paramref name="top"/> then <paramref name="top"/> is moved to the top of
  241. /// the Toplevel stack and made Current.
  242. /// </summary>
  243. /// <param name="top"></param>
  244. /// <returns></returns>
  245. internal static bool MoveCurrent (Toplevel top)
  246. {
  247. // The Current is modal and the top is not modal Toplevel then
  248. // the Current must be moved above the first not modal Toplevel.
  249. if (OverlappedTop is { }
  250. && top != OverlappedTop
  251. && top != Application.Current
  252. && Application.Current?.Modal == true
  253. && !Application.TopLevels.Peek ().Modal)
  254. {
  255. lock (Application.TopLevels)
  256. {
  257. Application.TopLevels.MoveTo (Application.Current, 0, new ToplevelEqualityComparer ());
  258. }
  259. var index = 0;
  260. Toplevel [] savedToplevels = Application.TopLevels.ToArray ();
  261. foreach (Toplevel t in savedToplevels)
  262. {
  263. if (!t!.Modal && t != Application.Current && t != top && t != savedToplevels [index])
  264. {
  265. lock (Application.TopLevels)
  266. {
  267. Application.TopLevels.MoveTo (top, index, new ToplevelEqualityComparer ());
  268. }
  269. }
  270. index++;
  271. }
  272. return false;
  273. }
  274. // The Current and the top are both not running Toplevel then
  275. // the top must be moved above the first not running Toplevel.
  276. if (OverlappedTop is { }
  277. && top != OverlappedTop
  278. && top != Application.Current
  279. && Application.Current?.Running == false
  280. && top?.Running == false)
  281. {
  282. lock (Application.TopLevels)
  283. {
  284. Application.TopLevels.MoveTo (Application.Current, 0, new ToplevelEqualityComparer ());
  285. }
  286. var index = 0;
  287. foreach (Toplevel t in Application.TopLevels.ToArray ())
  288. {
  289. if (!t.Running && t != Application.Current && index > 0)
  290. {
  291. lock (Application.TopLevels)
  292. {
  293. Application.TopLevels.MoveTo (top, index - 1, new ToplevelEqualityComparer ());
  294. }
  295. }
  296. index++;
  297. }
  298. return false;
  299. }
  300. if ((OverlappedTop is { } && top?.Modal == true && Application.TopLevels.Peek () != top)
  301. || (OverlappedTop is { } && Application.Current != OverlappedTop && Application.Current?.Modal == false && top == OverlappedTop)
  302. || (OverlappedTop is { } && Application.Current?.Modal == false && top != Application.Current)
  303. || (OverlappedTop is { } && Application.Current?.Modal == true && top == OverlappedTop))
  304. {
  305. lock (Application.TopLevels)
  306. {
  307. Application.TopLevels.MoveTo (top, 0, new ToplevelEqualityComparer ());
  308. Application.Current = top;
  309. }
  310. }
  311. return true;
  312. }
  313. }