add Label::show_tooltip_when_elided (#5710)

fixes #5708

Allows the user to disable the automatic tooltip when a Label is elided

* Closes <https://github.com/emilk/egui/issues/5708>
* [x] I have followed the instructions in the PR template
This commit is contained in:
Bryce Berger 2025-02-18 11:33:27 -05:00 committed by GitHub
parent 770c976ed7
commit 071e090e2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 1 deletions

View File

@ -30,6 +30,7 @@ pub struct Label {
sense: Option<Sense>,
selectable: Option<bool>,
halign: Option<Align>,
show_tooltip_when_elided: bool,
}
impl Label {
@ -40,6 +41,7 @@ impl Label {
sense: None,
selectable: None,
halign: None,
show_tooltip_when_elided: true,
}
}
@ -116,6 +118,23 @@ impl Label {
self.sense = Some(sense);
self
}
/// Show the full text when hovered, if the text was elided.
///
/// By default, this is true.
///
/// ```
/// # use egui::{Label, Sense};
/// # egui::__run_test_ui(|ui| {
/// ui.add(Label::new("some text").show_tooltip_when_elided(false))
/// .on_hover_text("completely different text");
/// # });
/// ```
#[inline]
pub fn show_tooltip_when_elided(mut self, show: bool) -> Self {
self.show_tooltip_when_elided = show;
self
}
}
impl Label {
@ -247,13 +266,14 @@ impl Widget for Label {
let interactive = self.sense.is_some_and(|sense| sense != Sense::hover());
let selectable = self.selectable;
let show_tooltip_when_elided = self.show_tooltip_when_elided;
let (galley_pos, galley, mut response) = self.layout_in_ui(ui);
response
.widget_info(|| WidgetInfo::labeled(WidgetType::Label, ui.is_enabled(), galley.text()));
if ui.is_rect_visible(response.rect) {
if galley.elided {
if show_tooltip_when_elided && galley.elided {
// Show the full (non-elided) text on hover:
response = response.on_hover_text(galley.text());
}