Skip to content

Commit

Permalink
Improving BottomOffset and RightOffset by not relying on CurrentRow a…
Browse files Browse the repository at this point in the history
…nd CurrentColumn.
  • Loading branch information
BDisp committed Jul 25, 2023
1 parent c642562 commit 5c370f5
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions Terminal.Gui/Views/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,11 +1654,8 @@ public bool WordWrap {
public int BottomOffset {
get => bottomOffset;
set {
if (currentRow == Lines - 1 && bottomOffset > 0 && value == 0) {
topRow = Math.Max (topRow - bottomOffset, 0);
}
topRow = AdjustOffset (value);
bottomOffset = value;
Adjust ();
}
}

Expand All @@ -1669,11 +1666,8 @@ public int BottomOffset {
public int RightOffset {
get => rightOffset;
set {
if (!wordWrap && currentColumn == GetCurrentLine ().Count && rightOffset > 0 && value == 0) {
leftColumn = Math.Max (leftColumn - rightOffset, 0);
}
leftColumn = AdjustOffset (value, false);
rightOffset = value;
Adjust ();
}
}

Expand Down Expand Up @@ -2710,6 +2704,30 @@ void Adjust ()
OnUnwrappedCursorPosition ();
}

int AdjustOffset (int valueOffset, bool isRow = true)
{
var curWrap = isRow ? false : wordWrap;
var curLength = isRow ? Lines - 1 : GetCurrentLine ().Count;
var curStart = isRow ? topRow : leftColumn;
var curOffset = isRow ? bottomOffset : rightOffset;
var curSize = isRow ? Frame.Height - valueOffset : Frame.Width - valueOffset;
var newStart = curStart;

if (!curWrap) {
if (curStart > 0 && curOffset > 0 && valueOffset == 0) {
newStart = Math.Max (curStart - curOffset, 0);
} else if (curOffset == 0 && valueOffset > 0) {
newStart = Math.Max (Math.Min (curStart + valueOffset, curLength - curSize + 1), 0);
}

if (newStart != curStart) {
Application.MainLoop.Invoke (() => SetNeedsDisplay ());
}
}

return newStart;
}

/// <summary>
/// Event arguments for events for when the contents of the TextView change. E.g. the <see cref="ContentsChanged"/> event.
/// </summary>
Expand Down

0 comments on commit 5c370f5

Please sign in to comment.