From 383ef94b4a38c8a03f05a0b52852a0eccc138da1 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 9 Dec 2020 21:32:28 +0100 Subject: [PATCH] Label: correct y offset when sharing a row with another bigger widget --- egui/src/demos/widgets.rs | 24 +++++++++++++++--------- egui/src/widgets/mod.rs | 31 ++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/egui/src/demos/widgets.rs b/egui/src/demos/widgets.rs index fb179db3..0a3be9d3 100644 --- a/egui/src/demos/widgets.rs +++ b/egui/src/demos/widgets.rs @@ -55,19 +55,25 @@ impl Widgets { ui.add(Label::new("and tooltips.")).on_hover_text( "This is a multiline tooltip that demonstrates that you can easily add tooltips to any element.\nThis is the second line.\nThis is the third.", ); - let tooltip_ui = |ui: &mut Ui| { - ui.heading("The name of the tooltip"); - ui.horizontal(|ui| { - ui.label("This tooltip was created with"); - ui.monospace(".on_hover_ui(...)"); - }); - let _ = ui.button("A button you can never press"); - }; - ui.label("Tooltips can be more than just simple text.").on_hover_ui(tooltip_ui); + + ui.label("You can mix in other widgets into text, like this button:"); + let _ = ui.button("button"); + ui.label("There is also (limited) non-ASCII support: Ευρηκα! τ = 2×π") .on_hover_text("The current font supports only a few non-latin characters and Egui does not currently support right-to-left text."); }); + let tooltip_ui = |ui: &mut Ui| { + ui.heading("The name of the tooltip"); + ui.horizontal(|ui| { + ui.label("This tooltip was created with"); + ui.monospace(".on_hover_ui(...)"); + }); + let _ = ui.button("A button you can never press"); + }; + ui.label("Tooltips can be more than just simple text.") + .on_hover_ui(tooltip_ui); + ui.horizontal(|ui| { ui.radio_value(&mut self.radio, Enum::First, "First"); ui.radio_value(&mut self.radio, Enum::Second, "Second"); diff --git a/egui/src/widgets/mod.rs b/egui/src/widgets/mod.rs index c6383f29..fdd8f166 100644 --- a/egui/src/widgets/mod.rs +++ b/egui/src/widgets/mod.rs @@ -134,7 +134,7 @@ impl Widget for Label { let text_style = self.text_style_or_default(ui.style()); let font = &ui.fonts()[text_style]; - let galley = font.layout_multiline_with_indentation_and_max_width( + let mut galley = font.layout_multiline_with_indentation_and_max_width( self.text.clone(), first_row_indentation, max_width, @@ -142,21 +142,30 @@ impl Widget for Label { let pos = pos2(ui.min_rect().left(), ui.cursor().y); - let mut total_response = None; + assert!(!galley.rows.is_empty(), "Gallyes are never empty"); + let rect = galley.rows[0].rect().translate(vec2(pos.x, pos.y)); + ui.advance_cursor_after_rect(rect); + let mut total_response = ui.interact_hover(rect); - for row in &galley.rows { - let rect = row.rect().translate(vec2(pos.x, pos.y)); - ui.advance_cursor_after_rect(rect); - let row_response = ui.interact_hover(rect); - if total_response.is_none() { - total_response = Some(row_response); - } else { - total_response = Some(total_response.unwrap().union(row_response)); + let mut y_translation = 0.0; + if let Some(row) = galley.rows.get(1) { + // We could be sharing the first row with e.g. a button, that is higher than text. + // So we need to compensate for that: + if pos.y + row.y_min < ui.min_rect().bottom() { + y_translation = ui.min_rect().bottom() - row.y_min - pos.y; } } + for row in galley.rows.iter_mut().skip(1) { + row.y_min += y_translation; + row.y_max += y_translation; + let rect = row.rect().translate(vec2(pos.x, pos.y)); + ui.advance_cursor_after_rect(rect); + total_response |= ui.interact_hover(rect); + } + self.paint_galley(ui, pos, galley); - total_response.expect("Galley rows shouldn't be empty") + total_response } else { let galley = self.layout(ui); let rect = ui.allocate_space(galley.size);