diff --git a/crates/egui/src/response.rs b/crates/egui/src/response.rs index 2b55a9d5..76cfb824 100644 --- a/crates/egui/src/response.rs +++ b/crates/egui/src/response.rs @@ -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; } } diff --git a/crates/egui_demo_lib/src/demo/misc_demo_window.rs b/crates/egui_demo_lib/src/demo/misc_demo_window.rs index f112d718..b4b28511 100644 --- a/crates/egui_demo_lib/src/demo/misc_demo_window.rs +++ b/crates/egui_demo_lib/src/demo/misc_demo_window.rs @@ -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();