Skip to content

Ensure TextEdit cursor is never out of bounds #7077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

juancampa
Copy link
Contributor

@juancampa juancampa commented May 23, 2025

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)

  • I have followed the instructions in the PR template

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 can suddenly stop working

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
Copy link

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.

@juancampa juancampa marked this pull request as ready for review May 23, 2025 05:46
@valadaptive
Copy link
Contributor

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.

juancampa added a commit to membrane-io/egui that referenced this pull request May 26, 2025
)

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 can suddenly stop working

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
juancampa added a commit to membrane-io/egui that referenced this pull request May 26, 2025
)

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 can suddenly stop working

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
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.

2 participants