Skip to content

Commit

Permalink
More robust handling of tooltip_grace_time
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jul 3, 2024
1 parent 84adbc5 commit 525bbdb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
25 changes: 25 additions & 0 deletions crates/egui/src/containers/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ use crate::*;

// ----------------------------------------------------------------------------

fn when_was_a_toolip_last_shown_id() -> Id {
Id::new("when_was_a_toolip_last_shown")
}

pub fn seconds_since_last_tooltip(ctx: &Context) -> f32 {
let when_was_a_toolip_last_shown =
ctx.data(|d| d.get_temp::<f64>(when_was_a_toolip_last_shown_id()));

if let Some(when_was_a_toolip_last_shown) = when_was_a_toolip_last_shown {
let now = ctx.input(|i| i.time);
(now - when_was_a_toolip_last_shown) as f32
} else {
f32::INFINITY
}
}

fn remember_that_tooltip_was_shown(ctx: &Context) {
let now = ctx.input(|i| i.time);
ctx.data_mut(|data| data.insert_temp::<f64>(when_was_a_toolip_last_shown_id(), now));
}

// ----------------------------------------------------------------------------

/// Show a tooltip at the current pointer position (if any).
///
/// Most of the time it is easier to use [`Response::on_hover_ui`].
Expand Down Expand Up @@ -123,6 +146,8 @@ fn show_tooltip_at_dyn<'c, R>(
widget_rect = transform * widget_rect;
}

remember_that_tooltip_was_shown(ctx);

let mut state = ctx.frame_state_mut(|fs| {
// Remember that this is the widget showing the tooltip:
fs.layers
Expand Down
21 changes: 5 additions & 16 deletions crates/egui/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,9 @@ impl Response {
return false;
}

let is_tooltip_open = self.is_tooltip_open();
let is_our_tooltip_open = self.is_tooltip_open();

if is_tooltip_open {
if is_our_tooltip_open {
let (pointer_pos, pointer_dir) = self
.ctx
.input(|i| (i.pointer.hover_pos(), i.pointer.direction()));
Expand Down Expand Up @@ -680,13 +680,6 @@ impl Response {
return false;
}

let when_was_a_toolip_last_shown_id = Id::new("when_was_a_toolip_last_shown");
let now = self.ctx.input(|i| i.time);

let when_was_a_toolip_last_shown = self
.ctx
.data(|d| d.get_temp::<f64>(when_was_a_toolip_last_shown_id));

let tooltip_delay = self.ctx.style().interaction.tooltip_delay;
let tooltip_grace_time = self.ctx.style().interaction.tooltip_grace_time;

Expand All @@ -695,10 +688,10 @@ impl Response {
// another widget should show the tooltip for that widget right away.

// Let the user quickly move over some dead space to hover the next thing
let tooltip_was_recently_shown = when_was_a_toolip_last_shown
.map_or(false, |time| ((now - time) as f32) < tooltip_grace_time);
let tooltip_was_recently_shown =
crate::popup::seconds_since_last_tooltip(&self.ctx) < tooltip_grace_time;

if !tooltip_was_recently_shown && !is_tooltip_open {
if !tooltip_was_recently_shown && !is_our_tooltip_open {
if self.ctx.style().interaction.show_tooltips_only_when_still {
// We only show the tooltip when the mouse pointer is still.
if !self.ctx.input(|i| i.pointer.is_still()) {
Expand Down Expand Up @@ -731,10 +724,6 @@ impl Response {

// All checks passed: show the tooltip!

// Remember that we're showing a tooltip
self.ctx
.data_mut(|data| data.insert_temp::<f64>(when_was_a_toolip_last_shown_id, now));

true
}

Expand Down

0 comments on commit 525bbdb

Please sign in to comment.