Cursor keys switch focus when unhandled, implement scrolling with keyboard in ScrollView

This commit is contained in:
Miguel de Icaza
2018-01-29 22:21:53 -05:00
parent 1d1d50c1dd
commit 1f5515679f
4 changed files with 98 additions and 5 deletions

View File

@@ -932,6 +932,8 @@ namespace Terminal.Gui {
return true;
#endif
case Key.Tab:
case Key.CursorRight:
case Key.CursorDown:
var old = Focused;
if (!FocusNext ())
FocusNext ();
@@ -940,6 +942,8 @@ namespace Terminal.Gui {
Focused?.SetNeedsDisplay ();
}
return true;
case Key.CursorLeft:
case Key.CursorUp:
case Key.BackTab:
old = Focused;
if (!FocusPrev ())
@@ -949,6 +953,7 @@ namespace Terminal.Gui {
Focused?.SetNeedsDisplay ();
}
return true;
case Key.ControlL:
Application.Refresh ();
return true;

View File

@@ -123,14 +123,16 @@ namespace Terminal.Gui {
if (cursor > 0) {
cursor--;
SetNeedsDisplay ();
return true;
}
return true;
break;
case Key.CursorDown:
if (cursor + 1 < radioLabels.Length) {
cursor++;
SetNeedsDisplay ();
return true;
}
return true;
break;
case Key.Space:
Selected = cursor;
return true;

View File

@@ -282,7 +282,7 @@ namespace Terminal.Gui {
return contentOffset;
}
set {
contentOffset = new Point (-value.X, -value.Y);
contentOffset = new Point (-Math.Abs (value.X), -Math.Abs(value.Y));
contentView.Frame = new Rect (contentOffset, contentSize);
vertical.Position = Math.Max (0, -contentOffset.Y);
horizontal.Position = Math.Max (0, -contentOffset.X);
@@ -359,5 +359,91 @@ namespace Terminal.Gui {
else
base.PositionCursor ();
}
/// <summary>
/// Scrolls the view up.
/// </summary>
/// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
/// <param name="lines">Number of lines to scroll.</param>
public bool ScrollUp (int lines)
{
if (contentOffset.Y < 0) {
ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
return true;
}
return false;
}
/// <summary>
/// Scrolls the view to the left
/// </summary>
/// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
/// <param name="cols">Number of columns to scroll by.</param>
public bool ScrollLeft (int cols)
{
if (contentOffset.X < 0) {
ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
return true;
}
return false;
}
/// <summary>
/// Scrolls the view down.
/// </summary>
/// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
/// <param name="lines">Number of lines to scroll.</param>
public bool ScrollDown (int lines)
{
var ny = Math.Max (-contentSize.Height, contentOffset.Y - lines);
if (ny == contentOffset.Y)
return false;
ContentOffset = new Point (contentOffset.X, ny);
return true;
}
/// <summary>
/// Scrolls the view to the right.
/// </summary>
/// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
/// <param name="cols">Number of columns to scroll by.</param>
public bool ScrollRight (int cols)
{
var nx = Math.Max (-contentSize.Width, contentOffset.X - cols);
if (nx == contentOffset.X)
return false;
ContentOffset = new Point (nx, contentOffset.Y);
return true;
}
public override bool ProcessKey(KeyEvent kb)
{
if (base.ProcessKey (kb))
return true;
switch (kb.Key) {
case Key.CursorUp:
return ScrollUp (1);
case (Key) 'v' | Key.AltMask:
case Key.PageUp:
return ScrollUp (Bounds.Height);
case Key.ControlV:
case Key.PageDown:
return ScrollDown (Bounds.Height);
case Key.CursorDown:
return ScrollDown (1);
case Key.CursorLeft:
return ScrollLeft (1);
case Key.CursorRight:
return ScrollRight (1);
}
return false;
}
}
}

View File

@@ -67,7 +67,7 @@ class Demo {
{
var scrollView = new ScrollView (new Rect (50, 10, 20, 8)) {
ContentSize = new Size (100, 100),
//ContentOffset = new Point (5, -2),
ContentOffset = new Point (-1, -1),
ShowVerticalScrollIndicator = true,
ShowHorizontalScrollIndicator = true
};
@@ -93,7 +93,7 @@ class Demo {
new CheckBox (1, 0, "Remember me"),
new RadioGroup (1, 2, new [] { "_Personal", "_Company" }),
},
//scrollView,
scrollView,
//scrollView2,
new Button (3, 19, "Ok"),
new Button (10, 19, "Cancel"),