From e995c4c5b4830b141646cdb5da0e13054d683c87 Mon Sep 17 00:00:00 2001 From: IaVashik <105387234+IaVashik@users.noreply.github.com> Date: Mon, 3 Mar 2025 11:22:01 +0300 Subject: [PATCH] Fix sizing bug in `TextEdit::singleline` (#5640) This PR reverts a change introduced in PR https://github.com/emilk/egui/pull/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 https://github.com/emilk/egui/issues/5500 * Closes https://github.com/emilk/egui/issues/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 https://github.com/emilk/egui/issues/5500 * Closes https://github.com/emilk/egui/issues/5597 * [x] I have followed the instructions in the PR template --- crates/egui/src/widgets/text_edit/builder.rs | 19 +++++++++++-------- .../tests/snapshots/demos/Code Example.png | 4 ++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index 465f5568..b3ac4cd8 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -730,14 +730,17 @@ impl TextEdit<'_> { } } - // Allocate additional space if edits were made this frame that changed the size. This is important so that, - // if there's a ScrollArea, it can properly scroll to the cursor. - let extra_size = galley.size() - rect.size(); - if extra_size.x > 0.0 || extra_size.y > 0.0 { - ui.allocate_rect( - Rect::from_min_size(outer_rect.max, extra_size), - Sense::hover(), - ); + if !clip_text { + // Allocate additional space if edits were made this frame that changed the size. This is important so that, + // if there's a ScrollArea, it can properly scroll to the cursor. + // Condition `!clip_text` is important to avoid breaking layout for `TextEdit::singleline` (PR #5640) + let extra_size = galley.size() - rect.size(); + if extra_size.x > 0.0 || extra_size.y > 0.0 { + ui.allocate_rect( + Rect::from_min_size(outer_rect.max, extra_size), + Sense::hover(), + ); + } } painter.galley(galley_pos, galley.clone(), text_color); diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Code Example.png b/crates/egui_demo_lib/tests/snapshots/demos/Code Example.png index ed731869..035f5944 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Code Example.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Code Example.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:322a50c522ba4ac67206332e1d251e121c8c3d5538ca7961880623b20f4933e5 -size 81732 +oid sha256:0466198f14d15f011e16d16efcc28aeaaf80978ea4e46b5d9a1282304c192c4c +size 80907