Application.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. if (mouseGrabView != null) {
  315. mouseGrabView.OnMouseEvent (nme);
  316. return;
  317. }
  318. }
  319. if (view != null) {
  320. var nme = new MouseEvent () {
  321. X = rx,
  322. Y = ry,
  323. Flags = me.Flags,
  324. OfX = rx,
  325. OfY = ry,
  326. View = view
  327. };
  328. if (lastMouseOwnerView == null) {
  329. lastMouseOwnerView = view;
  330. view.OnMouseEnter (nme);
  331. } else if (lastMouseOwnerView != view) {
  332. lastMouseOwnerView.OnMouseLeave (nme);
  333. view.OnMouseEnter (nme);
  334. lastMouseOwnerView = view;
  335. }
  336. if (!view.WantMousePositionReports && me.Flags == MouseFlags.ReportMousePosition)
  337. return;
  338. if (view.WantContinuousButtonPressed)
  339. wantContinuousButtonPressedView = view;
  340. else
  341. wantContinuousButtonPressedView = null;
  342. // Should we bubbled up the event, if it is not handled?
  343. view.OnMouseEvent (nme);
  344. }
  345. }
  346. static bool OutsideFrame (Point p, Rect r)
  347. {
  348. return p.X < 0 || p.X > r.Width - 1 || p.Y < 0 || p.Y > r.Height - 1;
  349. }
  350. /// <summary>
  351. /// This event is fired once when the application is first loaded. The dimensions of the
  352. /// terminal are provided.
  353. /// </summary>
  354. public static event EventHandler<ResizedEventArgs> Loaded;
  355. /// <summary>
  356. /// Building block API: Prepares the provided <see cref="Toplevel"/> for execution.
  357. /// </summary>
  358. /// <returns>The runstate handle that needs to be passed to the <see cref="End(RunState, bool)"/> method upon completion.</returns>
  359. /// <param name="toplevel">Toplevel to prepare execution for.</param>
  360. /// <remarks>
  361. /// This method prepares the provided toplevel for running with the focus,
  362. /// it adds this to the list of toplevels, sets up the mainloop to process the
  363. /// event, lays out the subviews, focuses the first element, and draws the
  364. /// toplevel in the screen. This is usually followed by executing
  365. /// the <see cref="RunLoop"/> method, and then the <see cref="End(RunState, bool)"/> method upon termination which will
  366. /// undo these changes.
  367. /// </remarks>
  368. public static RunState Begin (Toplevel toplevel)
  369. {
  370. if (toplevel == null)
  371. throw new ArgumentNullException (nameof (toplevel));
  372. var rs = new RunState (toplevel);
  373. Init ();
  374. if (toplevel is ISupportInitializeNotification initializableNotification &&
  375. !initializableNotification.IsInitialized) {
  376. initializableNotification.BeginInit ();
  377. initializableNotification.EndInit ();
  378. } else if (toplevel is ISupportInitialize initializable) {
  379. initializable.BeginInit ();
  380. initializable.EndInit ();
  381. }
  382. toplevels.Push (toplevel);
  383. Current = toplevel;
  384. Driver.PrepareToRun (MainLoop, ProcessKeyEvent, ProcessKeyDownEvent, ProcessKeyUpEvent, ProcessMouseEvent);
  385. if (toplevel.LayoutStyle == LayoutStyle.Computed)
  386. toplevel.SetRelativeLayout (new Rect (0, 0, Driver.Cols, Driver.Rows));
  387. toplevel.LayoutSubviews ();
  388. Loaded?.Invoke (null, new ResizedEventArgs () { Rows = Driver.Rows, Cols = Driver.Cols });
  389. toplevel.WillPresent ();
  390. Redraw (toplevel);
  391. toplevel.PositionCursor ();
  392. Driver.Refresh ();
  393. return rs;
  394. }
  395. /// <summary>
  396. /// Building block API: completes the execution of a <see cref="Toplevel"/> that was started with <see cref="Begin(Toplevel)"/> .
  397. /// </summary>
  398. /// <param name="runState">The runstate returned by the <see cref="Begin(Toplevel)"/> method.</param>
  399. /// <param name="closeDriver"><c>true</c>Closes the application.<c>false</c>Closes the toplevels only.</param>
  400. public static void End (RunState runState, bool closeDriver = true)
  401. {
  402. if (runState == null)
  403. throw new ArgumentNullException (nameof (runState));
  404. runState.closeDriver = closeDriver;
  405. runState.Dispose ();
  406. }
  407. /// <summary>
  408. /// Shutdown an application initialized with <see cref="Init()"/>
  409. /// </summary>
  410. /// /// <param name="closeDriver"><c>true</c>Closes the application.<c>false</c>Closes toplevels only.</param>
  411. public static void Shutdown (bool closeDriver = true)
  412. {
  413. // Shutdown is the bookend for Init. As such it needs to clean up all resources
  414. // Init created. Apps that do any threading will need to code defensively for this.
  415. // e.g. see Issue #537
  416. // TODO: Some of this state is actually related to Begin/End (not Init/Shutdown) and should be moved to `RunState` (#520)
  417. foreach (var t in toplevels) {
  418. t.Running = false;
  419. }
  420. toplevels.Clear ();
  421. Current = null;
  422. CurrentView = null;
  423. Top = null;
  424. // Closes the application if it's true.
  425. if (closeDriver) {
  426. MainLoop = null;
  427. Driver.End ();
  428. Driver = null;
  429. }
  430. _initialized = false;
  431. }
  432. static void Redraw (View view)
  433. {
  434. Application.CurrentView = view;
  435. view.Redraw (view.Bounds);
  436. Driver.Refresh ();
  437. }
  438. static void Refresh (View view)
  439. {
  440. view.Redraw (view.Bounds);
  441. Driver.Refresh ();
  442. }
  443. /// <summary>
  444. /// Triggers a refresh of the entire display.
  445. /// </summary>
  446. public static void Refresh ()
  447. {
  448. Driver.UpdateScreen ();
  449. View last = null;
  450. foreach (var v in toplevels.Reverse ()) {
  451. v.SetNeedsDisplay ();
  452. v.Redraw (v.Bounds);
  453. last = v;
  454. }
  455. last?.PositionCursor ();
  456. Driver.Refresh ();
  457. }
  458. internal static void End (View view, bool closeDriver = true)
  459. {
  460. if (toplevels.Peek () != view)
  461. throw new ArgumentException ("The view that you end with must be balanced");
  462. toplevels.Pop ();
  463. if (toplevels.Count == 0)
  464. Shutdown (closeDriver);
  465. else {
  466. Current = toplevels.Peek ();
  467. Refresh ();
  468. }
  469. }
  470. /// <summary>
  471. /// Building block API: Runs the main loop for the created dialog
  472. /// </summary>
  473. /// <remarks>
  474. /// Use the wait parameter to control whether this is a
  475. /// blocking or non-blocking call.
  476. /// </remarks>
  477. /// <param name="state">The state returned by the Begin method.</param>
  478. /// <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>
  479. public static void RunLoop (RunState state, bool wait = true)
  480. {
  481. if (state == null)
  482. throw new ArgumentNullException (nameof (state));
  483. if (state.Toplevel == null)
  484. throw new ObjectDisposedException ("state");
  485. bool firstIteration = true;
  486. for (state.Toplevel.Running = true; state.Toplevel.Running;) {
  487. if (MainLoop.EventsPending (wait)) {
  488. // Notify Toplevel it's ready
  489. if (firstIteration) {
  490. state.Toplevel.OnReady ();
  491. }
  492. firstIteration = false;
  493. MainLoop.MainIteration ();
  494. Iteration?.Invoke (null, EventArgs.Empty);
  495. } else if (wait == false)
  496. return;
  497. if (state.Toplevel.NeedDisplay != null && (!state.Toplevel.NeedDisplay.IsEmpty || state.Toplevel.childNeedsDisplay)) {
  498. state.Toplevel.Redraw (state.Toplevel.Bounds);
  499. if (DebugDrawBounds)
  500. DrawBounds (state.Toplevel);
  501. state.Toplevel.PositionCursor ();
  502. Driver.Refresh ();
  503. } else
  504. Driver.UpdateCursor ();
  505. }
  506. }
  507. internal static bool DebugDrawBounds = false;
  508. // Need to look into why this does not work properly.
  509. static void DrawBounds (View v)
  510. {
  511. v.DrawFrame (v.Frame, padding: 0, fill: false);
  512. if (v.InternalSubviews != null && v.InternalSubviews.Count > 0)
  513. foreach (var sub in v.InternalSubviews)
  514. DrawBounds (sub);
  515. }
  516. /// <summary>
  517. /// Runs the application by calling <see cref="Run(Toplevel, bool)"/> with the value of <see cref="Top"/>
  518. /// </summary>
  519. public static void Run ()
  520. {
  521. Run (Top);
  522. }
  523. /// <summary>
  524. /// Runs the application by calling <see cref="Run(Toplevel, bool)"/> with a new instance of the specified <see cref="Toplevel"/>-derived class
  525. /// </summary>
  526. public static void Run<T> () where T : Toplevel, new()
  527. {
  528. Init (() => new T ());
  529. Run (Top);
  530. }
  531. /// <summary>
  532. /// Runs the main loop on the given <see cref="Toplevel"/> container.
  533. /// </summary>
  534. /// <remarks>
  535. /// <para>
  536. /// This method is used to start processing events
  537. /// for the main application, but it is also used to
  538. /// run other modal <see cref="View"/>s such as <see cref="Dialog"/> boxes.
  539. /// </para>
  540. /// <para>
  541. /// To make a <see cref="Run(Toplevel, bool)"/> stop execution, call <see cref="Application.RequestStop"/>.
  542. /// </para>
  543. /// <para>
  544. /// Calling <see cref="Run(Toplevel, bool)"/> is equivalent to calling <see cref="Begin(Toplevel)"/>, followed by <see cref="RunLoop(RunState, bool)"/>,
  545. /// and then calling <see cref="End(RunState, bool)"/>.
  546. /// </para>
  547. /// <para>
  548. /// Alternatively, to have a program control the main loop and
  549. /// process events manually, call <see cref="Begin(Toplevel)"/> to set things up manually and then
  550. /// repeatedly call <see cref="RunLoop(RunState, bool)"/> with the wait parameter set to false. By doing this
  551. /// the <see cref="RunLoop(RunState, bool)"/> method will only process any pending events, timers, idle handlers and
  552. /// then return control immediately.
  553. /// </para>
  554. /// </remarks>
  555. public static void Run (Toplevel view, bool closeDriver = true)
  556. {
  557. var runToken = Begin (view);
  558. RunLoop (runToken);
  559. End (runToken, closeDriver);
  560. }
  561. /// <summary>
  562. /// Stops running the most recent <see cref="Toplevel"/>.
  563. /// </summary>
  564. /// <remarks>
  565. /// <para>
  566. /// This will cause <see cref="Application.Run()"/> to return.
  567. /// </para>
  568. /// <para>
  569. /// Calling <see cref="Application.RequestStop"/> is equivalent to setting the <see cref="Toplevel.Running"/> property on the curently running <see cref="Toplevel"/> to false.
  570. /// </para>
  571. /// </remarks>
  572. public static void RequestStop ()
  573. {
  574. Current.Running = false;
  575. }
  576. /// <summary>
  577. /// Event arguments for the <see cref="Application.Resized"/> event.
  578. /// </summary>
  579. public class ResizedEventArgs : EventArgs {
  580. /// <summary>
  581. /// The number of rows in the resized terminal.
  582. /// </summary>
  583. public int Rows { get; set; }
  584. /// <summary>
  585. /// The number of columns in the resized terminal.
  586. /// </summary>
  587. public int Cols { get; set; }
  588. }
  589. /// <summary>
  590. /// Invoked when the terminal was resized. The new size of the terminal is provided.
  591. /// </summary>
  592. public static event EventHandler<ResizedEventArgs> Resized;
  593. static void TerminalResized ()
  594. {
  595. var full = new Rect (0, 0, Driver.Cols, Driver.Rows);
  596. Resized?.Invoke (null, new ResizedEventArgs () { Cols = full.Width, Rows = full.Height });
  597. Driver.Clip = full;
  598. foreach (var t in toplevels) {
  599. t.PositionToplevels ();
  600. t.SetRelativeLayout (full);
  601. t.LayoutSubviews ();
  602. }
  603. Refresh ();
  604. }
  605. }
  606. }