Application.cs 20 KB

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