Core.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. //
  2. //
  3. // Pending:
  4. // - Check for NeedDisplay on the hierarchy and repaint
  5. // - Layout support
  6. // - "Colors" type or "Attributes" type?
  7. // - What to surface as "BackgroundCOlor" when clearing a window, an attribute or colors?
  8. //
  9. // Optimziations
  10. // - Add rendering limitation to the exposed area
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. namespace Terminal {
  15. public class Responder {
  16. public virtual bool CanFocus { get; set; }
  17. public virtual bool HasFocus { get; internal set; }
  18. // Key handling
  19. /// <summary>
  20. /// This method can be overwritten by view that
  21. /// want to provide accelerator functionality
  22. /// (Alt-key for example).
  23. /// </summary>
  24. /// <remarks>
  25. /// <para>
  26. /// Before keys are sent to the subview on the
  27. /// current view, all the views are
  28. /// processed and the key is passed to the widgets
  29. /// to allow some of them to process the keystroke
  30. /// as a hot-key. </para>
  31. /// <para>
  32. /// For example, if you implement a button that
  33. /// has a hotkey ok "o", you would catch the
  34. /// combination Alt-o here. If the event is
  35. /// caught, you must return true to stop the
  36. /// keystroke from being dispatched to other
  37. /// views.
  38. /// </para>
  39. /// </remarks>
  40. public virtual bool ProcessHotKey (KeyEvent kb)
  41. {
  42. return false;
  43. }
  44. /// <summary>
  45. /// If the view is focused, gives the view a
  46. /// chance to process the keystroke.
  47. /// </summary>
  48. /// <remarks>
  49. /// <para>
  50. /// Views can override this method if they are
  51. /// interested in processing the given keystroke.
  52. /// If they consume the keystroke, they must
  53. /// return true to stop the keystroke from being
  54. /// processed by other widgets or consumed by the
  55. /// widget engine. If they return false, the
  56. /// keystroke will be passed using the ProcessColdKey
  57. /// method to other views to process.
  58. /// </para>
  59. /// </remarks>
  60. public virtual bool ProcessKey (KeyEvent kb)
  61. {
  62. return false;
  63. }
  64. /// <summary>
  65. /// This method can be overwritten by views that
  66. /// want to provide accelerator functionality
  67. /// (Alt-key for example), but without
  68. /// interefering with normal ProcessKey behavior.
  69. /// </summary>
  70. /// <remarks>
  71. /// <para>
  72. /// After keys are sent to the subviews on the
  73. /// current view, all the view are
  74. /// processed and the key is passed to the views
  75. /// to allow some of them to process the keystroke
  76. /// as a cold-key. </para>
  77. /// <para>
  78. /// This functionality is used, for example, by
  79. /// default buttons to act on the enter key.
  80. /// Processing this as a hot-key would prevent
  81. /// non-default buttons from consuming the enter
  82. /// keypress when they have the focus.
  83. /// </para>
  84. /// </remarks>
  85. public virtual bool ProcessColdKey (KeyEvent kb)
  86. {
  87. return false;
  88. }
  89. // Mouse events
  90. public virtual void MouseEvent (Event.Mouse me) { }
  91. }
  92. public class View : Responder, IEnumerable {
  93. string id = "";
  94. View container = null;
  95. View focused = null;
  96. public static ConsoleDriver Driver = Application.Driver;
  97. public static IList<View> empty = new List<View> (0).AsReadOnly ();
  98. List<View> subviews;
  99. public IList<View> Subviews => subviews == null ? empty : subviews.AsReadOnly ();
  100. internal bool NeedDisplay { get; private set; } = true;
  101. // The frame for the object
  102. Rect frame;
  103. public string Id {
  104. get => id;
  105. set {
  106. id = value;
  107. }
  108. }
  109. // The frame for this view
  110. public Rect Frame {
  111. get => frame;
  112. set {
  113. frame = value;
  114. SetNeedsDisplay ();
  115. }
  116. }
  117. public IEnumerator GetEnumerator ()
  118. {
  119. foreach (var v in subviews)
  120. yield return v;
  121. }
  122. public Rect Bounds {
  123. get => new Rect (Point.Empty, Frame.Size);
  124. set {
  125. Frame = new Rect (frame.Location, value.Size);
  126. }
  127. }
  128. public View SuperView => container;
  129. public View (Rect frame)
  130. {
  131. this.Frame = frame;
  132. CanFocus = false;
  133. }
  134. /// <summary>
  135. /// Invoke to flag that this view needs to be redisplayed, by any code
  136. /// that alters the state of the view.
  137. /// </summary>
  138. public void SetNeedsDisplay ()
  139. {
  140. NeedDisplay = true;
  141. if (container != null)
  142. container.ChildNeedsDisplay ();
  143. }
  144. /// <summary>
  145. /// Sets the NeedsDisplay flag on this view and any child subviews.
  146. /// </summary>
  147. public void SetNeedsDisplayRecursive ()
  148. {
  149. NeedDisplay = true;
  150. if (subviews == null)
  151. return;
  152. foreach (var view in subviews)
  153. view.SetNeedsDisplayRecursive ();
  154. if (container != null)
  155. container.ChildNeedsDisplay ();
  156. }
  157. internal bool childNeedsDisplay;
  158. public void ChildNeedsDisplay ()
  159. {
  160. childNeedsDisplay = true;
  161. if (container != null)
  162. container.ChildNeedsDisplay ();
  163. }
  164. /// <summary>
  165. /// Adds a subview to this view.
  166. /// </summary>
  167. /// <remarks>
  168. /// </remarks>
  169. public virtual void Add (View view)
  170. {
  171. if (view == null)
  172. return;
  173. if (subviews == null)
  174. subviews = new List<View> ();
  175. subviews.Add (view);
  176. view.container = this;
  177. if (view.CanFocus)
  178. CanFocus = true;
  179. SetNeedsDisplay ();
  180. }
  181. public void Add (params View [] views)
  182. {
  183. if (views == null)
  184. return;
  185. foreach (var view in views)
  186. Add (view);
  187. }
  188. /// <summary>
  189. /// Removes all the widgets from this container.
  190. /// </summary>
  191. /// <remarks>
  192. /// </remarks>
  193. public virtual void RemoveAll ()
  194. {
  195. if (subviews == null)
  196. return;
  197. while (subviews.Count > 0) {
  198. var view = subviews [0];
  199. Remove (view);
  200. subviews.RemoveAt (0);
  201. }
  202. }
  203. /// <summary>
  204. /// Removes a widget from this container.
  205. /// </summary>
  206. /// <remarks>
  207. /// </remarks>
  208. public virtual void Remove (View view)
  209. {
  210. if (view == null)
  211. return;
  212. SetNeedsDisplay ();
  213. var touched = view.Frame;
  214. subviews.Remove (view);
  215. view.container = null;
  216. if (subviews.Count < 1)
  217. this.CanFocus = false;
  218. foreach (var v in subviews) {
  219. if (v.Frame.IntersectsWith (touched))
  220. view.SetNeedsDisplay ();
  221. }
  222. }
  223. /// <summary>
  224. /// Clears the view region with the current color.
  225. /// </summary>
  226. /// <remarks>
  227. /// <para>
  228. /// This clears the entire region used by this view.
  229. /// </para>
  230. /// </remarks>
  231. public void Clear ()
  232. {
  233. var h = Frame.Height;
  234. var w = Frame.Width;
  235. for (int line = 0; line < h; line++) {
  236. Move (0, line);
  237. for (int col = 0; col < w; col++)
  238. Driver.AddCh (' ');
  239. }
  240. }
  241. /// <summary>
  242. /// Converts the (col,row) position from the view into a screen (col,row). The values are clamped to (0..ScreenDim-1)
  243. /// </summary>
  244. /// <param name="col">View-based column.</param>
  245. /// <param name="row">View-based row.</param>
  246. /// <param name="rcol">Absolute column, display relative.</param>
  247. /// <param name="rrow">Absolute row, display relative.</param>
  248. internal void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
  249. {
  250. // Computes the real row, col relative to the screen.
  251. rrow = row + frame.Y;
  252. rcol = col + frame.X;
  253. var ccontainer = container;
  254. while (ccontainer != null) {
  255. rrow += ccontainer.frame.Y;
  256. rcol += ccontainer.frame.X;
  257. ccontainer = ccontainer.container;
  258. }
  259. // The following ensures that the cursor is always in the screen boundaries.
  260. if (clipped) {
  261. rrow = Math.Max (0, Math.Min (rrow, Driver.Rows - 1));
  262. rcol = Math.Max (0, Math.Min (rcol, Driver.Cols - 1));
  263. }
  264. }
  265. // Converts a rectangle in view coordinates to screen coordinates.
  266. Rect RectToScreen (Rect rect)
  267. {
  268. ViewToScreen (rect.X, rect.Y, out var x, out var y, clipped: false);
  269. return new Rect (x, y, rect.Width, rect.Height);
  270. }
  271. // Clips a rectangle in screen coordinates to the dimensions currently available on the screen
  272. Rect ScreenClip (Rect rect)
  273. {
  274. var x = rect.X < 0 ? 0 : rect.X;
  275. var y = rect.Y < 0 ? 0 : rect.Y;
  276. var w = rect.X + rect.Width >= Driver.Cols ? Driver.Cols - rect.X : rect.Width;
  277. var h = rect.Y + rect.Height >= Driver.Rows ? Driver.Rows - rect.Y : rect.Height;
  278. return new Rect (x, y, w, h);
  279. }
  280. /// <summary>
  281. /// Draws a frame in the current view, clipped by the boundary of this view
  282. /// </summary>
  283. /// <param name="rect">Rectangular region for the frame to be drawn.</param>
  284. /// <param name="fill">If set to <c>true</c> it fill will the contents.</param>
  285. public void DrawFrame (Rect rect, bool fill = false)
  286. {
  287. var scrRect = RectToScreen (rect);
  288. var savedClip = Driver.Clip;
  289. Driver.Clip = ScreenClip (RectToScreen (Bounds));
  290. Driver.DrawFrame (scrRect, fill);
  291. Driver.Clip = savedClip;
  292. }
  293. /// <summary>
  294. /// Utility function to draw strings that contain a hotkey
  295. /// </summary>
  296. /// <param name="s">String to display, the underscoore before a letter flags the next letter as the hotkey.</param>
  297. /// <param name="hotColor">Hot color.</param>
  298. /// <param name="normalColor">Normal color.</param>
  299. public void DrawHotString (string text, Attribute hotColor, Attribute normalColor)
  300. {
  301. Driver.SetAttribute (normalColor);
  302. foreach (var c in text) {
  303. if (c == '_') {
  304. Driver.SetAttribute (hotColor);
  305. continue;
  306. }
  307. Driver.AddCh (c);
  308. Driver.SetAttribute (normalColor);
  309. }
  310. }
  311. /// <summary>
  312. /// This moves the cursor to the specified column and row in the view.
  313. /// </summary>
  314. /// <returns>The move.</returns>
  315. /// <param name="col">Col.</param>
  316. /// <param name="row">Row.</param>
  317. public void Move (int col, int row)
  318. {
  319. ViewToScreen (col, row, out var rcol, out var rrow);
  320. Driver.Move (rcol, rrow);
  321. }
  322. /// <summary>
  323. /// Positions the cursor in the right position based on the currently focused view in the chain.
  324. /// </summary>
  325. public virtual void PositionCursor ()
  326. {
  327. if (focused != null)
  328. focused.PositionCursor ();
  329. else
  330. Move (frame.X, frame.Y);
  331. }
  332. public override bool HasFocus {
  333. get {
  334. return base.HasFocus;
  335. }
  336. internal set {
  337. if (base.HasFocus != value)
  338. SetNeedsDisplay ();
  339. base.HasFocus = value;
  340. }
  341. }
  342. /// <summary>
  343. /// Returns the currently focused view inside this view, or null if nothing is focused.
  344. /// </summary>
  345. /// <value>The focused.</value>
  346. public View Focused => focused;
  347. public View MostFocused {
  348. get {
  349. if (Focused == null)
  350. return null;
  351. var most = Focused.MostFocused;
  352. if (most != null)
  353. return most;
  354. return Focused;
  355. }
  356. }
  357. /// <summary>
  358. /// Displays the specified character in the specified column and row.
  359. /// </summary>
  360. /// <param name="col">Col.</param>
  361. /// <param name="row">Row.</param>
  362. /// <param name="ch">Ch.</param>
  363. public void AddCh (int col, int row, int ch)
  364. {
  365. if (row < 0 || col < 0)
  366. return;
  367. if (row > frame.Height - 1 || col > frame.Width - 1)
  368. return;
  369. Move (col, row);
  370. Driver.AddCh (ch);
  371. }
  372. /// <summary>
  373. /// Performs a redraw of this view and its subviews, only redraws the views that have been flagged for a re-display.
  374. /// </summary>
  375. /// <remarks>
  376. /// The region argument is relative to the view itself.
  377. /// </remarks>
  378. public virtual void Redraw (Rect region)
  379. {
  380. var clipRect = new Rect (Point.Empty, frame.Size);
  381. if (subviews != null) {
  382. foreach (var view in subviews) {
  383. if (view.NeedDisplay || view.childNeedsDisplay) {
  384. if (view.Frame.IntersectsWith (clipRect) && view.Frame.IntersectsWith (region)) {
  385. // TODO: optimize this by computing the intersection of region and view.Bounds
  386. view.Redraw (view.Bounds);
  387. }
  388. view.NeedDisplay = false;
  389. view.childNeedsDisplay = false;
  390. }
  391. }
  392. }
  393. NeedDisplay = false;
  394. childNeedsDisplay = false;
  395. }
  396. /// <summary>
  397. /// Focuses the specified sub-view.
  398. /// </summary>
  399. /// <param name="view">View.</param>
  400. public void SetFocus (View view)
  401. {
  402. if (view == null)
  403. return;
  404. //Console.WriteLine ($"Request to focus {view}");
  405. if (!view.CanFocus)
  406. return;
  407. if (focused == view)
  408. return;
  409. // Make sure that this view is a subview
  410. View c;
  411. for (c = view.container; c != null; c = c.container)
  412. if (c == this)
  413. break;
  414. if (c == null)
  415. throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
  416. if (focused != null)
  417. focused.HasFocus = false;
  418. focused = view;
  419. focused.HasFocus = true;
  420. focused.EnsureFocus ();
  421. }
  422. public override bool ProcessKey (KeyEvent kb)
  423. {
  424. if (Focused?.ProcessKey (kb) == true)
  425. return true;
  426. return false;
  427. }
  428. public override bool ProcessHotKey (KeyEvent kb)
  429. {
  430. if (subviews == null || subviews.Count == 0)
  431. return false;
  432. foreach (var view in subviews)
  433. if (view.ProcessHotKey (kb))
  434. return true;
  435. return false;
  436. }
  437. public override bool ProcessColdKey (KeyEvent kb)
  438. {
  439. if (subviews == null || subviews.Count == 0)
  440. return false;
  441. foreach (var view in subviews)
  442. if (view.ProcessHotKey (kb))
  443. return true;
  444. return false;
  445. }
  446. /// <summary>
  447. /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, it does nothing.
  448. /// </summary>
  449. public void EnsureFocus ()
  450. {
  451. if (focused == null)
  452. FocusFirst ();
  453. }
  454. /// <summary>
  455. /// Focuses the first focusable subview if one exists.
  456. /// </summary>
  457. public void FocusFirst ()
  458. {
  459. if (subviews == null) {
  460. SuperView.SetFocus (this);
  461. return;
  462. }
  463. foreach (var view in subviews) {
  464. if (view.CanFocus) {
  465. SetFocus (view);
  466. return;
  467. }
  468. }
  469. }
  470. /// <summary>
  471. /// Focuses the last focusable subview if one exists.
  472. /// </summary>
  473. public void FocusLast ()
  474. {
  475. if (subviews == null)
  476. return;
  477. for (int i = subviews.Count; i > 0;) {
  478. i--;
  479. View v = subviews [i];
  480. if (v.CanFocus) {
  481. SetFocus (v);
  482. return;
  483. }
  484. }
  485. }
  486. /// <summary>
  487. /// Focuses the previous view.
  488. /// </summary>
  489. /// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
  490. public bool FocusPrev ()
  491. {
  492. if (focused == null) {
  493. FocusLast ();
  494. return true;
  495. }
  496. int focused_idx = -1;
  497. for (int i = subviews.Count; i > 0;) {
  498. i--;
  499. View w = subviews [i];
  500. if (w.HasFocus) {
  501. if (w.FocusPrev ())
  502. return true;
  503. focused_idx = i;
  504. continue;
  505. }
  506. if (w.CanFocus && focused_idx != -1) {
  507. focused.HasFocus = false;
  508. if (w.CanFocus)
  509. w.FocusLast ();
  510. SetFocus (w);
  511. return true;
  512. }
  513. }
  514. if (focused != null) {
  515. focused.HasFocus = false;
  516. focused = null;
  517. }
  518. return false;
  519. }
  520. /// <summary>
  521. /// Focuses the next view.
  522. /// </summary>
  523. /// <returns><c>true</c>, if next was focused, <c>false</c> otherwise.</returns>
  524. public bool FocusNext ()
  525. {
  526. if (subviews == null || subviews.Count == 0)
  527. return false;
  528. if (focused == null) {
  529. FocusFirst ();
  530. return focused != null;
  531. }
  532. int n = subviews.Count;
  533. int focused_idx = -1;
  534. for (int i = 0; i < n; i++) {
  535. View w = subviews [i];
  536. if (w.HasFocus) {
  537. if (w.FocusNext ())
  538. return true;
  539. focused_idx = i;
  540. continue;
  541. }
  542. if (w.CanFocus && focused_idx != -1) {
  543. focused.HasFocus = false;
  544. if (w != null && w.CanFocus)
  545. w.FocusFirst ();
  546. SetFocus (w);
  547. return true;
  548. }
  549. }
  550. if (focused != null) {
  551. focused.HasFocus = false;
  552. focused = null;
  553. }
  554. return false;
  555. }
  556. public virtual void LayoutSubviews ()
  557. {
  558. }
  559. public override string ToString ()
  560. {
  561. return $"{GetType ().Name}({id})({Frame})";
  562. }
  563. }
  564. /// <summary>
  565. /// Toplevel views can be modally executed.
  566. /// </summary>
  567. public class Toplevel : View {
  568. public bool Running;
  569. public Toplevel (Rect frame) : base (frame)
  570. {
  571. }
  572. public static Toplevel Create ()
  573. {
  574. return new Toplevel (new Rect (0, 0, Driver.Cols, Driver.Rows));
  575. }
  576. public override bool CanFocus {
  577. get => true;
  578. }
  579. public override bool ProcessKey (KeyEvent kb)
  580. {
  581. if (ProcessHotKey (kb))
  582. return true;
  583. if (base.ProcessKey (kb))
  584. return true;
  585. // Process the key normally
  586. if (ProcessColdKey (kb))
  587. return true;
  588. switch (kb.Key) {
  589. case Key.ControlC:
  590. // TODO: stop current execution of this container
  591. break;
  592. case Key.ControlZ:
  593. // TODO: should suspend
  594. // console_csharp_send_sigtstp ();
  595. break;
  596. case Key.Tab:
  597. var old = Focused;
  598. if (!FocusNext ())
  599. FocusNext ();
  600. if (old != Focused) {
  601. old?.SetNeedsDisplay ();
  602. Focused?.SetNeedsDisplay ();
  603. }
  604. return true;
  605. case Key.BackTab:
  606. old = Focused;
  607. if (!FocusPrev ())
  608. FocusPrev ();
  609. if (old != Focused) {
  610. old?.SetNeedsDisplay ();
  611. Focused?.SetNeedsDisplay ();
  612. }
  613. return true;
  614. case Key.ControlL:
  615. SetNeedsDisplayRecursive ();
  616. return true;
  617. }
  618. return false;
  619. }
  620. #if false
  621. public override void Redraw ()
  622. {
  623. base.Redraw ();
  624. for (int i = 0; i < Driver.Cols; i++) {
  625. Driver.Move (0, i);
  626. Driver.AddStr ("Line: " + i);
  627. }
  628. }
  629. #endif
  630. }
  631. /// <summary>
  632. /// A toplevel view that draws a frame around its region
  633. /// </summary>
  634. public class Window : Toplevel, IEnumerable {
  635. View contentView;
  636. string title;
  637. public string Title {
  638. get => title;
  639. set {
  640. title = value;
  641. SetNeedsDisplay ();
  642. }
  643. }
  644. class ContentView : View {
  645. public ContentView (Rect frame) : base (frame) { }
  646. }
  647. public Window (Rect frame, string title = null) : base (frame)
  648. {
  649. this.Title = title;
  650. frame.Inflate (-1, -1);
  651. contentView = new ContentView (frame);
  652. base.Add (contentView);
  653. }
  654. public new IEnumerator GetEnumerator ()
  655. {
  656. return contentView.GetEnumerator ();
  657. }
  658. void DrawFrame ()
  659. {
  660. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), true);
  661. }
  662. public override void Add (View view)
  663. {
  664. contentView.Add (view);
  665. }
  666. public override void Redraw (Rect bounds)
  667. {
  668. if (NeedDisplay) {
  669. Driver.SetAttribute (Colors.Base.Normal);
  670. DrawFrame ();
  671. if (HasFocus)
  672. Driver.SetAttribute (Colors.Dialog.Normal);
  673. var width = Frame.Width;
  674. if (Title != null && width > 4) {
  675. Move (1, 0);
  676. Driver.AddCh (' ');
  677. var str = Title.Length > width ? Title.Substring (0, width - 4) : Title;
  678. Driver.AddStr (str);
  679. Driver.AddCh (' ');
  680. }
  681. Driver.SetAttribute (Colors.Dialog.Normal);
  682. }
  683. contentView.Redraw (contentView.Bounds);
  684. }
  685. }
  686. public class Application {
  687. public static ConsoleDriver Driver = new CursesDriver ();
  688. public static Toplevel Top { get; private set; }
  689. public static Mono.Terminal.MainLoop MainLoop { get; private set; }
  690. static Stack<View> toplevels = new Stack<View> ();
  691. static Responder focus;
  692. /// <summary>
  693. /// This event is raised on each iteration of the
  694. /// main loop.
  695. /// </summary>
  696. /// <remarks>
  697. /// See also <see cref="Timeout"/>
  698. /// </remarks>
  699. static public event EventHandler Iteration;
  700. public static void MakeFirstResponder (Responder newResponder)
  701. {
  702. if (newResponder == null)
  703. throw new ArgumentNullException ();
  704. throw new NotImplementedException ();
  705. }
  706. /// <summary>
  707. /// Initializes the Application
  708. /// </summary>
  709. public static void Init ()
  710. {
  711. if (Top != null)
  712. return;
  713. Driver.Init (TerminalResized);
  714. MainLoop = new Mono.Terminal.MainLoop ();
  715. Top = Toplevel.Create ();
  716. focus = Top;
  717. }
  718. public class RunState : IDisposable {
  719. internal RunState (Toplevel view)
  720. {
  721. Toplevel = view;
  722. }
  723. internal Toplevel Toplevel;
  724. public void Dispose ()
  725. {
  726. Dispose (true);
  727. GC.SuppressFinalize (this);
  728. }
  729. public virtual void Dispose (bool disposing)
  730. {
  731. if (Toplevel != null) {
  732. Application.End (Toplevel);
  733. Toplevel = null;
  734. }
  735. }
  736. }
  737. static void KeyEvent (Key key)
  738. {
  739. }
  740. static public RunState Begin (Toplevel toplevel)
  741. {
  742. if (toplevel == null)
  743. throw new ArgumentNullException (nameof (toplevel));
  744. var rs = new RunState (toplevel);
  745. Init ();
  746. toplevels.Push (toplevel);
  747. Driver.PrepareToRun (MainLoop, toplevel);
  748. toplevel.LayoutSubviews ();
  749. toplevel.FocusFirst ();
  750. Redraw (toplevel);
  751. toplevel.PositionCursor ();
  752. Driver.Refresh ();
  753. return rs;
  754. }
  755. static public void End (RunState rs)
  756. {
  757. if (rs == null)
  758. throw new ArgumentNullException (nameof (rs));
  759. rs.Dispose ();
  760. }
  761. static void Shutdown ()
  762. {
  763. Driver.End ();
  764. }
  765. static void Redraw (View view)
  766. {
  767. view.Redraw (view.Bounds);
  768. Driver.Refresh ();
  769. }
  770. static void Refresh (View view)
  771. {
  772. view.Redraw (view.Bounds);
  773. Driver.Refresh ();
  774. }
  775. public static void Refresh ()
  776. {
  777. Driver.RedrawTop ();
  778. View last = null;
  779. foreach (var v in toplevels) {
  780. v.Redraw (v.Bounds);
  781. last = v;
  782. }
  783. if (last != null)
  784. last.PositionCursor ();
  785. Driver.Refresh ();
  786. }
  787. internal static void End (View view)
  788. {
  789. if (toplevels.Peek () != view)
  790. throw new ArgumentException ("The view that you end with must be balanced");
  791. toplevels.Pop ();
  792. if (toplevels.Count == 0)
  793. Shutdown ();
  794. else
  795. Refresh ();
  796. }
  797. /// <summary>
  798. /// Runs the main loop for the created dialog
  799. /// </summary>
  800. /// <remarks>
  801. /// Use the wait parameter to control whether this is a
  802. /// blocking or non-blocking call.
  803. /// </remarks>
  804. public static void RunLoop (RunState state, bool wait = true)
  805. {
  806. if (state == null)
  807. throw new ArgumentNullException (nameof (state));
  808. if (state.Toplevel == null)
  809. throw new ObjectDisposedException ("state");
  810. for (state.Toplevel.Running = true; state.Toplevel.Running;) {
  811. if (MainLoop.EventsPending (wait)) {
  812. MainLoop.MainIteration ();
  813. if (Iteration != null)
  814. Iteration (null, EventArgs.Empty);
  815. } else if (wait == false)
  816. return;
  817. if (state.Toplevel.NeedDisplay || state.Toplevel.childNeedsDisplay) {
  818. state.Toplevel.Redraw (state.Toplevel.Bounds);
  819. state.Toplevel.PositionCursor ();
  820. Driver.Refresh ();
  821. }
  822. }
  823. }
  824. public static void Run ()
  825. {
  826. Run (Top);
  827. }
  828. /// <summary>
  829. /// Runs the main loop on the given container.
  830. /// </summary>
  831. /// <remarks>
  832. /// This method is used to start processing events
  833. /// for the main application, but it is also used to
  834. /// run modal dialog boxes.
  835. /// </remarks>
  836. public static void Run (Toplevel view)
  837. {
  838. var runToken = Begin (view);
  839. RunLoop (runToken);
  840. End (runToken);
  841. }
  842. static void TerminalResized ()
  843. {
  844. foreach (var t in toplevels) {
  845. t.Frame = new Rect (0, 0, Driver.Cols, Driver.Rows);
  846. }
  847. }
  848. }
  849. }