From 2078eaab697184a377899a94d9c70e1274f29cd1 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 25 Sep 2024 18:39:36 +0200 Subject: [PATCH] Fix bug causing tooltips with dynamic content to shrink MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Affects `.on_hover_text(…)` with dynamic content (i.e. content that changes over time). * Closes https://github.com/emilk/egui/issues/5167 --- crates/egui/src/response.rs | 12 ++++++++++++ tests/test_size_pass/src/main.rs | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/crates/egui/src/response.rs b/crates/egui/src/response.rs index 1378946bbfc..6940a587589 100644 --- a/crates/egui/src/response.rs +++ b/crates/egui/src/response.rs @@ -790,6 +790,10 @@ impl Response { #[doc(alias = "tooltip")] pub fn on_hover_text_at_pointer(self, text: impl Into) -> Self { self.on_hover_ui_at_pointer(|ui| { + // Prevent `Area` auto-sizing from shrinking tooltips with dynamic content. + // See https://github.com/emilk/egui/issues/5167 + ui.set_max_width(ui.spacing().tooltip_width); + ui.add(crate::widgets::Label::new(text)); }) } @@ -803,6 +807,10 @@ impl Response { #[doc(alias = "tooltip")] pub fn on_hover_text(self, text: impl Into) -> Self { self.on_hover_ui(|ui| { + // Prevent `Area` auto-sizing from shrinking tooltips with dynamic content. + // See https://github.com/emilk/egui/issues/5167 + ui.set_max_width(ui.spacing().tooltip_width); + ui.add(crate::widgets::Label::new(text)); }) } @@ -822,6 +830,10 @@ impl Response { /// Show this text when hovering if the widget is disabled. pub fn on_disabled_hover_text(self, text: impl Into) -> Self { self.on_disabled_hover_ui(|ui| { + // Prevent `Area` auto-sizing from shrinking tooltips with dynamic content. + // See https://github.com/emilk/egui/issues/5167 + ui.set_max_width(ui.spacing().tooltip_width); + ui.add(crate::widgets::Label::new(text)); }) } diff --git a/tests/test_size_pass/src/main.rs b/tests/test_size_pass/src/main.rs index daecf7758fa..6bb29330271 100644 --- a/tests/test_size_pass/src/main.rs +++ b/tests/test_size_pass/src/main.rs @@ -103,6 +103,12 @@ fn main() -> eframe::Result { ui.label("World"); ui.label("Hellooooooooooooooooooooooooo"); }); + + ui.separator(); + + let time = ui.input(|i| i.time); + ui.label("Hover for a tooltip with changing content") + .on_hover_text(format!("A number: {}", time % 10.0)); }); }) }