Miguel de Icaza 7 years ago
parent
commit
8aa31245d7
3 changed files with 104 additions and 9 deletions
  1. 73 3
      Application.cs
  2. 3 1
      Terminal.csproj
  3. 28 5
      driver.cs

+ 73 - 3
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<View> () : subviews;
+        public static IList<View> empty = new List<View>(0).AsReadOnly ();
+        List<View> subviews;
+        public IList<View> 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<View> ();
+            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<Responder> responders = new Stack<Responder> ();
+        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;
+			});
 
         }
     }

+ 3 - 1
Terminal.csproj

@@ -34,10 +34,12 @@
   <ItemGroup>
     <Compile Include="Application.cs" />
     <Compile Include="driver.cs" />
+    <Compile Include="Event.cs" />
+    <Compile Include="demo.cs" />
   </ItemGroup>
   <ItemGroup>
    <Reference Include="mono-curses.dll">
-     <HintPath>../mono-curses/mono-curses.dll</HintPath>
+     <HintPath>$(MSBuildProjectDirectory)/../mono-curses/mono-curses.dll</HintPath>
    </Reference>
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

+ 28 - 5
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 ();
+            }
+        }
     }
 }