Skip to content

fix: place text field caret at end of preloaded text#51

Open
mikenye wants to merge 3 commits intoebitengine:mainfrom
mikenye:fix-textfield-caret-start-position
Open

fix: place text field caret at end of preloaded text#51
mikenye wants to merge 3 commits intoebitengine:mainfrom
mikenye:fix-textfield-caret-start-position

Conversation

@mikenye
Copy link
Copy Markdown
Contributor

@mikenye mikenye commented Apr 11, 2026

Fixes a TextField bug where typing into a field with an existing value could insert new characters at the start of the string instead of at the end. Fixes #50.

Problem

debugui caches textinput.Field instances per widget ID. When a TextField was reopened or reused with preloaded text, the cached field could keep an old selection/insertion point from a previous editing session.

That meant the visible text was correct, but the next typed character could still be inserted at byte 0.

A related issue existed in SetTextFieldValue, which updated the field text but reset the selection to 0,0, leaving the next insertion at the start of the string.

Fix

This change:

  • resets the selection to the end of the current value whenever an unfocused text field becomes focused
  • does this even when the text value already matches, because the cached field may still have stale caret state
  • updates SetTextFieldValue to place the selection at the end of the new value
  • reuses the same helper for other programmatic text updates in the text field code

Tests

Adds regression tests covering:

  • focusing a field with preloaded text
  • focusing a reused field where the text is unchanged but the cached selection is stale
  • SetTextFieldValue moving the caret to the end of the new text

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.

TextField inserts new characters at the start when initialized with existing text

1 participant