Label: correct y offset when sharing a row with another bigger widget

This commit is contained in:
Emil Ernerfeldt 2020-12-09 21:32:28 +01:00
parent d137ea0443
commit 383ef94b4a
2 changed files with 35 additions and 20 deletions

View File

@ -55,6 +55,14 @@ impl Widgets {
ui.add(Label::new("and tooltips.")).on_hover_text( 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.", "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.",
); );
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| { let tooltip_ui = |ui: &mut Ui| {
ui.heading("The name of the tooltip"); ui.heading("The name of the tooltip");
ui.horizontal(|ui| { ui.horizontal(|ui| {
@ -63,10 +71,8 @@ impl Widgets {
}); });
let _ = ui.button("A button you can never press"); 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("Tooltips can be more than just simple text.")
ui.label("There is also (limited) non-ASCII support: Ευρηκα! τ = 2×π") .on_hover_ui(tooltip_ui);
.on_hover_text("The current font supports only a few non-latin characters and Egui does not currently support right-to-left text.");
});
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.radio_value(&mut self.radio, Enum::First, "First"); ui.radio_value(&mut self.radio, Enum::First, "First");

View File

@ -134,7 +134,7 @@ impl Widget for Label {
let text_style = self.text_style_or_default(ui.style()); let text_style = self.text_style_or_default(ui.style());
let font = &ui.fonts()[text_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(), self.text.clone(),
first_row_indentation, first_row_indentation,
max_width, max_width,
@ -142,21 +142,30 @@ impl Widget for Label {
let pos = pos2(ui.min_rect().left(), ui.cursor().y); 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 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)); let rect = row.rect().translate(vec2(pos.x, pos.y));
ui.advance_cursor_after_rect(rect); ui.advance_cursor_after_rect(rect);
let row_response = ui.interact_hover(rect); total_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));
}
} }
self.paint_galley(ui, pos, galley); self.paint_galley(ui, pos, galley);
total_response.expect("Galley rows shouldn't be empty") total_response
} else { } else {
let galley = self.layout(ui); let galley = self.layout(ui);
let rect = ui.allocate_space(galley.size); let rect = ui.allocate_space(galley.size);