Application.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. //
  2. // Core.cs: The core engine for gui.cs
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // Pending:
  8. // - Check for NeedDisplay on the hierarchy and repaint
  9. // - Layout support
  10. // - "Colors" type or "Attributes" type?
  11. // - What to surface as "BackgroundCOlor" when clearing a window, an attribute or colors?
  12. //
  13. // Optimziations
  14. // - Add rendering limitation to the exposed area
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Threading;
  19. using System.Linq;
  20. using NStack;
  21. using System.ComponentModel;
  22. namespace Terminal.Gui {
  23. /// <summary>
  24. /// A static, singelton class provding the main application driver for Terminal.Gui apps.
  25. /// </summary>
  26. /// <example>
  27. /// <code>
  28. /// // A simple Terminal.Gui app that creates a window with a frame and title with
  29. /// // 5 rows/columns of padding.
  30. /// Application.Init();
  31. /// var win = new Window ("Hello World - CTRL-Q to quit") {
  32. /// X = 5,
  33. /// Y = 5,
  34. /// Width = Dim.Fill (5),
  35. /// Height = Dim.Fill (5)
  36. /// };
  37. /// Application.Top.Add(win);
  38. /// Application.Run();
  39. /// </code>
  40. /// </example>
  41. /// <remarks>
  42. /// <para>
  43. /// Creates a instance of <see cref="Terminal.Gui.MainLoop"/> to process input events, handle timers and
  44. /// other sources of data. It is accessible via the <see cref="MainLoop"/> property.
  45. /// </para>
  46. /// <para>
  47. /// You can hook up to the <see cref="Iteration"/> event to have your method
  48. /// invoked on each iteration of the <see cref="Terminal.Gui.MainLoop"/>.
  49. /// </para>
  50. /// <para>
  51. /// When invoked sets the SynchronizationContext to one that is tied
  52. /// to the mainloop, allowing user code to use async/await.
  53. /// </para>
  54. /// </remarks>
  55. public static class Application {
  56. /// <summary>
  57. /// The current <see cref="ConsoleDriver"/> in use.
  58. /// </summary>
  59. public static ConsoleDriver Driver;
  60. /// <summary>
  61. /// The <see cref="Toplevel"/> object used for the application on startup (<seealso cref="Application.Top"/>)
  62. /// </summary>
  63. /// <value>The top.</value>
  64. public static Toplevel Top { get; private set; }
  65. /// <summary>
  66. /// The current <see cref="Toplevel"/> object. This is updated when <see cref="Application.Run()"/> enters and leaves to point to the current <see cref="Toplevel"/> .
  67. /// </summary>
  68. /// <value>The current.</value>
  69. public static Toplevel Current { get; private set; }
  70. /// <summary>
  71. /// TThe current <see cref="View"/> object being redrawn.
  72. /// </summary>
  73. /// /// <value>The current.</value>
  74. public static View CurrentView { get; set; }
  75. /// <summary>
  76. /// The <see cref="MainLoop"/> driver for the applicaiton
  77. /// </summary>
  78. /// <value>The main loop.</value>
  79. public static MainLoop MainLoop { get; private set; }
  80. static Stack<Toplevel> toplevels = new Stack<Toplevel> ();
  81. /// <summary>
  82. /// This event is raised on each iteration of the <see cref="MainLoop"/>
  83. /// </summary>
  84. /// <remarks>
  85. /// See also <see cref="Timeout"/>
  86. /// </remarks>
  87. public static Action Iteration;
  88. /// <summary>
  89. /// Returns a rectangle that is centered in the screen for the provided size.
  90. /// </summary>
  91. /// <returns>The centered rect.</returns>
  92. /// <param name="size">Size for the rectangle.</param>
  93. public static Rect MakeCenteredRect (Size size)
  94. {
  95. return new Rect (new Point ((Driver.Cols - size.Width) / 2, (Driver.Rows - size.Height) / 2), size);
  96. }
  97. //
  98. // provides the sync context set while executing code in Terminal.Gui, to let
  99. // users use async/await on their code
  100. //
  101. class MainLoopSyncContext : SynchronizationContext {
  102. MainLoop mainLoop;
  103. public MainLoopSyncContext (MainLoop mainLoop)
  104. {
  105. this.mainLoop = mainLoop;
  106. }
  107. public override SynchronizationContext CreateCopy ()
  108. {
  109. return new MainLoopSyncContext (MainLoop);
  110. }
  111. public override void Post (SendOrPostCallback d, object state)
  112. {
  113. mainLoop.AddIdle (() => {
  114. d (state);
  115. return false;
  116. });
  117. //mainLoop.Driver.Wakeup ();
  118. }
  119. public override void Send (SendOrPostCallback d, object state)
  120. {
  121. mainLoop.Invoke (() => {
  122. d (state);
  123. });
  124. }
  125. }
  126. /// <summary>
  127. /// If set, it forces the use of the System.Console-based driver.
  128. /// </summary>
  129. public static bool UseSystemConsole;
  130. /// <summary>
  131. /// Initializes a new instance of <see cref="Terminal.Gui"/> Application.
  132. /// </summary>
  133. /// <remarks>
  134. /// <para>
  135. /// Call this method once per instance (or after <see cref="Shutdown"/> has been called).
  136. /// </para>
  137. /// <para>
  138. /// Loads the right <see cref="ConsoleDriver"/> for the platform.
  139. /// </para>
  140. /// <para>
  141. /// Creates a <see cref="Toplevel"/> and assigns it to <see cref="Top"/> and <see cref="CurrentView"/>
  142. /// </para>
  143. /// </remarks>
  144. public static void Init () => Init (() => Toplevel.Create ());
  145. internal static bool _initialized = false;
  146. /// <summary>
  147. /// Initializes the Terminal.Gui application
  148. /// </summary>
  149. static void Init (Func<Toplevel> topLevelFactory)
  150. {
  151. if (_initialized) return;
  152. if (Driver == null) {
  153. var p = Environment.OSVersion.Platform;
  154. IMainLoopDriver mainLoopDriver;
  155. if (UseSystemConsole) {
  156. mainLoopDriver = new NetMainLoop ();
  157. Driver = new NetDriver ();
  158. } else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows) {
  159. var windowsDriver = new WindowsDriver ();
  160. mainLoopDriver = windowsDriver;
  161. Driver = windowsDriver;
  162. } else {
  163. mainLoopDriver = new UnixMainLoop ();
  164. Driver = new CursesDriver ();
  165. }
  166. Driver.Init (TerminalResized);
  167. MainLoop = new MainLoop (mainLoopDriver);
  168. SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext (MainLoop));
  169. }
  170. Top = topLevelFactory ();
  171. Current = Top;
  172. CurrentView = Top;
  173. _initialized = true;
  174. }
  175. /// <summary>
  176. /// Captures the execution state for the provided <see cref="Toplevel"/> view.
  177. /// </summary>
  178. public class RunState : IDisposable {
  179. internal bool closeDriver = true;
  180. internal RunState (Toplevel view)
  181. {
  182. Toplevel = view;
  183. }
  184. internal Toplevel Toplevel;
  185. /// <summary>
  186. /// Releases alTop = l resource used by the <see cref="Application.RunState"/> object.
  187. /// </summary>
  188. /// <remarks>Call <see cref="Dispose()"/> when you are finished using the <see cref="Application.RunState"/>. The
  189. /// <see cref="Dispose()"/> method leaves the <see cref="Application.RunState"/> in an unusable state. After
  190. /// calling <see cref="Dispose()"/>, you must release all references to the
  191. /// <see cref="Application.RunState"/> so the garbage collector can reclaim the memory that the
  192. /// <see cref="Application.RunState"/> was occupying.</remarks>
  193. public void Dispose ()
  194. {
  195. Dispose (closeDriver);
  196. GC.SuppressFinalize (this);
  197. }
  198. /// <summary>
  199. /// Dispose the specified disposing.
  200. /// </summary>
  201. /// <returns>The dispose.</returns>
  202. /// <param name="disposing">If set to <c>true</c> disposing.</param>
  203. protected virtual void Dispose (bool disposing)
  204. {
  205. if (Toplevel != null) {
  206. End (Toplevel, disposing);
  207. Toplevel = null;
  208. }
  209. }
  210. }
  211. static void ProcessKeyEvent (KeyEvent ke)
  212. {
  213. var chain = toplevels.ToList ();
  214. foreach (var topLevel in chain) {
  215. if (topLevel.ProcessHotKey (ke))
  216. return;
  217. if (topLevel.Modal)
  218. break;
  219. }
  220. foreach (var topLevel in chain) {
  221. if (topLevel.ProcessKey (ke))
  222. return;
  223. if (topLevel.Modal)
  224. break;
  225. }
  226. foreach (var topLevel in chain) {
  227. // Process the key normally
  228. if (topLevel.ProcessColdKey (ke))
  229. return;
  230. if (topLevel.Modal)
  231. break;
  232. }
  233. }
  234. static void ProcessKeyDownEvent (KeyEvent ke)
  235. {
  236. var chain = toplevels.ToList ();
  237. foreach (var topLevel in chain) {
  238. if (topLevel.OnKeyDown (ke))
  239. return;
  240. if (topLevel.Modal)
  241. break;
  242. }
  243. }
  244. static void ProcessKeyUpEvent (KeyEvent ke)
  245. {
  246. var chain = toplevels.ToList ();
  247. foreach (var topLevel in chain) {
  248. if (topLevel.OnKeyUp (ke))
  249. return;
  250. if (topLevel.Modal)
  251. break;
  252. }
  253. }
  254. static View FindDeepestView (View start, int x, int y, out int resx, out int resy)
  255. {
  256. var startFrame = start.Frame;
  257. if (!startFrame.Contains (x, y)) {
  258. resx = 0;
  259. resy = 0;
  260. return null;
  261. }
  262. if (start.InternalSubviews != null) {
  263. int count = start.InternalSubviews.Count;
  264. if (count > 0) {
  265. var rx = x - startFrame.X;
  266. var ry = y - startFrame.Y;
  267. for (int i = count - 1; i >= 0; i--) {
  268. View v = start.InternalSubviews [i];
  269. if (v.Frame.Contains (rx, ry)) {
  270. var deep = FindDeepestView (v, rx, ry, out resx, out resy);
  271. if (deep == null)
  272. return v;
  273. return deep;
  274. }
  275. }
  276. }
  277. }
  278. resx = x - startFrame.X;
  279. resy = y - startFrame.Y;
  280. return start;
  281. }
  282. internal static View mouseGrabView;
  283. /// <summary>
  284. /// Grabs the mouse, forcing all mouse events to be routed to the specified view until UngrabMouse is called.
  285. /// </summary>
  286. /// <returns>The grab.</returns>
  287. /// <param name="view">View that will receive all mouse events until UngrabMouse is invoked.</param>
  288. public static void GrabMouse (View view)
  289. {
  290. if (view == null)
  291. return;
  292. mouseGrabView = view;
  293. Driver.UncookMouse ();
  294. }
  295. /// <summary>
  296. /// Releases the mouse grab, so mouse events will be routed to the view on which the mouse is.
  297. /// </summary>
  298. public static void UngrabMouse ()
  299. {
  300. mouseGrabView = null;
  301. Driver.CookMouse ();
  302. }
  303. /// <summary>
  304. /// Merely a debugging aid to see the raw mouse events
  305. /// </summary>
  306. public static Action<MouseEvent> RootMouseEvent;
  307. internal static View wantContinuousButtonPressedView;
  308. static View lastMouseOwnerView;
  309. static void ProcessMouseEvent (MouseEvent me)
  310. {
  311. var view = FindDeepestView (Current, me.X, me.Y, out int rx, out int ry);
  312. if (view != null && view.WantContinuousButtonPressed)
  313. wantContinuousButtonPressedView = view;
  314. else
  315. wantContinuousButtonPressedView = null;
  316. RootMouseEvent?.Invoke (me);
  317. if (mouseGrabView != null) {
  318. var newxy = mouseGrabView.ScreenToView (me.X, me.Y);
  319. var nme = new MouseEvent () {
  320. X = newxy.X,
  321. Y = newxy.Y,
  322. Flags = me.Flags,
  323. OfX = me.X - newxy.X,
  324. OfY = me.Y - newxy.Y,
  325. View = view
  326. };
  327. if (OutsideFrame (new Point (nme.X, nme.Y), mouseGrabView.Frame)) {
  328. lastMouseOwnerView?.OnMouseLeave (me);
  329. }
  330. if (mouseGrabView != null) {
  331. mouseGrabView.OnMouseEvent (nme);
  332. return;
  333. }
  334. }
  335. if (view != null) {
  336. var nme = new MouseEvent () {
  337. X = rx,
  338. Y = ry,
  339. Flags = me.Flags,
  340. OfX = rx,
  341. OfY = ry,
  342. View = view
  343. };
  344. if (lastMouseOwnerView == null) {
  345. lastMouseOwnerView = view;
  346. view.OnMouseEnter (nme);
  347. } else if (lastMouseOwnerView != view) {
  348. lastMouseOwnerView.OnMouseLeave (nme);
  349. view.OnMouseEnter (nme);
  350. lastMouseOwnerView = view;
  351. }
  352. if (!view.WantMousePositionReports && me.Flags == MouseFlags.ReportMousePosition)
  353. return;
  354. if (view.WantContinuousButtonPressed)
  355. wantContinuousButtonPressedView = view;
  356. else
  357. wantContinuousButtonPressedView = null;
  358. // Should we bubbled up the event, if it is not handled?
  359. view.OnMouseEvent (nme);
  360. }
  361. }
  362. static bool OutsideFrame (Point p, Rect r)
  363. {
  364. return p.X < 0 || p.X > r.Width - 1 || p.Y < 0 || p.Y > r.Height - 1;
  365. }
  366. /// <summary>
  367. /// This event is fired once when the application is first loaded. The dimensions of the
  368. /// terminal are provided.
  369. /// </summary>
  370. public static Action<ResizedEventArgs> Loaded;
  371. /// <summary>
  372. /// Building block API: Prepares the provided <see cref="Toplevel"/> for execution.
  373. /// </summary>
  374. /// <returns>The runstate handle that needs to be passed to the <see cref="End(RunState, bool)"/> method upon completion.</returns>
  375. /// <param name="toplevel">Toplevel to prepare execution for.</param>
  376. /// <remarks>
  377. /// This method prepares the provided toplevel for running with the focus,
  378. /// it adds this to the list of toplevels, sets up the mainloop to process the
  379. /// event, lays out the subviews, focuses the first element, and draws the
  380. /// toplevel in the screen. This is usually followed by executing
  381. /// the <see cref="RunLoop"/> method, and then the <see cref="End(RunState, bool)"/> method upon termination which will
  382. /// undo these changes.
  383. /// </remarks>
  384. public static RunState Begin (Toplevel toplevel)
  385. {
  386. if (toplevel == null)
  387. throw new ArgumentNullException (nameof (toplevel));
  388. var rs = new RunState (toplevel);
  389. Init ();
  390. if (toplevel is ISupportInitializeNotification initializableNotification &&
  391. !initializableNotification.IsInitialized) {
  392. initializableNotification.BeginInit ();
  393. initializableNotification.EndInit ();
  394. } else if (toplevel is ISupportInitialize initializable) {
  395. initializable.BeginInit ();
  396. initializable.EndInit ();
  397. }
  398. toplevels.Push (toplevel);
  399. Current = toplevel;
  400. Driver.PrepareToRun (MainLoop, ProcessKeyEvent, ProcessKeyDownEvent, ProcessKeyUpEvent, ProcessMouseEvent);
  401. if (toplevel.LayoutStyle == LayoutStyle.Computed)
  402. toplevel.SetRelativeLayout (new Rect (0, 0, Driver.Cols, Driver.Rows));
  403. toplevel.LayoutSubviews ();
  404. Loaded?.Invoke (new ResizedEventArgs () { Rows = Driver.Rows, Cols = Driver.Cols });
  405. toplevel.WillPresent ();
  406. Redraw (toplevel);
  407. toplevel.PositionCursor ();
  408. Driver.Refresh ();
  409. return rs;
  410. }
  411. /// <summary>
  412. /// Building block API: completes the execution of a <see cref="Toplevel"/> that was started with <see cref="Begin(Toplevel)"/> .
  413. /// </summary>
  414. /// <param name="runState">The runstate returned by the <see cref="Begin(Toplevel)"/> method.</param>
  415. /// <param name="closeDriver"><c>true</c>Closes the application.<c>false</c>Closes the toplevels only.</param>
  416. public static void End (RunState runState, bool closeDriver = true)
  417. {
  418. if (runState == null)
  419. throw new ArgumentNullException (nameof (runState));
  420. runState.closeDriver = closeDriver;
  421. runState.Dispose ();
  422. }
  423. /// <summary>
  424. /// Shutdown an application initialized with <see cref="Init()"/>
  425. /// </summary>
  426. /// /// <param name="closeDriver"><c>true</c>Closes the application.<c>false</c>Closes toplevels only.</param>
  427. public static void Shutdown (bool closeDriver = true)
  428. {
  429. // Shutdown is the bookend for Init. As such it needs to clean up all resources
  430. // Init created. Apps that do any threading will need to code defensively for this.
  431. // e.g. see Issue #537
  432. // TODO: Some of this state is actually related to Begin/End (not Init/Shutdown) and should be moved to `RunState` (#520)
  433. foreach (var t in toplevels) {
  434. t.Running = false;
  435. }
  436. toplevels.Clear ();
  437. Current = null;
  438. CurrentView = null;
  439. Top = null;
  440. // Closes the application if it's true.
  441. if (closeDriver) {
  442. MainLoop = null;
  443. Driver?.End ();
  444. Driver = null;
  445. }
  446. _initialized = false;
  447. }
  448. static void Redraw (View view)
  449. {
  450. Application.CurrentView = view;
  451. view.Redraw (view.Bounds);
  452. Driver.Refresh ();
  453. }
  454. static void Refresh (View view)
  455. {
  456. view.Redraw (view.Bounds);
  457. Driver.Refresh ();
  458. }
  459. /// <summary>
  460. /// Triggers a refresh of the entire display.
  461. /// </summary>
  462. public static void Refresh ()
  463. {
  464. Driver.UpdateScreen ();
  465. View last = null;
  466. foreach (var v in toplevels.Reverse ()) {
  467. v.SetNeedsDisplay ();
  468. v.Redraw (v.Bounds);
  469. last = v;
  470. }
  471. last?.PositionCursor ();
  472. Driver.Refresh ();
  473. }
  474. internal static void End (View view, bool closeDriver = true)
  475. {
  476. if (toplevels.Peek () != view)
  477. throw new ArgumentException ("The view that you end with must be balanced");
  478. toplevels.Pop ();
  479. if (toplevels.Count == 0)
  480. Shutdown (closeDriver);
  481. else {
  482. Current = toplevels.Peek ();
  483. Refresh ();
  484. }
  485. }
  486. /// <summary>
  487. /// Building block API: Runs the main loop for the created dialog
  488. /// </summary>
  489. /// <remarks>
  490. /// Use the wait parameter to control whether this is a
  491. /// blocking or non-blocking call.
  492. /// </remarks>
  493. /// <param name="state">The state returned by the Begin method.</param>
  494. /// <param name="wait">By default this is true which will execute the runloop waiting for events, if you pass false, you can use this method to run a single iteration of the events.</param>
  495. public static void RunLoop (RunState state, bool wait = true)
  496. {
  497. if (state == null)
  498. throw new ArgumentNullException (nameof (state));
  499. if (state.Toplevel == null)
  500. throw new ObjectDisposedException ("state");
  501. bool firstIteration = true;
  502. for (state.Toplevel.Running = true; state.Toplevel.Running;) {
  503. if (MainLoop.EventsPending (wait)) {
  504. // Notify Toplevel it's ready
  505. if (firstIteration) {
  506. state.Toplevel.OnReady ();
  507. }
  508. firstIteration = false;
  509. MainLoop.MainIteration ();
  510. Iteration?.Invoke ();
  511. } else if (wait == false)
  512. return;
  513. if (state.Toplevel.NeedDisplay != null && (!state.Toplevel.NeedDisplay.IsEmpty || state.Toplevel.childNeedsDisplay)) {
  514. state.Toplevel.Redraw (state.Toplevel.Bounds);
  515. if (DebugDrawBounds)
  516. DrawBounds (state.Toplevel);
  517. state.Toplevel.PositionCursor ();
  518. Driver.Refresh ();
  519. } else
  520. Driver.UpdateCursor ();
  521. }
  522. }
  523. internal static bool DebugDrawBounds = false;
  524. // Need to look into why this does not work properly.
  525. static void DrawBounds (View v)
  526. {
  527. v.DrawFrame (v.Frame, padding: 0, fill: false);
  528. if (v.InternalSubviews != null && v.InternalSubviews.Count > 0)
  529. foreach (var sub in v.InternalSubviews)
  530. DrawBounds (sub);
  531. }
  532. /// <summary>
  533. /// Runs the application by calling <see cref="Run(Toplevel, bool)"/> with the value of <see cref="Top"/>
  534. /// </summary>
  535. public static void Run ()
  536. {
  537. Run (Top);
  538. }
  539. /// <summary>
  540. /// Runs the application by calling <see cref="Run(Toplevel, bool)"/> with a new instance of the specified <see cref="Toplevel"/>-derived class
  541. /// </summary>
  542. public static void Run<T> () where T : Toplevel, new()
  543. {
  544. Init (() => new T ());
  545. Run (Top);
  546. }
  547. /// <summary>
  548. /// Runs the main loop on the given <see cref="Toplevel"/> container.
  549. /// </summary>
  550. /// <remarks>
  551. /// <para>
  552. /// This method is used to start processing events
  553. /// for the main application, but it is also used to
  554. /// run other modal <see cref="View"/>s such as <see cref="Dialog"/> boxes.
  555. /// </para>
  556. /// <para>
  557. /// To make a <see cref="Run(Toplevel, bool)"/> stop execution, call <see cref="Application.RequestStop"/>.
  558. /// </para>
  559. /// <para>
  560. /// Calling <see cref="Run(Toplevel, bool)"/> is equivalent to calling <see cref="Begin(Toplevel)"/>, followed by <see cref="RunLoop(RunState, bool)"/>,
  561. /// and then calling <see cref="End(RunState, bool)"/>.
  562. /// </para>
  563. /// <para>
  564. /// Alternatively, to have a program control the main loop and
  565. /// process events manually, call <see cref="Begin(Toplevel)"/> to set things up manually and then
  566. /// repeatedly call <see cref="RunLoop(RunState, bool)"/> with the wait parameter set to false. By doing this
  567. /// the <see cref="RunLoop(RunState, bool)"/> method will only process any pending events, timers, idle handlers and
  568. /// then return control immediately.
  569. /// </para>
  570. /// </remarks>
  571. public static void Run (Toplevel view, bool closeDriver = true)
  572. {
  573. var runToken = Begin (view);
  574. RunLoop (runToken);
  575. End (runToken, closeDriver);
  576. }
  577. /// <summary>
  578. /// Stops running the most recent <see cref="Toplevel"/>.
  579. /// </summary>
  580. /// <remarks>
  581. /// <para>
  582. /// This will cause <see cref="Application.Run()"/> to return.
  583. /// </para>
  584. /// <para>
  585. /// Calling <see cref="Application.RequestStop"/> is equivalent to setting the <see cref="Toplevel.Running"/> property on the curently running <see cref="Toplevel"/> to false.
  586. /// </para>
  587. /// </remarks>
  588. public static void RequestStop ()
  589. {
  590. Current.Running = false;
  591. }
  592. /// <summary>
  593. /// Event arguments for the <see cref="Application.Resized"/> event.
  594. /// </summary>
  595. public class ResizedEventArgs : EventArgs {
  596. /// <summary>
  597. /// The number of rows in the resized terminal.
  598. /// </summary>
  599. public int Rows { get; set; }
  600. /// <summary>
  601. /// The number of columns in the resized terminal.
  602. /// </summary>
  603. public int Cols { get; set; }
  604. }
  605. /// <summary>
  606. /// Invoked when the terminal was resized. The new size of the terminal is provided.
  607. /// </summary>
  608. public static Action<ResizedEventArgs> Resized;
  609. static void TerminalResized ()
  610. {
  611. var full = new Rect (0, 0, Driver.Cols, Driver.Rows);
  612. Resized?.Invoke (new ResizedEventArgs () { Cols = full.Width, Rows = full.Height });
  613. Driver.Clip = full;
  614. foreach (var t in toplevels) {
  615. t.PositionToplevels ();
  616. t.SetRelativeLayout (full);
  617. t.LayoutSubviews ();
  618. }
  619. Refresh ();
  620. }
  621. }
  622. }