Browse Source

Remove magic from Toplevel for event processing into its own method

Miguel de Icaza 7 years ago
parent
commit
6666d19202
4 changed files with 31 additions and 21 deletions
  1. 20 10
      Core.cs
  2. 9 9
      Driver.cs
  3. 1 1
      Views/Menu.cs
  4. 1 1
      demo.cs

+ 20 - 10
Core.cs

@@ -561,6 +561,9 @@ namespace Terminal {
 		/// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
 		/// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
 		public bool FocusPrev ()
 		public bool FocusPrev ()
 		{
 		{
+			if (subviews == null || subviews.Count == 0)
+				return false;
+
 			if (focused == null) {
 			if (focused == null) {
 				FocusLast ();
 				FocusLast ();
 				return true;
 				return true;
@@ -586,6 +589,10 @@ namespace Terminal {
 					return true;
 					return true;
 				}
 				}
 			}
 			}
+			if (focused_idx != -1) {
+				FocusLast ();
+				return true;
+			}
 
 
 			if (focused != null) {
 			if (focused != null) {
 				focused.HasFocus = false;
 				focused.HasFocus = false;
@@ -666,16 +673,9 @@ namespace Terminal {
 
 
 		public override bool ProcessKey (KeyEvent kb)
 		public override bool ProcessKey (KeyEvent kb)
 		{
 		{
-			if (ProcessHotKey (kb))
-				return true;
-
 			if (base.ProcessKey (kb))
 			if (base.ProcessKey (kb))
 				return true;
 				return true;
 
 
-			// Process the key normally
-			if (ProcessColdKey (kb))
-				return true;
-
 			switch (kb.Key) {
 			switch (kb.Key) {
 			case Key.ControlC:
 			case Key.ControlC:
 				// TODO: stop current execution of this container
 				// TODO: stop current execution of this container
@@ -694,7 +694,7 @@ namespace Terminal {
 				}
 				}
 				return true;
 				return true;
 			case Key.BackTab:
 			case Key.BackTab:
-				old = Focused;
+			old = Focused;
 				if (!FocusPrev ())
 				if (!FocusPrev ())
 					FocusPrev ();
 					FocusPrev ();
 				if (old != Focused) {
 				if (old != Focused) {
@@ -875,10 +875,20 @@ namespace Terminal {
 			}
 			}
 		}
 		}
 
 
-		static void KeyEvent (Key key)
+		static void ProcessKeyEvent (KeyEvent ke)
 		{
 		{
+			if (Top.ProcessHotKey (ke))
+				return;
+
+			if (Top.ProcessKey (ke))
+				return;
+			
+			// Process the key normally
+			if (Top.ProcessColdKey (ke))
+				return;
 		}
 		}
 
 
+
 		static public RunState Begin (Toplevel toplevel)
 		static public RunState Begin (Toplevel toplevel)
 		{
 		{
 			if (toplevel == null)
 			if (toplevel == null)
@@ -887,7 +897,7 @@ namespace Terminal {
 
 
 			Init ();
 			Init ();
 			toplevels.Push (toplevel);
 			toplevels.Push (toplevel);
-			Driver.PrepareToRun (MainLoop, toplevel);
+			Driver.PrepareToRun (MainLoop, ProcessKeyEvent);
 			toplevel.LayoutSubviews ();
 			toplevel.LayoutSubviews ();
 			toplevel.FocusFirst ();
 			toplevel.FocusFirst ();
 			Redraw (toplevel);
 			Redraw (toplevel);

+ 9 - 9
Driver.cs

@@ -78,7 +78,7 @@ namespace Terminal {
 		public abstract void Move (int col, int row);
 		public abstract void Move (int col, int row);
 		public abstract void AddCh (int ch);
 		public abstract void AddCh (int ch);
 		public abstract void AddStr (string str);
 		public abstract void AddStr (string str);
-		public abstract void PrepareToRun (MainLoop mainLoop, Responder target);
+		public abstract void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> target);
 		public abstract void Refresh ();
 		public abstract void Refresh ();
 		public abstract void End ();
 		public abstract void End ();
 		public abstract void RedrawTop ();
 		public abstract void RedrawTop ();
@@ -223,7 +223,7 @@ namespace Terminal {
 			}
 			}
 		}
 		}
 
 
-		void ProcessInput (Responder handler)
+		void ProcessInput (Action<KeyEvent> keyHandler)
 		{
 		{
 			int wch;
 			int wch;
 			var code = Curses.get_wch (out wch);
 			var code = Curses.get_wch (out wch);
@@ -241,7 +241,7 @@ namespace Terminal {
 					// handler.HandleMouse ();
 					// handler.HandleMouse ();
 					return;
 					return;
 				}
 				}
-				handler.ProcessKey (new KeyEvent (MapCursesKey (wch)));
+				keyHandler (new KeyEvent (MapCursesKey (wch)));
 				return;
 				return;
 			}
 			}
 
 
@@ -251,7 +251,7 @@ namespace Terminal {
 
 
 				code = Curses.get_wch (out wch);
 				code = Curses.get_wch (out wch);
 				if (code == Curses.KEY_CODE_YES)
 				if (code == Curses.KEY_CODE_YES)
-					handler.ProcessKey (new KeyEvent (Key.AltMask | MapCursesKey (wch)));
+					keyHandler (new KeyEvent (Key.AltMask | MapCursesKey (wch)));
 				if (code == 0) {
 				if (code == 0) {
 					KeyEvent key;
 					KeyEvent key;
 
 
@@ -264,19 +264,19 @@ namespace Terminal {
 						key = new KeyEvent ((Key)wch);
 						key = new KeyEvent ((Key)wch);
 					else
 					else
 						key = new KeyEvent (Key.AltMask | (Key)wch);
 						key = new KeyEvent (Key.AltMask | (Key)wch);
-					handler.ProcessKey (key);
+					keyHandler (key);
 				} else
 				} else
-					handler.ProcessKey (new KeyEvent (Key.Esc));
+					keyHandler (new KeyEvent (Key.Esc));
 			} else
 			} else
-				handler.ProcessKey (new KeyEvent ((Key)wch));
+				keyHandler (new KeyEvent ((Key)wch));
 		}
 		}
 
 
-		public override void PrepareToRun (MainLoop mainLoop, Responder handler)
+		public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler)
 		{
 		{
 			Curses.timeout (-1);
 			Curses.timeout (-1);
 
 
 			mainLoop.AddWatch (0, Mono.Terminal.MainLoop.Condition.PollIn, x => {
 			mainLoop.AddWatch (0, Mono.Terminal.MainLoop.Condition.PollIn, x => {
-				ProcessInput (handler);
+				ProcessInput (keyHandler);
 				return true;
 				return true;
 			});
 			});
 
 

+ 1 - 1
Views/Menu.cs

@@ -190,7 +190,7 @@ namespace Terminal {
 				}
 				}
 				break;
 				break;
 			}
 			}
-			return true;
+			return false;
 		}
 		}
 	}
 	}
 
 

+ 1 - 1
demo.cs

@@ -33,7 +33,7 @@ class Demo {
 		var win = new Window (new Rect (0, 1, tframe.Width, tframe.Height-1), "Hello");
 		var win = new Window (new Rect (0, 1, tframe.Width, tframe.Height-1), "Hello");
 		var menu = new MenuBar (new MenuBarItem [] {
 		var menu = new MenuBar (new MenuBarItem [] {
 			new MenuBarItem ("_File", new MenuItem [] {
 			new MenuBarItem ("_File", new MenuItem [] {
-				new MenuItem ("_New", "Creates new file", () => System.Console.WriteLine ("foo")),
+				new MenuItem ("_New", "Creates new file", null),
 				new MenuItem ("_Open", "", null),
 				new MenuItem ("_Open", "", null),
 				new MenuItem ("_Close", "", null),
 				new MenuItem ("_Close", "", null),
 				new MenuItem ("_Quit", "", null)
 				new MenuItem ("_Quit", "", null)