Show tooltips of container responses when hovering their children (#7909)

I have found that `ui.horizontal(...).response.on_hover_text(...)`
doesn't work, although [documentation says
otherwise](https://docs.rs/egui/latest/egui/struct.Ui.html#method.horizontal):


> The returned
[Response](https://docs.rs/egui/latest/egui/response/struct.Response.html)
will only have checked for mouse hover but can be used for tooltips
(on_hover_text). It also contains the
[Rect](https://docs.rs/egui/latest/egui/struct.Rect.html) used by the
horizontal layout.The returned
[Response](https://docs.rs/egui/latest/egui/response/struct.Response.html)
will only have checked for mouse hover but can be used for tooltips
(on_hover_text). It also contains the
[Rect](https://docs.rs/egui/latest/egui/struct.Rect.html) used by the
horizontal layout.

So I added fix to this into tooltip, but this looks like a hack, and I
don't know a better place to do so. Do you have a better idea?

This PR is split into two commits: first commit adds test, and you can
see wrong behaviour in the image. Second commit fixes this and also
regenerates the image and you can now see tooltip.

I have used LLM to help me with this PR (codex). And I retained LLM
comment, because such hacks should have some comments, and it seems ok
to me.

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
ilya sheprutandEmil Ernerfeldt authored and GitHub committed 2026-10-06 07:28:36 +00:00
1 parent 3dc897b6f0
commit deb9ed9feb
4 files changed
+207 -12

No files matched your search

+60 -12
View File
@@ -1,7 +1,7 @@
use crate::pass_state::PerWidgetTooltipState;
use crate::{
AreaState, Context, Id, InnerResponse, LayerId, Layout, Order, Popup, PopupAnchor, PopupKind,
Response, Sense,
Response, Sense, WidgetRects,
};
use emath::{Rect, Vec2};
@@ -242,6 +242,16 @@ impl Tooltip<'_> {
})
}
/// Is any widget inside of the [`crate::Ui`] with the id `container` hovered?
fn is_child_hovered(ctx: &Context, container: Id) -> bool {
ctx.viewport(|vp| {
vp.interact_widgets
.hovered
.iter()
.any(|&id| is_descendant_of(&vp.this_pass.widgets, id, container))
})
}
/// Should we show a tooltip for this response?
///
/// Argument `allow_interactive_tooltip` controls whether mouse can interact with tooltip that
@@ -256,6 +266,18 @@ impl Tooltip<'_> {
return true;
}
let is_other_tooltip_shown_this_pass = response.ctx.pass_state(|fs| {
fs.layers
.get(&response.layer_id)
.and_then(|layer| layer.widget_with_tooltip)
.is_some_and(|id| id != response.id)
});
if is_other_tooltip_shown_this_pass {
// Only one tooltip per layer. Children are shown before their parent container,
// so this makes sure the innermost widget wins.
return false;
}
let any_open_popups = response.ctx.prev_pass_state(|fs| {
fs.layers
.get(&response.layer_id)
@@ -348,16 +370,15 @@ impl Tooltip<'_> {
}
}
let is_other_tooltip_open = response.ctx.prev_pass_state(|fs| {
if let Some(already_open_tooltip) = fs
.layers
.get(&response.layer_id)
.and_then(|layer| layer.widget_with_tooltip)
{
already_open_tooltip != response.id
} else {
false
}
let already_open_tooltip = response
.ctx
.prev_pass_state(|fs| fs.layers.get(&response.layer_id)?.widget_with_tooltip);
let is_other_tooltip_open = already_open_tooltip.is_some_and(|already_open_tooltip| {
already_open_tooltip != response.id
// A widget may take over the tooltip from a container it is inside of:
&& !response.ctx.viewport(|vp| {
is_descendant_of(&vp.this_pass.widgets, response.id, already_open_tooltip)
})
});
if is_other_tooltip_open {
// We only allow one tooltip per layer. First one wins. It is up to that tooltip to close itself.
@@ -366,7 +387,15 @@ impl Tooltip<'_> {
// Fast early-outs:
if response.enabled() {
if !response.hovered() || !response.ctx.input(|i| i.pointer.has_pointer()) {
// A container (e.g. `ui.horizontal(…).response`) is not hovered when the pointer
// is over one of its children, but we still want to show its tooltip then.
// If the child has a tooltip of its own, that tooltip was already shown
// (children are added before the container response is available), and wins.
let hovered = response.hovered()
|| (response.container_contains_pointer()
&& Self::is_child_hovered(&response.ctx, response.id));
if !hovered || !response.ctx.input(|i| i.pointer.has_pointer()) {
return false;
}
} else if !response
@@ -432,3 +461,22 @@ impl Tooltip<'_> {
})
}
}
/// Is `widget` (transitively) inside of the [`crate::Ui`] with the id `ancestor`?
fn is_descendant_of(widgets: &WidgetRects, widget: Id, ancestor: Id) -> bool {
let mut id = widget;
// Bounded, in case of id clashes creating a cycle:
for _ in 0..1000 {
let Some(w) = widgets.get(id) else {
return false;
};
if w.parent_id == ancestor {
return true;
}
if w.parent_id == id {
return false; // Reached the root
}
id = w.parent_id;
}
false
}
+141
View File
@@ -229,6 +229,147 @@ fn combobox_should_have_value() {
);
}
const CONTAINER_TOOLTIPS: [&str; 2] = ["Container tooltip", "B tooltip"];
/// A `ui.horizontal` with a tooltip, containing a button `A` without a tooltip,
/// and a button `B` with a tooltip, followed by some empty space.
fn container_tooltip_harness() -> Harness<'static> {
Harness::builder().with_size((140.0, 64.0)).build_ui(|ui| {
ui.style_mut().interaction.tooltip_delay = 0.0;
ui.style_mut().interaction.show_tooltips_only_when_still = false;
ui.horizontal(|ui| {
_ = ui.button("A");
_ = ui.button("B").on_hover_text("B tooltip");
ui.add_space(32.0);
})
.response
.on_hover_text("Container tooltip");
})
}
/// Assert that exactly `expected` (if any) of `all_tooltips` is shown.
#[track_caller]
fn assert_shown_tooltip(harness: &Harness<'_>, all_tooltips: &[&str], expected: Option<&str>) {
for &tooltip in all_tooltips {
// The container itself is also labelled by its tooltip (for accessibility),
// so we look for the label inside the tooltip:
let is_shown = harness
.query_by_role_and_label(Role::Label, tooltip)
.is_some();
let should_be_shown = Some(tooltip) == expected;
assert_eq!(
is_shown, should_be_shown,
"Tooltip {tooltip:?} shown: {is_shown}, expected: {should_be_shown}"
);
}
}
/// The response of `ui.horizontal` should show its tooltip when hovering empty space in it.
#[test]
fn container_tooltip_should_show_when_hovering_empty_space() {
let mut harness = container_tooltip_harness();
let b_rect = harness.get_by_label("B").rect();
harness.hover_at(b_rect.right_center() + egui::vec2(16.0, 0.0));
harness.run();
assert_shown_tooltip(&harness, &CONTAINER_TOOLTIPS, Some("Container tooltip"));
}
/// The response of `ui.horizontal` should show its tooltip when hovering a child without a tooltip.
#[test]
fn container_tooltip_should_show_when_hovering_child() {
let mut harness = container_tooltip_harness();
harness.get_by_label("A").hover();
harness.run();
assert_shown_tooltip(&harness, &CONTAINER_TOOLTIPS, Some("Container tooltip"));
harness.snapshot("container_tooltip_should_show_when_hovering_child");
}
/// A child with its own tooltip should win over the tooltip of its container.
#[test]
fn child_tooltip_should_win_over_container_tooltip() {
let mut harness = container_tooltip_harness();
harness.get_by_label("B").hover();
harness.run();
assert_shown_tooltip(&harness, &CONTAINER_TOOLTIPS, Some("B tooltip"));
harness.snapshot("child_tooltip_should_win_over_container_tooltip");
}
/// Moving from a child without a tooltip to a child with a tooltip
/// should replace the container tooltip with the tooltip of the child.
#[test]
fn child_tooltip_should_take_over_from_container_tooltip() {
let mut harness = container_tooltip_harness();
harness.get_by_label("A").hover();
harness.run();
assert_shown_tooltip(&harness, &CONTAINER_TOOLTIPS, Some("Container tooltip"));
harness.get_by_label("B").hover();
harness.run();
assert_shown_tooltip(&harness, &CONTAINER_TOOLTIPS, Some("B tooltip"));
harness.get_by_label("A").hover();
harness.run();
assert_shown_tooltip(&harness, &CONTAINER_TOOLTIPS, Some("Container tooltip"));
}
/// With nested containers, the innermost tooltip should win.
#[test]
fn nested_container_tooltips() {
const TOOLTIPS: [&str; 3] = ["Outer tooltip", "Inner tooltip", "Inner button tooltip"];
let build_harness = || {
Harness::builder().with_size((300.0, 120.0)).build_ui(|ui| {
ui.style_mut().interaction.tooltip_delay = 0.0;
ui.style_mut().interaction.show_tooltips_only_when_still = false;
ui.vertical(|ui| {
_ = ui.button("Outer button");
// A container without a tooltip:
ui.horizontal(|ui| {
_ = ui.button("Middle button");
ui.horizontal(|ui| {
_ = ui.button("Inner button");
_ = ui
.button("Inner button with tooltip")
.on_hover_text("Inner button tooltip");
})
.response
.on_hover_text("Inner tooltip");
});
})
.response
.on_hover_text("Outer tooltip");
})
};
let cases = [
("Outer button", "Outer tooltip"),
("Middle button", "Outer tooltip"),
("Inner button", "Inner tooltip"),
("Inner button with tooltip", "Inner button tooltip"),
];
// Hovering each widget directly:
for (button, expected) in cases {
let mut harness = build_harness();
harness.get_by_label(button).hover();
harness.run();
assert_shown_tooltip(&harness, &TOOLTIPS, Some(expected));
}
// Moving between the widgets, inwards and then outwards again:
let mut harness = build_harness();
for (button, expected) in core::iter::chain(cases.iter(), cases.iter().rev()) {
harness.get_by_label(button).hover();
harness.run();
assert_shown_tooltip(&harness, &TOOLTIPS, Some(expected));
}
}
/// This test ensures that `ui.response().interact(...)` works correctly.
///
/// This was broken, because there was an optimization in [`egui::Response::interact`]
@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1f35297cad33257c24ae9febd50da5831cc2a8f79ac752413bdedf4ec6b35459
size 3529
@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:00b0211b18d3a01699103bcb8149c544127799739c635ab540214bca2015ebb0
size 4355