Skip to content

Commit

Permalink
Make on_disabled_hover_ui respect tooltip_delay (#4012)
Browse files Browse the repository at this point in the history
* Closes <#3997>
  • Loading branch information
YgorSouza authored Feb 10, 2024
1 parent 15370bb commit 1bc70b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
17 changes: 11 additions & 6 deletions crates/egui/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -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;
}

Expand All @@ -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;
}
}
Expand Down
24 changes: 21 additions & 3 deletions crates/egui_demo_lib/src/demo/misc_demo_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,27 @@ fn label_ui(ui: &mut egui::Ui) {
#[cfg_attr(feature = "serde", serde(default))]
pub struct Widgets {
angle: f32,
enabled: bool,
password: String,
}

impl Default for Widgets {
fn default() -> Self {
Self {
angle: std::f32::consts::TAU / 3.0,
enabled: true,
password: "hunter2".to_owned(),
}
}
}

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!());
});
Expand All @@ -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();

Expand Down

0 comments on commit 1bc70b2

Please sign in to comment.