Application.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Terminal {
  4. public struct Rect {
  5. public int X, Y, Width, Height;
  6. public Rect (int x, int y, int width, int height)
  7. {
  8. X = x;
  9. Y = y;
  10. Width = width;
  11. Height = height;
  12. }
  13. public override string ToString() => $"[{X},{Y}:{Width},{Height}]";
  14. }
  15. public class Responder {
  16. public virtual Responder Next { get; set; }
  17. public virtual bool IsFirstResponder => true;
  18. public virtual bool CanBecomeFirstResponder => true;
  19. public virtual bool CanResignFirstResponder => true;
  20. public virtual void BecomeFirstResponder () {}
  21. public virtual void ResignFirstResponder () {}
  22. // Key handling
  23. public virtual void KeyDown (Event.Key kb) {}
  24. // Mouse events
  25. public virtual void MouseEvent (Event.Mouse me) {}
  26. }
  27. public class View : Responder {
  28. public static ConsoleDriver Driver = Application.Driver;
  29. public static IList<View> empty = new List<View>(0).AsReadOnly ();
  30. List<View> subviews;
  31. public IList<View> Subviews => subviews == null ? empty : subviews.AsReadOnly ();
  32. Rect frame;
  33. public View (Rect frame)
  34. {
  35. this.frame = frame;
  36. }
  37. public void AddSubview (View view)
  38. {
  39. if (view == null)
  40. return;
  41. if (subviews == null)
  42. subviews = new List<View> ();
  43. subviews.Add (view);
  44. }
  45. }
  46. public class ViewController : Responder {
  47. View view;
  48. public View View => view;
  49. public ViewController (View startup)
  50. {
  51. view = startup;
  52. }
  53. }
  54. public class Window : View {
  55. public ViewController RootViewController;
  56. public Window (Rect frame) : base (frame)
  57. {
  58. }
  59. public override void BecomeFirstResponder()
  60. {
  61. Application.MakeFirstResponder (this);
  62. }
  63. public static Window Toplevel ()
  64. {
  65. return new Window (new Rect (0, 0, Driver.Cols, Driver.Rows));
  66. }
  67. }
  68. public class Application {
  69. public static ConsoleDriver Driver = new CursesDriver ();
  70. public Window MainWindow { get; private set; }
  71. public Mono.Terminal.MainLoop MainLoop { get; private set; }
  72. static Stack<Responder> responders = new Stack<Responder> ();
  73. static Responder responder;
  74. public static void MakeFirstResponder (Responder newResponder)
  75. {
  76. if (newResponder == null)
  77. throw new ArgumentNullException ();
  78. responders.Push (responder);
  79. responder = newResponder;
  80. }
  81. public void Init ()
  82. {
  83. if (MainWindow != null)
  84. return;
  85. MainLoop = new Mono.Terminal.MainLoop ();
  86. MainWindow = Window.Toplevel ();
  87. responder = MainWindow;
  88. MainLoop.AddWatch (0, Mono.Terminal.MainLoop.Condition.PollIn, x => {
  89. //ProcessChar ();
  90. return true;
  91. });
  92. }
  93. }
  94. }