Application.cs 22 KB

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