Skip to content

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
@emilk emilk closed this in #7081 Jun 15, 2025
emilk pushed a commit that referenced this pull request Jun 15, 2025
Closes #7077.

This fixes the problem shown in #7077 where clearing a `TextEdit`
wouldn't reset its cursor position. I've fixed that by adding back the
`TextCursorState::range` method, which clamps the selection range to
that of the passed `Galley`, and calling it in the same places where it
was called before #5785.

(/cc @juancampa)

* [x] I have followed the instructions in the PR template
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