Skip to content

Fix cursor clipping in TextEdit inside a ScrollArea#3660

Merged
emilk merged 2 commits intoemilk:masterfrom
membrane-io:ignore-unrelated-scroll-targets
Dec 2, 2024
Merged

Fix cursor clipping in TextEdit inside a ScrollArea#3660
emilk merged 2 commits intoemilk:masterfrom
membrane-io:ignore-unrelated-scroll-targets

Conversation

@juancampa
Copy link
Copy Markdown
Contributor

@juancampa juancampa commented Nov 29, 2023

Before

Notice how the cursor hides after third enter and when the line is long.

Screenshot.2024-10-13.at.04.36.56.mp4

After

Cursor is always visible

Screenshot.2024-10-13.at.04.37.52.mp4
  • ScrollArea now checks if there's a scroll_target in begin, if there is, it saves it because it's not from its children, then restore it in end.
  • TextEdit now allocates additional space if its galley grows during the frame. This is needed so that any surrounding ScrollArea can bring the cursor to view, otherwise the cursor lays outside the the ScrollArea's content_ui.

@juancampa juancampa changed the title Ignore unrelated scroll targets Ignore unrelated scroll_target values Nov 29, 2023
@juancampa juancampa marked this pull request as ready for review November 29, 2023 03:38
@juancampa juancampa changed the title Ignore unrelated scroll_target values ScrollArea now ignores unrelated scroll_target values Nov 29, 2023
@juancampa
Copy link
Copy Markdown
Contributor Author

juancampa commented Nov 29, 2023

The reason for this fix btw is because the temporary workaround #153 (cc @Barugon) has a problem. Notice that on the fourth Enter press (00:03), the scrollarea doesn't follow the cursor. That's because the ScrollArea doesn't set up a clip rect (because contents are small enough) and the text area doesn't know it's been clipped.

Screen.Recording.2023-11-29.at.10.52.31.AM.mov

@juancampa juancampa force-pushed the ignore-unrelated-scroll-targets branch from ff4b722 to d7bd15d Compare October 13, 2024 08:41
@juancampa juancampa changed the title ScrollArea now ignores unrelated scroll_target values Fix cursor clipping in TextEdit inside a ScrollArea Oct 13, 2024
@juancampa
Copy link
Copy Markdown
Contributor Author

@emilk I just reimplemented this on top of the latest master. Also posted two videos in the description showing the issue.

@juancampa juancampa requested a review from emilk October 13, 2024 09:09
@github-actions
Copy link
Copy Markdown

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

Copy link
Copy Markdown
Collaborator

@lucasmerlin lucasmerlin left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Comment on lines +503 to +505
/// If there was a scroll target before the ScrollArea was added this frame, it's
/// not for us to handle so we save it and restore it after this ScrollArea is done.
saved_scroll_target: [Option<pass_state::ScrollTarget>; 2],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There is also FrameState::scroll_delta, I think we should do the same with that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@lucasmerlin There's no FrameState anymore so I think this comment is not valid anymore?

@lucasmerlin lucasmerlin added bug Something is broken egui labels Oct 14, 2024
Copy link
Copy Markdown
Contributor Author

@juancampa juancampa left a comment

Choose a reason for hiding this comment

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

@emilk this should be ready to merge unless there's more feedback.

@emilk emilk merged commit 4f7f23e into emilk:master Dec 2, 2024
@emilk
Copy link
Copy Markdown
Owner

emilk commented Dec 2, 2024

thanks!

@LaVashikk
Copy link
Copy Markdown
Contributor

This change breaks TextEdit::singleline when it's used outside of a ScrollArea. The added ui.allocate_rect call causes incorrect horizontal expansion because it allocates extra space based on the formatted text size, which is not appropriate for single-line text edits. This logic should be implemented in ScrollArea instead of TextEdit. ScrollArea should handle the cursor visibility, as it already manages its content's position and size. Imo, TextEdit shouldn't have to alter its size for ScrollArea to work correctly.

