Add `Response::show_tooltip_ui` and `show_tooltip_text` (#4580)

These functions will always show a tooltip under the widget when called,
even if the user is not hovering the widget.

This can be useful for tutorials and notification and similar.

* Closes https://github.com/emilk/egui/issues/890
This commit is contained in:
Emil Ernerfeldt 2024-05-29 21:28:56 +02:00 committed by GitHub
parent 00396145d1
commit 16277ebb86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 6 deletions

View File

@ -523,12 +523,7 @@ impl Response {
#[doc(alias = "tooltip")]
pub fn on_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
if self.enabled && self.should_show_hover_ui() {
crate::containers::show_tooltip_for(
&self.ctx,
self.id.with("__tooltip"),
&self.rect,
add_contents,
);
self.show_tooltip_ui(add_contents);
}
self
}
@ -558,6 +553,27 @@ impl Response {
self
}
/// Always show this tooltip, even if disabled and the user isn't hovering it.
///
/// This can be used to give attention to a widget during a tutorial.
pub fn show_tooltip_ui(&self, add_contents: impl FnOnce(&mut Ui)) {
crate::containers::show_tooltip_for(
&self.ctx,
self.id.with("__tooltip"),
&self.rect,
add_contents,
);
}
/// Always show this tooltip, even if disabled and the user isn't hovering it.
///
/// This can be used to give attention to a widget during a tutorial.
pub fn show_tooltip_text(&self, text: impl Into<WidgetText>) {
self.show_tooltip_ui(|ui| {
ui.label(text);
});
}
/// Was the tooltip open last frame?
pub fn is_tooltip_open(&self) -> bool {
crate::popup::was_tooltip_open_last_frame(&self.ctx, self.id.with("__tooltip"))