Core.cs 23 KB

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