Application.Run.cs 23 KB

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