Make `on_disabled_hover_ui` respect `tooltip_delay` (#4012)

* Closes <https://github.com/emilk/egui/issues/3997>
This commit is contained in:
YgorSouza 2024-02-10 09:07:21 +01:00 committed by GitHub
parent 15370bbea0
commit 1bc70b20b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 9 deletions

View File

@ -438,7 +438,7 @@ impl Response {
/// If you call this multiple times the tooltips will stack underneath the previous ones.
#[doc(alias = "tooltip")]
pub fn on_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
if self.should_show_hover_ui() {
if self.enabled && self.should_show_hover_ui() {
crate::containers::show_tooltip_for(
&self.ctx,
self.id.with("__tooltip"),
@ -451,7 +451,7 @@ impl Response {
/// Show this UI when hovering if the widget is disabled.
pub fn on_disabled_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
if !self.enabled && self.ctx.rect_contains_pointer(self.layer_id, self.rect) {
if !self.enabled && self.should_show_hover_ui() {
crate::containers::show_tooltip_for(
&self.ctx,
self.id.with("__tooltip"),
@ -464,7 +464,7 @@ impl Response {
/// Like `on_hover_ui`, but show the ui next to cursor.
pub fn on_hover_ui_at_pointer(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
if self.should_show_hover_ui() {
if self.enabled && self.should_show_hover_ui() {
crate::containers::show_tooltip_at_pointer(
&self.ctx,
self.id.with("__tooltip"),
@ -484,7 +484,11 @@ impl Response {
return true;
}
if !self.hovered || !self.ctx.input(|i| i.pointer.has_pointer()) {
if self.enabled {
if !self.hovered || !self.ctx.input(|i| i.pointer.has_pointer()) {
return false;
}
} else if !self.ctx.rect_contains_pointer(self.layer_id, self.rect) {
return false;
}
@ -505,8 +509,9 @@ impl Response {
if 0.0 < time_til_tooltip {
// Wait until the mouse has been still for a while
self.ctx
.request_repaint_after(std::time::Duration::from_secs_f32(time_til_tooltip));
if let Ok(duration) = std::time::Duration::try_from_secs_f32(time_til_tooltip) {
self.ctx.request_repaint_after(duration);
}
return false;
}
}

View File

@ -233,6 +233,7 @@ fn label_ui(ui: &mut egui::Ui) {
#[cfg_attr(feature = "serde", serde(default))]
pub struct Widgets {
angle: f32,
enabled: bool,
password: String,
}
@ -240,6 +241,7 @@ impl Default for Widgets {
fn default() -> Self {
Self {
angle: std::f32::consts::TAU / 3.0,
enabled: true,
password: "hunter2".to_owned(),
}
}
@ -247,7 +249,11 @@ impl Default for Widgets {
impl Widgets {
pub fn ui(&mut self, ui: &mut Ui) {
let Self { angle, password } = self;
let Self {
angle,
enabled,
password,
} = self;
ui.vertical_centered(|ui| {
ui.add(crate::egui_github_link_file_line!());
});
@ -260,8 +266,20 @@ impl Widgets {
});
let _ = ui.button("A button you can never press");
};
ui.label("Tooltips can be more than just simple text.")
.on_hover_ui(tooltip_ui);
let disabled_tooltip_ui = |ui: &mut Ui| {
ui.heading("Different tooltip when widget is disabled");
ui.horizontal(|ui| {
ui.label("This tooltip was created with");
ui.monospace(".on_disabled_hover_ui(…)");
});
};
ui.checkbox(enabled, "Enabled");
ui.add_enabled(
*enabled,
egui::Label::new("Tooltips can be more than just simple text."),
)
.on_hover_ui(tooltip_ui)
.on_disabled_hover_ui(disabled_tooltip_ui);
ui.separator();