Skip to content

Conversation

valadaptive
Copy link
Contributor

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)

  • I have followed the instructions in the PR template

Copy link

Preview available at https://egui-pr-preview.github.io/pr/7081-clamp-cursors
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

@juancampa
Copy link
Contributor

Did you find other issues that require clamping? The reason I added the Weak<Galley> to TextArea state is to prevent clamp_cursor from running every frame, since it's O(N) to the number of rows. But maybe that's okay?

@valadaptive
Copy link
Contributor Author

It's what the previous code did, so it's probably okay. It's possible we don't need to call cursor_from_layout and layout_from_cursor and can just clamp the cursor's byte range directly; I can test that out later.

@juancampa
Copy link
Contributor

Yeah that makes sense. Would be nice to do a quick sanity check to see how much it’s getting called.

By the way, my reasoning for clamping using cursor -> layout -> cursor, instead of the byte range was to maintain the cursor position as close as possible to the old one. For example, if the string is modified by unindenting it, I’d like the cursor to stay in the same line, instead of being moved downwards to the corresponding byte offset. I don’t think I tested that particular case though.

Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@emilk emilk merged commit 54fded3 into emilk:main Jun 15, 2025
28 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants