Application.Run.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. #nullable enable
  2. using System.Diagnostics;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Text.Json.Serialization;
  5. using Microsoft.CodeAnalysis.Diagnostics;
  6. namespace Terminal.Gui;
  7. public static partial class Application // Run (Begin, Run, End, Stop)
  8. {
  9. private static Key _quitKey = Key.Esc; // Resources/config.json overrides
  10. /// <summary>Gets or sets the key to quit the application.</summary>
  11. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
  12. public static Key QuitKey
  13. {
  14. get => _quitKey;
  15. set
  16. {
  17. if (_quitKey != value)
  18. {
  19. KeyBindings.Replace (_quitKey, value);
  20. _quitKey = value;
  21. }
  22. }
  23. }
  24. private static Key _arrangeKey = Key.F5.WithCtrl; // Resources/config.json overrides
  25. /// <summary>Gets or sets the key to activate arranging views using the keyboard.</summary>
  26. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
  27. public static Key ArrangeKey
  28. {
  29. get => _arrangeKey;
  30. set
  31. {
  32. if (_arrangeKey != value)
  33. {
  34. KeyBindings.Replace (_arrangeKey, value);
  35. _arrangeKey = value;
  36. }
  37. }
  38. }
  39. // When `End ()` is called, it is possible `RunState.Toplevel` is a different object than `Top`.
  40. // This variable is set in `End` in this case so that `Begin` correctly sets `Top`.
  41. private static Toplevel? _cachedRunStateToplevel;
  42. /// <summary>
  43. /// Notify that a new <see cref="RunState"/> was created (<see cref="Begin(Toplevel)"/> was called). The token is
  44. /// created in <see cref="Begin(Toplevel)"/> and this event will be fired before that function exits.
  45. /// </summary>
  46. /// <remarks>
  47. /// If <see cref="EndAfterFirstIteration"/> is <see langword="true"/> callers to <see cref="Begin(Toplevel)"/>
  48. /// must also subscribe to <see cref="NotifyStopRunState"/> and manually dispose of the <see cref="RunState"/> token
  49. /// when the application is done.
  50. /// </remarks>
  51. public static event EventHandler<RunStateEventArgs>? NotifyNewRunState;
  52. /// <summary>Notify that an existent <see cref="RunState"/> is stopping (<see cref="End(RunState)"/> was called).</summary>
  53. /// <remarks>
  54. /// If <see cref="EndAfterFirstIteration"/> is <see langword="true"/> callers to <see cref="Begin(Toplevel)"/>
  55. /// must also subscribe to <see cref="NotifyStopRunState"/> and manually dispose of the <see cref="RunState"/> token
  56. /// when the application is done.
  57. /// </remarks>
  58. public static event EventHandler<ToplevelEventArgs>? NotifyStopRunState;
  59. /// <summary>Building block API: Prepares the provided <see cref="Toplevel"/> for execution.</summary>
  60. /// <returns>
  61. /// The <see cref="RunState"/> handle that needs to be passed to the <see cref="End(RunState)"/> method upon
  62. /// completion.
  63. /// </returns>
  64. /// <param name="toplevel">The <see cref="Toplevel"/> to prepare execution for.</param>
  65. /// <remarks>
  66. /// This method prepares the provided <see cref="Toplevel"/> for running with the focus, it adds this to the list
  67. /// of <see cref="Toplevel"/>s, lays out the Subviews, focuses the first element, and draws the <see cref="Toplevel"/>
  68. /// in the screen. This is usually followed by executing the <see cref="RunLoop"/> method, and then the
  69. /// <see cref="End(RunState)"/> method upon termination which will undo these changes.
  70. /// </remarks>
  71. public static RunState Begin (Toplevel toplevel)
  72. {
  73. ArgumentNullException.ThrowIfNull (toplevel);
  74. //#if DEBUG_IDISPOSABLE
  75. // Debug.Assert (!toplevel.WasDisposed);
  76. // if (_cachedRunStateToplevel is { } && _cachedRunStateToplevel != toplevel)
  77. // {
  78. // Debug.Assert (_cachedRunStateToplevel.WasDisposed);
  79. // }
  80. //#endif
  81. // Ensure the mouse is ungrabbed.
  82. MouseGrabView = null;
  83. var rs = new RunState (toplevel);
  84. #if DEBUG_IDISPOSABLE
  85. if (Top is { } && toplevel != Top && !TopLevels.Contains (Top))
  86. {
  87. // This assertion confirm if the Top was already disposed
  88. Debug.Assert (Top.WasDisposed);
  89. Debug.Assert (Top == _cachedRunStateToplevel);
  90. }
  91. #endif
  92. lock (TopLevels)
  93. {
  94. if (Top is { } && toplevel != Top && !TopLevels.Contains (Top))
  95. {
  96. // If Top was already disposed and isn't on the Toplevels Stack,
  97. // clean it up here if is the same as _cachedRunStateToplevel
  98. if (Top == _cachedRunStateToplevel)
  99. {
  100. Top = null;
  101. }
  102. else
  103. {
  104. // Probably this will never hit
  105. throw new ObjectDisposedException (Top.GetType ().FullName);
  106. }
  107. }
  108. // BUGBUG: We should not depend on `Id` internally.
  109. // BUGBUG: It is super unclear what this code does anyway.
  110. if (string.IsNullOrEmpty (toplevel.Id))
  111. {
  112. var count = 1;
  113. var id = (TopLevels.Count + count).ToString ();
  114. while (TopLevels.Count > 0 && TopLevels.FirstOrDefault (x => x.Id == id) is { })
  115. {
  116. count++;
  117. id = (TopLevels.Count + count).ToString ();
  118. }
  119. toplevel.Id = (TopLevels.Count + count).ToString ();
  120. TopLevels.Push (toplevel);
  121. }
  122. else
  123. {
  124. Toplevel? dup = TopLevels.FirstOrDefault (x => x.Id == toplevel.Id);
  125. if (dup is null)
  126. {
  127. TopLevels.Push (toplevel);
  128. }
  129. }
  130. if (TopLevels.FindDuplicates (new ToplevelEqualityComparer ()).Count > 0)
  131. {
  132. throw new ArgumentException ("There are duplicates Toplevel IDs");
  133. }
  134. }
  135. if (Top is null)
  136. {
  137. Top = toplevel;
  138. }
  139. if ((Top?.Modal == false && toplevel.Modal)
  140. || (Top?.Modal == false && !toplevel.Modal)
  141. || (Top?.Modal == true && toplevel.Modal))
  142. {
  143. if (toplevel.Visible)
  144. {
  145. if (Top is { HasFocus: true })
  146. {
  147. Top.HasFocus = false;
  148. }
  149. // Force leave events for any entered views in the old Top
  150. if (GetLastMousePosition () is { })
  151. {
  152. RaiseMouseEnterLeaveEvents (GetLastMousePosition ()!.Value, new List<View?> ());
  153. }
  154. Top?.OnDeactivate (toplevel);
  155. Toplevel previousTop = Top!;
  156. Top = toplevel;
  157. Top.OnActivate (previousTop);
  158. }
  159. }
  160. // View implements ISupportInitializeNotification which is derived from ISupportInitialize
  161. if (!toplevel.IsInitialized)
  162. {
  163. toplevel.BeginInit ();
  164. toplevel.EndInit (); // Calls Layout
  165. }
  166. // Try to set initial focus to any TabStop
  167. if (!toplevel.HasFocus)
  168. {
  169. toplevel.SetFocus ();
  170. }
  171. toplevel.OnLoaded ();
  172. if (PositionCursor ())
  173. {
  174. Driver?.UpdateCursor ();
  175. }
  176. NotifyNewRunState?.Invoke (toplevel, new (rs));
  177. // Force an Idle event so that an Iteration (and Refresh) happen.
  178. Application.Invoke (() => { });
  179. return rs;
  180. }
  181. /// <summary>
  182. /// Calls <see cref="View.PositionCursor"/> on the most focused view.
  183. /// </summary>
  184. /// <remarks>
  185. /// Does nothing if there is no most focused view.
  186. /// <para>
  187. /// If the most focused view is not visible within it's superview, the cursor will be hidden.
  188. /// </para>
  189. /// </remarks>
  190. /// <returns><see langword="true"/> if a view positioned the cursor and the position is visible.</returns>
  191. internal static bool PositionCursor ()
  192. {
  193. // Find the most focused view and position the cursor there.
  194. View? mostFocused = Navigation?.GetFocused ();
  195. // If the view is not visible or enabled, don't position the cursor
  196. if (mostFocused is null || !mostFocused.Visible || !mostFocused.Enabled)
  197. {
  198. CursorVisibility current = CursorVisibility.Invisible;
  199. Driver?.GetCursorVisibility (out current);
  200. if (current != CursorVisibility.Invisible)
  201. {
  202. Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  203. }
  204. return false;
  205. }
  206. // If the view is not visible within it's superview, don't position the cursor
  207. Rectangle mostFocusedViewport = mostFocused.ViewportToScreen (mostFocused.Viewport with { Location = Point.Empty });
  208. Rectangle superViewViewport = mostFocused.SuperView?.ViewportToScreen (mostFocused.SuperView.Viewport with { Location = Point.Empty }) ?? Driver!.Screen;
  209. if (!superViewViewport.IntersectsWith (mostFocusedViewport))
  210. {
  211. return false;
  212. }
  213. Point? cursor = mostFocused.PositionCursor ();
  214. Driver!.GetCursorVisibility (out CursorVisibility currentCursorVisibility);
  215. if (cursor is { })
  216. {
  217. // Convert cursor to screen coords
  218. cursor = mostFocused.ViewportToScreen (mostFocused.Viewport with { Location = cursor.Value }).Location;
  219. // If the cursor is not in a visible location in the SuperView, hide it
  220. if (!superViewViewport.Contains (cursor.Value))
  221. {
  222. if (currentCursorVisibility != CursorVisibility.Invisible)
  223. {
  224. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  225. }
  226. return false;
  227. }
  228. // Show it
  229. if (currentCursorVisibility == CursorVisibility.Invisible)
  230. {
  231. Driver.SetCursorVisibility (mostFocused.CursorVisibility);
  232. }
  233. return true;
  234. }
  235. if (currentCursorVisibility != CursorVisibility.Invisible)
  236. {
  237. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  238. }
  239. return false;
  240. }
  241. /// <summary>
  242. /// Runs the application by creating a <see cref="Toplevel"/> object and calling
  243. /// <see cref="Run(Toplevel, Func{Exception, bool})"/>.
  244. /// </summary>
  245. /// <remarks>
  246. /// <para>Calling <see cref="Init"/> first is not needed as this function will initialize the application.</para>
  247. /// <para>
  248. /// <see cref="Shutdown"/> must be called when the application is closing (typically after Run> has returned) to
  249. /// ensure resources are cleaned up and terminal settings restored.
  250. /// </para>
  251. /// <para>
  252. /// The caller is responsible for disposing the object returned by this method.
  253. /// </para>
  254. /// </remarks>
  255. /// <returns>The created <see cref="Toplevel"/> object. The caller is responsible for disposing this object.</returns>
  256. [RequiresUnreferencedCode ("AOT")]
  257. [RequiresDynamicCode ("AOT")]
  258. public static Toplevel Run (Func<Exception, bool>? errorHandler = null, IConsoleDriver? driver = null) =>
  259. ApplicationImpl.Instance.Run (errorHandler, driver);
  260. /// <summary>
  261. /// Runs the application by creating a <see cref="Toplevel"/>-derived object of type <c>T</c> and calling
  262. /// <see cref="Run(Toplevel, Func{Exception, bool})"/>.
  263. /// </summary>
  264. /// <remarks>
  265. /// <para>Calling <see cref="Init"/> first is not needed as this function will initialize the application.</para>
  266. /// <para>
  267. /// <see cref="Shutdown"/> must be called when the application is closing (typically after Run> has returned) to
  268. /// ensure resources are cleaned up and terminal settings restored.
  269. /// </para>
  270. /// <para>
  271. /// The caller is responsible for disposing the object returned by this method.
  272. /// </para>
  273. /// </remarks>
  274. /// <param name="errorHandler"></param>
  275. /// <param name="driver">
  276. /// The <see cref="IConsoleDriver"/> to use. If not specified the default driver for the platform will
  277. /// be used ( <see cref="WindowsDriver"/>, <see cref="CursesDriver"/>, or <see cref="NetDriver"/>). Must be
  278. /// <see langword="null"/> if <see cref="Init"/> has already been called.
  279. /// </param>
  280. /// <returns>The created T object. The caller is responsible for disposing this object.</returns>
  281. [RequiresUnreferencedCode ("AOT")]
  282. [RequiresDynamicCode ("AOT")]
  283. public static T Run<T> (Func<Exception, bool>? errorHandler = null, IConsoleDriver? driver = null)
  284. where T : Toplevel, new() => ApplicationImpl.Instance.Run<T> (errorHandler, driver);
  285. /// <summary>Runs the Application using the provided <see cref="Toplevel"/> view.</summary>
  286. /// <remarks>
  287. /// <para>
  288. /// This method is used to start processing events for the main application, but it is also used to run other
  289. /// modal <see cref="View"/>s such as <see cref="Dialog"/> boxes.
  290. /// </para>
  291. /// <para>
  292. /// To make a <see cref="Run(Terminal.Gui.Toplevel,System.Func{System.Exception,bool})"/> stop execution, call
  293. /// <see cref="Application.RequestStop"/>.
  294. /// </para>
  295. /// <para>
  296. /// Calling <see cref="Run(Terminal.Gui.Toplevel,System.Func{System.Exception,bool})"/> is equivalent to calling
  297. /// <see cref="Begin(Toplevel)"/>, followed by <see cref="RunLoop(RunState)"/>, and then calling
  298. /// <see cref="End(RunState)"/>.
  299. /// </para>
  300. /// <para>
  301. /// Alternatively, to have a program control the main loop and process events manually, call
  302. /// <see cref="Begin(Toplevel)"/> to set things up manually and then repeatedly call
  303. /// <see cref="RunLoop(RunState)"/> with the wait parameter set to false. By doing this the
  304. /// <see cref="RunLoop(RunState)"/> method will only process any pending events, timers, idle handlers and then
  305. /// return control immediately.
  306. /// </para>
  307. /// <para>When using <see cref="Run{T}"/> or
  308. /// <see cref="Run(System.Func{System.Exception,bool},Terminal.Gui.IConsoleDriver)"/>
  309. /// <see cref="Init"/> will be called automatically.
  310. /// </para>
  311. /// <para>
  312. /// RELEASE builds only: When <paramref name="errorHandler"/> is <see langword="null"/> any exceptions will be
  313. /// rethrown. Otherwise, if <paramref name="errorHandler"/> will be called. If <paramref name="errorHandler"/>
  314. /// returns <see langword="true"/> the <see cref="RunLoop(RunState)"/> will resume; otherwise this method will
  315. /// exit.
  316. /// </para>
  317. /// </remarks>
  318. /// <param name="view">The <see cref="Toplevel"/> to run as a modal.</param>
  319. /// <param name="errorHandler">
  320. /// RELEASE builds only: Handler for any unhandled exceptions (resumes when returns true,
  321. /// rethrows when null).
  322. /// </param>
  323. public static void Run (Toplevel view, Func<Exception, bool>? errorHandler = null)
  324. => ApplicationImpl.Instance.Run (view, errorHandler);
  325. /// <summary>Adds a timeout to the application.</summary>
  326. /// <remarks>
  327. /// When time specified passes, the callback will be invoked. If the callback returns true, the timeout will be
  328. /// reset, repeating the invocation. If it returns false, the timeout will stop and be removed. The returned value is a
  329. /// token that can be used to stop the timeout by calling <see cref="RemoveTimeout(object)"/>.
  330. /// </remarks>
  331. public static object? AddTimeout (TimeSpan time, Func<bool> callback) => ApplicationImpl.Instance.AddTimeout (time, callback);
  332. /// <summary>Removes a previously scheduled timeout</summary>
  333. /// <remarks>The token parameter is the value returned by <see cref="AddTimeout"/>.</remarks>
  334. /// Returns
  335. /// <see langword="true"/>
  336. /// if the timeout is successfully removed; otherwise,
  337. /// <see langword="false"/>
  338. /// .
  339. /// This method also returns
  340. /// <see langword="false"/>
  341. /// if the timeout is not found.
  342. public static bool RemoveTimeout (object token) => ApplicationImpl.Instance.RemoveTimeout (token);
  343. /// <summary>Runs <paramref name="action"/> on the thread that is processing events</summary>
  344. /// <param name="action">the action to be invoked on the main processing thread.</param>
  345. public static void Invoke (Action action) => ApplicationImpl.Instance.Invoke (action);
  346. // TODO: Determine if this is really needed. The only code that calls WakeUp I can find
  347. // is ProgressBarStyles, and it's not clear it needs to.
  348. /// <summary>Wakes up the running application that might be waiting on input.</summary>
  349. public static void Wakeup () { MainLoop?.Wakeup (); }
  350. /// <summary>
  351. /// Causes any Toplevels that need layout to be laid out. Then draws any Toplevels that need display. Only Views that need to be laid out (see <see cref="View.NeedsLayout"/>) will be laid out.
  352. /// Only Views that need to be drawn (see <see cref="View.NeedsDraw"/>) will be drawn.
  353. /// </summary>
  354. /// <param name="forceDraw">If <see langword="true"/> the entire View hierarchy will be redrawn. The default is <see langword="false"/> and should only be overriden for testing.</param>
  355. public static void LayoutAndDraw (bool forceDraw = false)
  356. {
  357. bool neededLayout = View.Layout (TopLevels.Reverse (), Screen.Size);
  358. if (ClearScreenNextIteration)
  359. {
  360. forceDraw = true;
  361. ClearScreenNextIteration = false;
  362. }
  363. if (forceDraw)
  364. {
  365. Driver?.ClearContents ();
  366. }
  367. View.SetClipToScreen ();
  368. View.Draw (TopLevels, neededLayout || forceDraw);
  369. View.SetClipToScreen ();
  370. Driver?.Refresh ();
  371. }
  372. /// <summary>This event is raised on each iteration of the main loop.</summary>
  373. /// <remarks>See also <see cref="Timeout"/></remarks>
  374. public static event EventHandler<IterationEventArgs>? Iteration;
  375. /// <summary>The <see cref="MainLoop"/> driver for the application</summary>
  376. /// <value>The main loop.</value>
  377. internal static MainLoop? MainLoop { get; set; }
  378. /// <summary>
  379. /// Set to true to cause <see cref="End"/> to be called after the first iteration. Set to false (the default) to
  380. /// cause the application to continue running until Application.RequestStop () is called.
  381. /// </summary>
  382. public static bool EndAfterFirstIteration { get; set; }
  383. /// <summary>Building block API: Runs the main loop for the created <see cref="Toplevel"/>.</summary>
  384. /// <param name="state">The state returned by the <see cref="Begin(Toplevel)"/> method.</param>
  385. public static void RunLoop (RunState state)
  386. {
  387. ArgumentNullException.ThrowIfNull (state);
  388. ObjectDisposedException.ThrowIf (state.Toplevel is null, "state");
  389. var firstIteration = true;
  390. for (state.Toplevel.Running = true; state.Toplevel?.Running == true;)
  391. {
  392. MainLoop!.Running = true;
  393. if (EndAfterFirstIteration && !firstIteration)
  394. {
  395. return;
  396. }
  397. firstIteration = RunIteration (ref state, firstIteration);
  398. }
  399. MainLoop!.Running = false;
  400. // Run one last iteration to consume any outstanding input events from Driver
  401. // This is important for remaining OnKeyUp events.
  402. RunIteration (ref state, firstIteration);
  403. }
  404. /// <summary>Run one application iteration.</summary>
  405. /// <param name="state">The state returned by <see cref="Begin(Toplevel)"/>.</param>
  406. /// <param name="firstIteration">
  407. /// Set to <see langword="true"/> if this is the first run loop iteration.
  408. /// </param>
  409. /// <returns><see langword="false"/> if at least one iteration happened.</returns>
  410. public static bool RunIteration (ref RunState state, bool firstIteration = false)
  411. {
  412. // If the driver has events pending do an iteration of the driver MainLoop
  413. if (MainLoop!.Running && MainLoop.EventsPending ())
  414. {
  415. // Notify Toplevel it's ready
  416. if (firstIteration)
  417. {
  418. state.Toplevel.OnReady ();
  419. }
  420. MainLoop.RunIteration ();
  421. Iteration?.Invoke (null, new ());
  422. }
  423. firstIteration = false;
  424. if (Top is null)
  425. {
  426. return firstIteration;
  427. }
  428. LayoutAndDraw ();
  429. if (PositionCursor ())
  430. {
  431. Driver!.UpdateCursor ();
  432. }
  433. return firstIteration;
  434. }
  435. /// <summary>Stops the provided <see cref="Toplevel"/>, causing or the <paramref name="top"/> if provided.</summary>
  436. /// <param name="top">The <see cref="Toplevel"/> to stop.</param>
  437. /// <remarks>
  438. /// <para>This will cause <see cref="Application.Run(Toplevel, Func{Exception, bool})"/> to return.</para>
  439. /// <para>
  440. /// Calling <see cref="RequestStop(Terminal.Gui.Toplevel)"/> is equivalent to setting the <see cref="Toplevel.Running"/>
  441. /// property on the currently running <see cref="Toplevel"/> to false.
  442. /// </para>
  443. /// </remarks>
  444. public static void RequestStop (Toplevel? top = null) => ApplicationImpl.Instance.RequestStop (top);
  445. internal static void OnNotifyStopRunState (Toplevel top)
  446. {
  447. if (EndAfterFirstIteration)
  448. {
  449. NotifyStopRunState?.Invoke (top, new (top));
  450. }
  451. }
  452. /// <summary>
  453. /// Building block API: completes the execution of a <see cref="Toplevel"/> that was started with
  454. /// <see cref="Begin(Toplevel)"/> .
  455. /// </summary>
  456. /// <param name="runState">The <see cref="RunState"/> returned by the <see cref="Begin(Toplevel)"/> method.</param>
  457. public static void End (RunState runState)
  458. {
  459. ArgumentNullException.ThrowIfNull (runState);
  460. runState.Toplevel.OnUnloaded ();
  461. // End the RunState.Toplevel
  462. // First, take it off the Toplevel Stack
  463. if (TopLevels.Count > 0)
  464. {
  465. if (TopLevels.Peek () != runState.Toplevel)
  466. {
  467. // If the top of the stack is not the RunState.Toplevel then
  468. // this call to End is not balanced with the call to Begin that started the RunState
  469. throw new ArgumentException ("End must be balanced with calls to Begin");
  470. }
  471. TopLevels.Pop ();
  472. }
  473. // Notify that it is closing
  474. runState.Toplevel?.OnClosed (runState.Toplevel);
  475. if (TopLevels.Count > 0)
  476. {
  477. Top = TopLevels.Peek ();
  478. Top.SetNeedsDraw ();
  479. }
  480. if (runState.Toplevel is { HasFocus: true })
  481. {
  482. runState.Toplevel.HasFocus = false;
  483. }
  484. if (Top is { HasFocus: false })
  485. {
  486. Top.SetFocus ();
  487. }
  488. _cachedRunStateToplevel = runState.Toplevel;
  489. runState.Toplevel = null;
  490. runState.Dispose ();
  491. LayoutAndDraw (true);
  492. }
  493. }