Remove magic from Toplevel for event processing into its own method

This commit is contained in:
Miguel de Icaza
2018-01-06 22:16:22 -05:00
parent 01848bc5ee
commit 6666d19202
4 changed files with 31 additions and 21 deletions

30
Core.cs
View File

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

View File

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

View File

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

View File

@@ -33,7 +33,7 @@ class Demo {
var win = new Window (new Rect (0, 1, tframe.Width, tframe.Height-1), "Hello");
var menu = new MenuBar (new MenuBarItem [] {
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 ("_Close", "", null),
new MenuItem ("_Quit", "", null)