From 1f5515679f6dde6c7ee90aad4dc790be533f89c7 Mon Sep 17 00:00:00 2001 From: Miguel de Icaza Date: Mon, 29 Jan 2018 22:21:53 -0500 Subject: [PATCH] Cursor keys switch focus when unhandled, implement scrolling with keyboard in ScrollView --- Terminal.Gui/Core.cs | 5 ++ Terminal.Gui/Views/RadioGroup.cs | 6 ++- Terminal.Gui/Views/ScrollView.cs | 88 +++++++++++++++++++++++++++++++- demo.cs | 4 +- 4 files changed, 98 insertions(+), 5 deletions(-) diff --git a/Terminal.Gui/Core.cs b/Terminal.Gui/Core.cs index 2edfac654..79dc308c4 100644 --- a/Terminal.Gui/Core.cs +++ b/Terminal.Gui/Core.cs @@ -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; diff --git a/Terminal.Gui/Views/RadioGroup.cs b/Terminal.Gui/Views/RadioGroup.cs index 5f07709cb..bb4a2be1d 100644 --- a/Terminal.Gui/Views/RadioGroup.cs +++ b/Terminal.Gui/Views/RadioGroup.cs @@ -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; diff --git a/Terminal.Gui/Views/ScrollView.cs b/Terminal.Gui/Views/ScrollView.cs index 411032bf5..dd76c083e 100644 --- a/Terminal.Gui/Views/ScrollView.cs +++ b/Terminal.Gui/Views/ScrollView.cs @@ -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 (); } + + /// + /// Scrolls the view up. + /// + /// true, if left was scrolled, false otherwise. + /// Number of lines to scroll. + public bool ScrollUp (int lines) + { + if (contentOffset.Y < 0) { + ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0)); + return true; + } + return false; + } + + /// + /// Scrolls the view to the left + /// + /// true, if left was scrolled, false otherwise. + /// Number of columns to scroll by. + public bool ScrollLeft (int cols) + { + if (contentOffset.X < 0) { + ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y); + return true; + } + return false; + } + + /// + /// Scrolls the view down. + /// + /// true, if left was scrolled, false otherwise. + /// Number of lines to scroll. + 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; + } + + /// + /// Scrolls the view to the right. + /// + /// true, if right was scrolled, false otherwise. + /// Number of columns to scroll by. + 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; + } } } diff --git a/demo.cs b/demo.cs index 6106e72bf..13f9fae94 100644 --- a/demo.cs +++ b/demo.cs @@ -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"),