Skip to content

Fix TextBoxWidget cursor drifting left when deleting overflowed single-line text#29

Open
cytoph wants to merge 1 commit into
spectreconsole:mainfrom
cytoph:fix/textbox-cursor-pinned-on-delete
Open

Fix TextBoxWidget cursor drifting left when deleting overflowed single-line text#29
cytoph wants to merge 1 commit into
spectreconsole:mainfrom
cytoph:fix/textbox-cursor-pinned-on-delete

Conversation

@cytoph

@cytoph cytoph commented May 29, 2026

Copy link
Copy Markdown

Summary

When a single-line TextBoxWidget contains text that overflows the viewport and the cursor is at the end, pressing Backspace caused the cursor to visually drift left rather than staying pinned at the right edge. After several deletions the cursor ends up in the middle of the visible area, making it impossible to tell whether content is still hidden to the left.

Root cause: _horizontalOffset is only adjusted in RenderSingleLine when the cursor moves past a viewport boundary. When deletion moves the cursor one cell inward from the right edge, neither boundary condition fires, so the offset stays stale and the cursor appears one cell to the left.

Fix

Added a third condition in RenderSingleLine: when the cursor is at the end of the line and _horizontalOffset is larger than needed to keep the cursor at the right edge, clamp it down.

// When the cursor is at the end of the text and content is still scrolled off to the
// left, keep the cursor pinned to the right edge rather than letting it drift left as
// characters are deleted.
if (_cursorColumn == line.Count && _horizontalOffset > cursorCellPos - width + 1)
{
    _horizontalOffset = Math.Max(0, cursorCellPos - width + 1);
}

Test

Added Should_Keep_Cursor_Pinned_At_Right_Edge_When_Deleting_From_Overflowed_Text to TextBoxWidgetTests.TheRenderMethod. It renders overflowed text once to establish the scroll offset, deletes one character, renders again, and asserts the cursor is still at the right edge.

All 334 existing tests continue to pass.

@cytoph

cytoph commented May 29, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@cytoph cytoph force-pushed the fix/textbox-cursor-pinned-on-delete branch from fef83bd to 1b01cb7 Compare May 29, 2026 21:05
…single-line input

When text overflows the viewport and the cursor is at the end, backspacing
caused the cursor to drift left instead of staying pinned at the right edge.

Added a rule in RenderSingleLine: when the cursor is at the end of the line
and _horizontalOffset is greater than what's needed to keep the cursor at
the right edge, clamp it down so the cursor stays pinned there.

Added a regression test covering the delete-from-overflow case.
@cytoph cytoph force-pushed the fix/textbox-cursor-pinned-on-delete branch from 1b01cb7 to 6a74c19 Compare May 29, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant