Label: correct y offset when sharing a row with another bigger widget
This commit is contained in:
parent
d137ea0443
commit
383ef94b4a
|
|
@ -55,19 +55,25 @@ 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.",
|
||||||
);
|
);
|
||||||
let tooltip_ui = |ui: &mut Ui| {
|
|
||||||
ui.heading("The name of the tooltip");
|
ui.label("You can mix in other widgets into text, like this button:");
|
||||||
ui.horizontal(|ui| {
|
let _ = ui.button("button");
|
||||||
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("There is also (limited) non-ASCII support: Ευρηκα! τ = 2×π")
|
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.");
|
.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.horizontal(|ui| {
|
||||||
ui.radio_value(&mut self.radio, Enum::First, "First");
|
ui.radio_value(&mut self.radio, Enum::First, "First");
|
||||||
ui.radio_value(&mut self.radio, Enum::Second, "Second");
|
ui.radio_value(&mut self.radio, Enum::Second, "Second");
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
let rect = row.rect().translate(vec2(pos.x, pos.y));
|
if let Some(row) = galley.rows.get(1) {
|
||||||
ui.advance_cursor_after_rect(rect);
|
// We could be sharing the first row with e.g. a button, that is higher than text.
|
||||||
let row_response = ui.interact_hover(rect);
|
// So we need to compensate for that:
|
||||||
if total_response.is_none() {
|
if pos.y + row.y_min < ui.min_rect().bottom() {
|
||||||
total_response = Some(row_response);
|
y_translation = ui.min_rect().bottom() - row.y_min - pos.y;
|
||||||
} else {
|
|
||||||
total_response = Some(total_response.unwrap().union(row_response));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
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);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue