From 8aa31245d729a7fd9d9a629067229e5624b17ba4 Mon Sep 17 00:00:00 2001 From: Miguel de Icaza Date: Tue, 12 Dec 2017 08:37:28 -0500 Subject: [PATCH] Basics --- Application.cs | 76 +++++++++++++++++++++++++++++++++++++++++++++++-- Terminal.csproj | 4 ++- driver.cs | 33 +++++++++++++++++---- 3 files changed, 104 insertions(+), 9 deletions(-) diff --git a/Application.cs b/Application.cs index b200552e9..3883d3429 100644 --- a/Application.cs +++ b/Application.cs @@ -16,10 +16,26 @@ namespace Terminal { public override string ToString() => $"[{X},{Y}:{Width},{Height}]"; } - public class View { + public class Responder { + public virtual Responder Next { get; set; } + public virtual bool IsFirstResponder => true; + public virtual bool CanBecomeFirstResponder => true; + public virtual bool CanResignFirstResponder => true; + public virtual void BecomeFirstResponder () {} + public virtual void ResignFirstResponder () {} + + // Key handling + public virtual void KeyDown (Event.Key kb) {} + + // Mouse events + public virtual void MouseEvent (Event.Mouse me) {} + } + + public class View : Responder { public static ConsoleDriver Driver = Application.Driver; - View [] subviews; - public View [] Subviews => subviews == null ? Array.Empty () : subviews; + public static IList empty = new List(0).AsReadOnly (); + List subviews; + public IList Subviews => subviews == null ? empty : subviews.AsReadOnly (); Rect frame; @@ -27,13 +43,41 @@ namespace Terminal { { this.frame = frame; } + + public void AddSubview (View view) + { + if (view == null) + return; + if (subviews == null) + subviews = new List (); + subviews.Add (view); + } + + + } + + public class ViewController : Responder { + View view; + public View View => view; + + public ViewController (View startup) + { + view = startup; + } } public class Window : View { + public ViewController RootViewController; + public Window (Rect frame) : base (frame) { } + public override void BecomeFirstResponder() + { + Application.MakeFirstResponder (this); + } + public static Window Toplevel () { return new Window (new Rect (0, 0, Driver.Cols, Driver.Rows)); @@ -42,9 +86,35 @@ namespace Terminal { public class Application { public static ConsoleDriver Driver = new CursesDriver (); + public Window MainWindow { get; private set; } + public Mono.Terminal.MainLoop MainLoop { get; private set; } + + static Stack responders = new Stack (); + static Responder responder; + + public static void MakeFirstResponder (Responder newResponder) + { + if (newResponder == null) + throw new ArgumentNullException (); + + responders.Push (responder); + responder = newResponder; + } public void Init () { + if (MainWindow != null) + return; + + MainLoop = new Mono.Terminal.MainLoop (); + MainWindow = Window.Toplevel (); + responder = MainWindow; + + MainLoop.AddWatch (0, Mono.Terminal.MainLoop.Condition.PollIn, x => { + //ProcessChar (); + + return true; + }); } } diff --git a/Terminal.csproj b/Terminal.csproj index e8015c55e..3b5e2f66b 100644 --- a/Terminal.csproj +++ b/Terminal.csproj @@ -34,10 +34,12 @@ + + - ../mono-curses/mono-curses.dll + $(MSBuildProjectDirectory)/../mono-curses/mono-curses.dll diff --git a/driver.cs b/driver.cs index 89dc9f84e..552979091 100644 --- a/driver.cs +++ b/driver.cs @@ -1,16 +1,39 @@ using System; using System.Collections.Generic; -using Mono.Terminal; +using C=Unix.Terminal; namespace Terminal { public abstract class ConsoleDriver { - public virtual int Cols {get;} - public virtual int Rows {get;} + public abstract int Cols {get;} + public abstract int Rows {get;} + public abstract void Init (); } public class CursesDriver : ConsoleDriver { - public override int Cols => Curses.Cols; - public override int Rows => Curses.Lines; + public override int Cols => C.Curses.Cols; + public override int Rows => C.Curses.Lines; + + public C.Curses.Window window; + + public override void Init() + { + if (window != null) + return; + + try { + window = C.Curses.initscr (); + } catch (Exception e){ + Console.WriteLine ("Curses failed to initialize, the exception is: " + e); + } + C.Curses.raw (); + C.Curses.noecho (); + C.Curses.Window.Standard.keypad (true); + + if (C.Curses.HasColors){ + C.Curses.StartColor (); + C.Curses.UseDefaultColors (); + } + } } } \ No newline at end of file