egui icon indicating copy to clipboard operation
egui copied to clipboard

Ensure TextEdit cursor is never out of bounds

Open juancampa opened this issue 7 months ago • 2 comments

When rendering a TextArea we don't know if the saved cursor applies to the current galley since it's possible the app changed the TextBuffer (e.g. when submitting a chat input)

So we now detect if the galley changed from the last known one and clamp the cursor to ensure it's not out of bounds.

This fixes an issue where backspace and arrow keys can suddenly stop working. In this video I changed the TextArea to not lose focus when enter is pressed (using .return_key). And instead, the app clears the String. Which is what we do in our chat and REPL UIs.

Screenshot 2025-05-23 at 01 36 14

Repro:

  • Render TextArea with long-ish text (say 20 chars)
  • Without losing focus, clear the text
  • Write something short (say 5 chars)
  • Result: backspace doesn't work because the cursor position is wrong

I think this issue started happening when this function was removed in #5785

(cc @valadaptive in case you happen to know of a better fix that doesn't require keeping the Galley in the TextEdit state)

  • [x] I have followed the instructions in the PR template

juancampa avatar May 23 '25 05:05 juancampa

Preview available at https://egui-pr-preview.github.io/pr/7077-fix-out-of-bounds-cursor Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

github-actions[bot] avatar May 23 '25 05:05 github-actions[bot]

The removed range function took the galley as an argument, whereas its replacement (char_range) does not. Is it possible to make char_range take a Galley and do the clamping there, and just pass in the galley in the same paths that the old code did?

It's been a while since I made those changes so I could be totally off-base.

valadaptive avatar May 23 '25 05:05 valadaptive