Application.Toplevel.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. namespace Terminal.Gui;
  2. public static partial class Application // Toplevel handling
  3. {
  4. /// <summary>Holds the stack of TopLevel views.</summary>
  5. // BUGBUG: Technically, this is not the full lst of TopLevels. There be dragons here, e.g. see how Toplevel.Id is used. What
  6. // about TopLevels that are just a SubView of another View?
  7. internal static readonly Stack<Toplevel> _topLevels = new ();
  8. /// <summary>The <see cref="Toplevel"/> object used for the application on startup (<seealso cref="Top"/>)</summary>
  9. /// <value>The top.</value>
  10. public static Toplevel Top { get; private set; }
  11. /// <summary>
  12. /// The current <see cref="Toplevel"/> object. This is updated in <see cref="Application.Begin"/> enters and leaves to
  13. /// point to the current
  14. /// <see cref="Toplevel"/> .
  15. /// </summary>
  16. /// <remarks>
  17. /// This will only be distinct from <see cref="Application.Top"/> in scenarios where <see cref="Toplevel.IsOverlappedContainer"/> is <see langword="true"/>.
  18. /// </remarks>
  19. /// <value>The current.</value>
  20. public static Toplevel Current { get; private set; }
  21. private static void EnsureModalOrVisibleAlwaysOnTop (Toplevel topLevel)
  22. {
  23. if (!topLevel.Running
  24. || (topLevel == Current && topLevel.Visible)
  25. || OverlappedTop == null
  26. || _topLevels.Peek ().Modal)
  27. {
  28. return;
  29. }
  30. foreach (Toplevel top in _topLevels.Reverse ())
  31. {
  32. if (top.Modal && top != Current)
  33. {
  34. MoveCurrent (top);
  35. return;
  36. }
  37. }
  38. if (!topLevel.Visible && topLevel == Current)
  39. {
  40. OverlappedMoveNext ();
  41. }
  42. }
  43. private static Toplevel FindDeepestTop (Toplevel start, in Point location)
  44. {
  45. if (!start.Frame.Contains (location))
  46. {
  47. return null;
  48. }
  49. if (_topLevels is { Count: > 0 })
  50. {
  51. int rx = location.X - start.Frame.X;
  52. int ry = location.Y - start.Frame.Y;
  53. foreach (Toplevel t in _topLevels)
  54. {
  55. if (t != Current)
  56. {
  57. if (t != start && t.Visible && t.Frame.Contains (rx, ry))
  58. {
  59. start = t;
  60. break;
  61. }
  62. }
  63. }
  64. }
  65. return start;
  66. }
  67. private static View FindTopFromView (View view)
  68. {
  69. View top = view?.SuperView is { } && view?.SuperView != Top
  70. ? view.SuperView
  71. : view;
  72. while (top?.SuperView is { } && top?.SuperView != Top)
  73. {
  74. top = top.SuperView;
  75. }
  76. return top;
  77. }
  78. private static bool MoveCurrent (Toplevel top)
  79. {
  80. // The Current is modal and the top is not modal Toplevel then
  81. // the Current must be moved above the first not modal Toplevel.
  82. if (OverlappedTop is { }
  83. && top != OverlappedTop
  84. && top != Current
  85. && Current?.Modal == true
  86. && !_topLevels.Peek ().Modal)
  87. {
  88. lock (_topLevels)
  89. {
  90. _topLevels.MoveTo (Current, 0, new ToplevelEqualityComparer ());
  91. }
  92. var index = 0;
  93. Toplevel [] savedToplevels = _topLevels.ToArray ();
  94. foreach (Toplevel t in savedToplevels)
  95. {
  96. if (!t!.Modal && t != Current && t != top && t != savedToplevels [index])
  97. {
  98. lock (_topLevels)
  99. {
  100. _topLevels.MoveTo (top, index, new ToplevelEqualityComparer ());
  101. }
  102. }
  103. index++;
  104. }
  105. return false;
  106. }
  107. // The Current and the top are both not running Toplevel then
  108. // the top must be moved above the first not running Toplevel.
  109. if (OverlappedTop is { }
  110. && top != OverlappedTop
  111. && top != Current
  112. && Current?.Running == false
  113. && top?.Running == false)
  114. {
  115. lock (_topLevels)
  116. {
  117. _topLevels.MoveTo (Current, 0, new ToplevelEqualityComparer ());
  118. }
  119. var index = 0;
  120. foreach (Toplevel t in _topLevels.ToArray ())
  121. {
  122. if (!t.Running && t != Current && index > 0)
  123. {
  124. lock (_topLevels)
  125. {
  126. _topLevels.MoveTo (top, index - 1, new ToplevelEqualityComparer ());
  127. }
  128. }
  129. index++;
  130. }
  131. return false;
  132. }
  133. if ((OverlappedTop is { } && top?.Modal == true && _topLevels.Peek () != top)
  134. || (OverlappedTop is { } && Current != OverlappedTop && Current?.Modal == false && top == OverlappedTop)
  135. || (OverlappedTop is { } && Current?.Modal == false && top != Current)
  136. || (OverlappedTop is { } && Current?.Modal == true && top == OverlappedTop))
  137. {
  138. lock (_topLevels)
  139. {
  140. _topLevels.MoveTo (top, 0, new ToplevelEqualityComparer ());
  141. Current = top;
  142. }
  143. }
  144. return true;
  145. }
  146. /// <summary>Invoked when the terminal's size changed. The new size of the terminal is provided.</summary>
  147. /// <remarks>
  148. /// Event handlers can set <see cref="SizeChangedEventArgs.Cancel"/> to <see langword="true"/> to prevent
  149. /// <see cref="Application"/> from changing it's size to match the new terminal size.
  150. /// </remarks>
  151. public static event EventHandler<SizeChangedEventArgs> SizeChanging;
  152. /// <summary>
  153. /// Called when the application's size changes. Sets the size of all <see cref="Toplevel"/>s and fires the
  154. /// <see cref="SizeChanging"/> event.
  155. /// </summary>
  156. /// <param name="args">The new size.</param>
  157. /// <returns><see lanword="true"/>if the size was changed.</returns>
  158. public static bool OnSizeChanging (SizeChangedEventArgs args)
  159. {
  160. SizeChanging?.Invoke (null, args);
  161. if (args.Cancel || args.Size is null)
  162. {
  163. return false;
  164. }
  165. foreach (Toplevel t in _topLevels)
  166. {
  167. t.SetRelativeLayout (args.Size.Value);
  168. t.LayoutSubviews ();
  169. t.PositionToplevels ();
  170. t.OnSizeChanging (new (args.Size));
  171. if (PositionCursor (t))
  172. {
  173. Driver.UpdateCursor ();
  174. }
  175. }
  176. Refresh ();
  177. return true;
  178. }
  179. }