ApplicationOverlapped.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #nullable enable
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// Helper class for managing overlapped views in the application.
  7. /// </summary>
  8. public static class ApplicationOverlapped
  9. {
  10. /// <summary>
  11. /// Gets or sets if <paramref name="top"/> is in overlapped mode within a Toplevel container.
  12. /// </summary>
  13. /// <param name="top"></param>
  14. /// <returns></returns>
  15. public static bool IsOverlapped (Toplevel? top)
  16. {
  17. return ApplicationOverlapped.OverlappedTop is { } && ApplicationOverlapped.OverlappedTop != top && !top!.Modal;
  18. }
  19. /// <summary>
  20. /// Gets the list of the Overlapped children which are not modal <see cref="Toplevel"/> from the
  21. /// <see cref="OverlappedTop"/>.
  22. /// </summary>
  23. public static List<Toplevel>? OverlappedChildren
  24. {
  25. get
  26. {
  27. if (OverlappedTop is { })
  28. {
  29. List<Toplevel> overlappedChildren = new ();
  30. lock (Application.TopLevels)
  31. {
  32. foreach (Toplevel top in Application.TopLevels)
  33. {
  34. if (top != OverlappedTop && !top.Modal)
  35. {
  36. overlappedChildren.Add (top);
  37. }
  38. }
  39. }
  40. return overlappedChildren;
  41. }
  42. return null;
  43. }
  44. }
  45. /// <summary>
  46. /// The <see cref="Toplevel"/> object used for the application on startup which
  47. /// <see cref="Toplevel.IsOverlappedContainer"/> is true.
  48. /// </summary>
  49. public static Toplevel? OverlappedTop
  50. {
  51. get
  52. {
  53. if (Application.Top is { IsOverlappedContainer: true })
  54. {
  55. return Application.Top;
  56. }
  57. return null;
  58. }
  59. }
  60. /// <summary>Brings the superview of the most focused overlapped view is on front.</summary>
  61. public static void BringOverlappedTopToFront ()
  62. {
  63. if (OverlappedTop is { })
  64. {
  65. return;
  66. }
  67. View? top = FindTopFromView (Application.Top?.MostFocused);
  68. if (top is Toplevel && Application.Top?.Subviews.Count > 1 && Application.Top.Subviews [^1] != top)
  69. {
  70. Application.Top.MoveSubviewToStart (top);
  71. }
  72. }
  73. /// <summary>Gets the current visible Toplevel overlapped child that matches the arguments pattern.</summary>
  74. /// <param name="type">The type.</param>
  75. /// <param name="exclude">The strings to exclude.</param>
  76. /// <returns>The matched view.</returns>
  77. public static Toplevel? GetTopOverlappedChild (Type? type = null, string []? exclude = null)
  78. {
  79. if (OverlappedChildren is null || OverlappedTop is null)
  80. {
  81. return null;
  82. }
  83. foreach (Toplevel top in OverlappedChildren)
  84. {
  85. if (type is { } && top.GetType () == type && exclude?.Contains (top.Data?.ToString ()) == false)
  86. {
  87. return top;
  88. }
  89. if ((type is { } && top.GetType () != type) || exclude?.Contains (top.Data?.ToString ()) == true)
  90. {
  91. continue;
  92. }
  93. return top;
  94. }
  95. return null;
  96. }
  97. /// <summary>
  98. /// Sets the focus to the next view in the specified direction within the provided list of views.
  99. /// If the end of the list is reached, the focus wraps around to the first view in the list.
  100. /// The method considers the current focused view (`Application.Current`) and attempts to move the focus
  101. /// to the next view in the specified direction. If the focus cannot be set to the next view, it wraps around
  102. /// to the first view in the list.
  103. /// </summary>
  104. /// <param name="viewsInTabIndexes"></param>
  105. /// <param name="direction"></param>
  106. internal static void SetFocusToNextViewWithWrap (IEnumerable<View>? viewsInTabIndexes, NavigationDirection direction)
  107. {
  108. if (viewsInTabIndexes is null)
  109. {
  110. return;
  111. }
  112. // This code-path only executes in obtuse IsOverlappedContainer scenarios.
  113. Debug.Assert (Application.Current!.IsOverlappedContainer);
  114. bool foundCurrentView = false;
  115. bool focusSet = false;
  116. IEnumerable<View> indexes = viewsInTabIndexes as View [] ?? viewsInTabIndexes.ToArray ();
  117. int viewCount = indexes.Count ();
  118. int currentIndex = 0;
  119. foreach (View view in indexes)
  120. {
  121. if (view == Application.Current)
  122. {
  123. foundCurrentView = true;
  124. }
  125. else if (foundCurrentView && !focusSet)
  126. {
  127. // One of the views is Current, but view is not. Attempt to Advance...
  128. Application.Current!.SuperView?.AdvanceFocus (direction, null);
  129. // QUESTION: AdvanceFocus returns false AND sets Focused to null if no view was found to advance to. Should't we only set focusProcessed if it returned true?
  130. focusSet = true;
  131. if (Application.Current.SuperView?.Focused != Application.Current)
  132. {
  133. return;
  134. }
  135. // Either AdvanceFocus didn't set focus or the view it set focus to is not current...
  136. // continue...
  137. }
  138. currentIndex++;
  139. if (foundCurrentView && !focusSet && currentIndex == viewCount)
  140. {
  141. // One of the views is Current AND AdvanceFocus didn't set focus AND we are at the last view in the list...
  142. // This means we should wrap around to the first view in the list.
  143. indexes.First ().SetFocus ();
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// Move to the next Overlapped child from the <see cref="OverlappedTop"/> and set it as the <see cref="Application.Top"/> if
  149. /// it is not already.
  150. /// </summary>
  151. /// <param name="top"></param>
  152. /// <returns></returns>
  153. public static bool MoveToOverlappedChild (Toplevel? top)
  154. {
  155. ArgumentNullException.ThrowIfNull (top);
  156. if (top.Visible && OverlappedTop is { } && Application.Current?.Modal == false)
  157. {
  158. lock (Application.TopLevels)
  159. {
  160. Application.TopLevels.MoveTo (top, 0, new ToplevelEqualityComparer ());
  161. Application.Current = top;
  162. }
  163. return true;
  164. }
  165. return false;
  166. }
  167. /// <summary>Move to the next Overlapped child from the <see cref="OverlappedTop"/>.</summary>
  168. public static void OverlappedMoveNext ()
  169. {
  170. if (OverlappedTop is { } && !Application.Current!.Modal)
  171. {
  172. lock (Application.TopLevels)
  173. {
  174. Application.TopLevels.MoveNext ();
  175. var isOverlapped = false;
  176. while (Application.TopLevels.Peek () == OverlappedTop || !Application.TopLevels.Peek ().Visible)
  177. {
  178. if (!isOverlapped && Application.TopLevels.Peek () == OverlappedTop)
  179. {
  180. isOverlapped = true;
  181. }
  182. else if (isOverlapped && Application.TopLevels.Peek () == OverlappedTop)
  183. {
  184. MoveCurrent (Application.Top!);
  185. break;
  186. }
  187. Application.TopLevels.MoveNext ();
  188. }
  189. Application.Current = Application.TopLevels.Peek ();
  190. }
  191. }
  192. }
  193. /// <summary>Move to the previous Overlapped child from the <see cref="OverlappedTop"/>.</summary>
  194. public static void OverlappedMovePrevious ()
  195. {
  196. if (OverlappedTop is { } && !Application.Current!.Modal)
  197. {
  198. lock (Application.TopLevels)
  199. {
  200. Application.TopLevels.MovePrevious ();
  201. var isOverlapped = false;
  202. while (Application.TopLevels.Peek () == OverlappedTop || !Application.TopLevels.Peek ().Visible)
  203. {
  204. if (!isOverlapped && Application.TopLevels.Peek () == OverlappedTop)
  205. {
  206. isOverlapped = true;
  207. }
  208. else if (isOverlapped && Application.TopLevels.Peek () == OverlappedTop)
  209. {
  210. MoveCurrent (Application.Top!);
  211. break;
  212. }
  213. Application.TopLevels.MovePrevious ();
  214. }
  215. Application.Current = Application.TopLevels.Peek ();
  216. }
  217. }
  218. }
  219. internal static bool OverlappedChildNeedsDisplay ()
  220. {
  221. if (OverlappedTop is null)
  222. {
  223. return false;
  224. }
  225. lock (Application.TopLevels)
  226. {
  227. foreach (Toplevel top in Application.TopLevels)
  228. {
  229. if (top != Application.Current && top.Visible && (top.NeedsDisplay || top.SubViewNeedsDisplay || top.LayoutNeeded))
  230. {
  231. OverlappedTop.SetSubViewNeedsDisplay ();
  232. return true;
  233. }
  234. }
  235. }
  236. return false;
  237. }
  238. internal static bool SetCurrentOverlappedAsTop ()
  239. {
  240. if (OverlappedTop is null && Application.Current != Application.Top && Application.Current?.SuperView is null && Application.Current?.Modal == false)
  241. {
  242. Application.Top = Application.Current;
  243. return true;
  244. }
  245. return false;
  246. }
  247. /// <summary>
  248. /// Finds the first Toplevel in the stack that is Visible and who's Frame contains the <paramref name="location"/>.
  249. /// </summary>
  250. /// <param name="start"></param>
  251. /// <param name="location"></param>
  252. /// <returns></returns>
  253. internal static Toplevel? FindDeepestTop (Toplevel start, in Point location)
  254. {
  255. if (!start.Frame.Contains (location))
  256. {
  257. return null;
  258. }
  259. lock (Application.TopLevels)
  260. {
  261. if (Application.TopLevels is not { Count: > 0 })
  262. {
  263. return start;
  264. }
  265. int rx = location.X - start.Frame.X;
  266. int ry = location.Y - start.Frame.Y;
  267. foreach (Toplevel t in Application.TopLevels)
  268. {
  269. if (t == Application.Current)
  270. {
  271. continue;
  272. }
  273. if (t != start && t.Visible && t.Frame.Contains (rx, ry))
  274. {
  275. start = t;
  276. break;
  277. }
  278. }
  279. }
  280. return start;
  281. }
  282. /// <summary>
  283. /// Given <paramref name="view"/>, returns the first Superview up the chain that is <see cref="Application.Top"/>.
  284. /// </summary>
  285. internal static View? FindTopFromView (View? view)
  286. {
  287. if (view is null)
  288. {
  289. return null;
  290. }
  291. View top = view.SuperView is { } && view.SuperView != Application.Top
  292. ? view.SuperView
  293. : view;
  294. while (top?.SuperView is { } && top?.SuperView != Application.Top)
  295. {
  296. top = top!.SuperView;
  297. }
  298. return top;
  299. }
  300. /// <summary>
  301. /// If the <see cref="Application.Current"/> is not the <paramref name="top"/> then <paramref name="top"/> is moved to the top of
  302. /// the Toplevel stack and made Current.
  303. /// </summary>
  304. /// <param name="top"></param>
  305. /// <returns></returns>
  306. internal static bool MoveCurrent (Toplevel top)
  307. {
  308. // The Current is modal and the top is not modal Toplevel then
  309. // the Current must be moved above the first not modal Toplevel.
  310. if (OverlappedTop is { }
  311. && top != OverlappedTop
  312. && top != Application.Current
  313. && Application.Current?.Modal == true
  314. && !Application.TopLevels.Peek ().Modal)
  315. {
  316. lock (Application.TopLevels)
  317. {
  318. Application.TopLevels.MoveTo (Application.Current, 0, new ToplevelEqualityComparer ());
  319. }
  320. var index = 0;
  321. Toplevel [] savedToplevels = Application.TopLevels.ToArray ();
  322. foreach (Toplevel t in savedToplevels)
  323. {
  324. if (!t!.Modal && t != Application.Current && t != top && t != savedToplevels [index])
  325. {
  326. lock (Application.TopLevels)
  327. {
  328. Application.TopLevels.MoveTo (top, index, new ToplevelEqualityComparer ());
  329. }
  330. }
  331. index++;
  332. }
  333. return false;
  334. }
  335. // The Current and the top are both not running Toplevel then
  336. // the top must be moved above the first not running Toplevel.
  337. if (OverlappedTop is { }
  338. && top != OverlappedTop
  339. && top != Application.Current
  340. && Application.Current?.Running == false
  341. && top?.Running == false)
  342. {
  343. lock (Application.TopLevels)
  344. {
  345. Application.TopLevels.MoveTo (Application.Current, 0, new ToplevelEqualityComparer ());
  346. }
  347. var index = 0;
  348. foreach (Toplevel t in Application.TopLevels.ToArray ())
  349. {
  350. if (!t.Running && t != Application.Current && index > 0)
  351. {
  352. lock (Application.TopLevels)
  353. {
  354. Application.TopLevels.MoveTo (top, index - 1, new ToplevelEqualityComparer ());
  355. }
  356. }
  357. index++;
  358. }
  359. return false;
  360. }
  361. if ((OverlappedTop is { } && top?.Modal == true && Application.TopLevels.Peek () != top)
  362. || (OverlappedTop is { } && Application.Current != OverlappedTop && Application.Current?.Modal == false && top == OverlappedTop)
  363. || (OverlappedTop is { } && Application.Current?.Modal == false && top != Application.Current)
  364. || (OverlappedTop is { } && Application.Current?.Modal == true && top == OverlappedTop))
  365. {
  366. lock (Application.TopLevels)
  367. {
  368. Application.TopLevels.MoveTo (top!, 0, new ToplevelEqualityComparer ());
  369. Application.Current = top;
  370. }
  371. }
  372. return true;
  373. }
  374. }