From cb5a08cbfff4dcea90fa8cd57d4f8ecfd1fa6cba Mon Sep 17 00:00:00 2001 From: orion Date: Wed, 10 Oct 2018 13:35:40 -0400 Subject: [PATCH] Pageupdown (#138) * feat(text view pageup/down): Added code to process `PageUp` and `PageDown` keys in the `TextView`. * fix(text view pageup/down): Corrected the functionality of the `PageUp` so that it can go till row 1 --- Terminal.Gui/Views/TextView.cs | 67 +++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index be3951802..4ad8b86f4 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -6,7 +6,6 @@ // // // TODO: -// PageUp/PageDown // In ReadOnly mode backspace/space behave like pageup/pagedown // Attributed text on spans // Replace insertion with Insert method @@ -113,8 +112,9 @@ namespace Terminal.Gui { public override string ToString () { var sb = new StringBuilder (); - foreach (var line in lines) { - sb.Append (ustring.Make (line).ToString ()); + foreach (var line in lines) + { + sb.Append (ustring.Make(line)); sb.AppendLine (); } return sb.ToString (); @@ -130,7 +130,7 @@ namespace Terminal.Gui { /// /// The line. /// Line number to retrieve. - public List GetLine (int line) => lines [line]; + public List GetLine (int line) => line < Count ? lines [line]: lines[Count-1]; /// /// Adds a line to the model at the specified position. @@ -392,7 +392,12 @@ namespace Terminal.Gui { Driver.SetAttribute (ColorScheme.Normal); } - bool isReadOnly; + bool isReadOnly = false; + + /// + /// Indicates readonly attribute of TextView + /// + /// Boolean value(Default false) public bool ReadOnly { get => isReadOnly; set { @@ -494,22 +499,26 @@ namespace Terminal.Gui { int bottom = region.Bottom; int right = region.Right; - for (int row = region.Top; row < bottom; row++) { + for (int row = region.Top; row < bottom; row++) + { int textLine = topRow + row; - if (textLine >= model.Count) { + if (textLine >= model.Count) + { ColorNormal (); ClearRegion (region.Left, row, region.Right, row + 1); continue; } var line = model.GetLine (textLine); int lineRuneCount = line.Count; - if (line.Count < region.Left){ + if (line.Count < region.Left) + { ClearRegion (region.Left, row, region.Right, row + 1); continue; } Move (region.Left, row); - for (int col = region.Left; col < right; col++) { + for (int col = region.Left; col < right; col++) + { var lineCol = leftColumn + col; var rune = lineCol >= lineRuneCount ? ' ' : line [lineCol]; if (selecting && PointInSelection (col, row)) @@ -682,6 +691,38 @@ namespace Terminal.Gui { // Dispatch the command. switch (kb.Key) { + case Key.PageDown: + case Key.ControlV: + int nPageDnShift = Frame.Height - 1; + if (currentRow < model.Count) { + if (columnTrack == -1) + columnTrack = currentColumn; + currentRow = (currentRow + nPageDnShift) > model.Count ? model.Count : currentRow + nPageDnShift; + if (topRow < currentRow - nPageDnShift) { + topRow = currentRow >= model.Count ? currentRow - nPageDnShift : topRow + nPageDnShift; + SetNeedsDisplay (); + } + TrackColumn (); + PositionCursor (); + } + break; + + case Key.PageUp: + case ((int)'v' + Key.AltMask): + int nPageUpShift = Frame.Height - 1; + if (currentRow > 0) { + if (columnTrack == -1) + columnTrack = currentColumn; + currentRow = currentRow - nPageUpShift < 0 ? 0 : currentRow - nPageUpShift; + if (currentRow < topRow) { + topRow = topRow - nPageUpShift < 0 ? 0 : topRow - nPageUpShift; + SetNeedsDisplay (); + } + TrackColumn (); + PositionCursor (); + } + break; + case Key.ControlN: case Key.CursorDown: if (currentRow + 1 < model.Count) { @@ -938,11 +979,12 @@ namespace Terminal.Gui { break; default: - if (isReadOnly) - return true; // Ignore control characters and other special keys if (kb.Key < Key.Space || kb.Key > Key.CharMask) return false; + //So that special keys like tab can be processed + if (isReadOnly) + return true; Insert ((uint)kb.Key); currentColumn++; if (currentColumn >= leftColumn + Frame.Width) { @@ -1109,5 +1151,4 @@ namespace Terminal.Gui { } } -} - +} \ No newline at end of file