@LaVashikk
Copy link
Copy Markdown
Contributor

Also, I haven't been able to reproduce this bug in the current version, but only when I remove the code from lines 726-734 in builder.rs. Do we actually need this code at all?

lucasmerlin pushed a commit that referenced this pull request Mar 3, 2025
This PR reverts a change introduced in PR
#3660 that caused a regression with
`TextEdit::singleline`. The original PR attempted to fix an issue with
the cursor in `TextEdit` inside `ScrollArea`, but it did so by adding
unnecessary size allocation to `TextEdit`, which breaks the layout when
`TextEdit::singleline` is used outside of `ScrollArea`.


![Image](https://github.com/user-attachments/assets/78fdf20a-0763-4b5f-b83b-64522f15b35b)

The regression introduced by #3660 is more severe, as it completely
breaks the layout of applications using `TextEdit::singleline`, as shown
in the following issues:

*   Closes #5500
*   Closes #5597

Furthermore, I was unable to reproduce the original bug from PR #3660 in
the current version of egui using the following code:

```rust
impl eframe::App for MyEguiApp {
    fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) {
        ctx.set_debug_on_hover(true);
        egui::CentralPanel::default().show(ctx, |ui| {
            ScrollArea::vertical().max_height(100.0).show(ui, |ui| {
                ui.add(TextEdit::multiline(&mut self.text).hint_text("Enter text here..."))
            });
        });
    }
}
```

This code attempts to recreate the layout shown in the video from PR
#3660, using a `ScrollArea` with limited height and a `TextEdit` inside.
However, the cursor hiding issue was not reproducible.

![Video_2025-01-26_17-54-24](https://github.com/user-attachments/assets/ca4750ea-8af8-4ab5-8c10-bdf73a090362)

Therefore, I believe the code added in PR #3660 is no longer necessary
and only creates more problems.

*   Closes #5500
*   Closes #5597
*   [x] I have followed the instructions in the PR template
lucasmerlin pushed a commit that referenced this pull request Mar 3, 2025
This PR reverts a change introduced in PR
#3660 that caused a regression with
`TextEdit::singleline`. The original PR attempted to fix an issue with
the cursor in `TextEdit` inside `ScrollArea`, but it did so by adding
unnecessary size allocation to `TextEdit`, which breaks the layout when
`TextEdit::singleline` is used outside of `ScrollArea`.

![Image](https://github.com/user-attachments/assets/78fdf20a-0763-4b5f-b83b-64522f15b35b)

The regression introduced by #3660 is more severe, as it completely
breaks the layout of applications using `TextEdit::singleline`, as shown
in the following issues:

*   Closes #5500
*   Closes #5597

Furthermore, I was unable to reproduce the original bug from PR #3660 in
the current version of egui using the following code:

```rust
impl eframe::App for MyEguiApp {
    fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) {
        ctx.set_debug_on_hover(true);
        egui::CentralPanel::default().show(ctx, |ui| {
            ScrollArea::vertical().max_height(100.0).show(ui, |ui| {
                ui.add(TextEdit::multiline(&mut self.text).hint_text("Enter text here..."))
            });
        });
    }
}
```

This code attempts to recreate the layout shown in the video from PR
#3660, using a `ScrollArea` with limited height and a `TextEdit` inside.
However, the cursor hiding issue was not reproducible.

![Video_2025-01-26_17-54-24](https://github.com/user-attachments/assets/ca4750ea-8af8-4ab5-8c10-bdf73a090362)

Therefore, I believe the code added in PR #3660 is no longer necessary
and only creates more problems.

*   Closes #5500
*   Closes #5597
*   [x] I have followed the instructions in the PR template

(cherry picked from commit e995c4c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something is broken egui

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ScrollArea scroll state modified by sibling textedit

4 participants