From c5ac7d301a90afbaec843ee04fb43d8a0956cc90 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 4 Dec 2024 14:23:05 +0100 Subject: [PATCH] Fix `on_hover_text_at_pointer` for transformed layers (#5429) --- crates/egui/src/containers/popup.rs | 12 +++++++++--- crates/egui_demo_lib/src/demo/pan_zoom.rs | 19 ++++++++++++++----- .../tests/snapshots/demos/Pan Zoom.png | 4 ++-- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/crates/egui/src/containers/popup.rs b/crates/egui/src/containers/popup.rs index 56e3c44b150..a90e615aca7 100644 --- a/crates/egui/src/containers/popup.rs +++ b/crates/egui/src/containers/popup.rs @@ -87,17 +87,22 @@ pub fn show_tooltip_at_pointer( // Add a small exclusion zone around the pointer to avoid tooltips // covering what we're hovering over. - let mut exclusion_rect = Rect::from_center_size(pointer_pos, Vec2::splat(24.0)); + let mut pointer_rect = Rect::from_center_size(pointer_pos, Vec2::splat(24.0)); // Keep the left edge of the tooltip in line with the cursor: - exclusion_rect.min.x = pointer_pos.x; + pointer_rect.min.x = pointer_pos.x; + + // Transform global coords to layer coords: + if let Some(transform) = ctx.memory(|m| m.layer_transforms.get(&parent_layer).copied()) { + pointer_rect = transform.inverse() * pointer_rect; + } show_tooltip_at_dyn( ctx, parent_layer, widget_id, allow_placing_below, - &exclusion_rect, + &pointer_rect, Box::new(add_contents), ) }) @@ -155,6 +160,7 @@ fn show_tooltip_at_dyn<'c, R>( widget_rect: &Rect, add_contents: Box R + 'c>, ) -> R { + // Transform layer coords to global coords: let mut widget_rect = *widget_rect; if let Some(transform) = ctx.memory(|m| m.layer_transforms.get(&parent_layer).copied()) { widget_rect = transform * widget_rect; diff --git a/crates/egui_demo_lib/src/demo/pan_zoom.rs b/crates/egui_demo_lib/src/demo/pan_zoom.rs index 6367946faef..f8411a740c0 100644 --- a/crates/egui_demo_lib/src/demo/pan_zoom.rs +++ b/crates/egui_demo_lib/src/demo/pan_zoom.rs @@ -73,20 +73,29 @@ impl crate::View for PanZoom { for (i, (pos, callback)) in [ ( egui::Pos2::new(0.0, 0.0), - Box::new(|ui: &mut egui::Ui, _: &mut Self| ui.button("top left!")) - as Box egui::Response>, + Box::new(|ui: &mut egui::Ui, _: &mut Self| { + ui.button("top left").on_hover_text("Normal tooltip") + }) as Box egui::Response>, ), ( egui::Pos2::new(0.0, 120.0), - Box::new(|ui: &mut egui::Ui, _| ui.button("bottom left?")), + Box::new(|ui: &mut egui::Ui, _| { + ui.button("bottom left").on_hover_text("Normal tooltip") + }), ), ( egui::Pos2::new(120.0, 120.0), - Box::new(|ui: &mut egui::Ui, _| ui.button("right bottom :D")), + Box::new(|ui: &mut egui::Ui, _| { + ui.button("right bottom") + .on_hover_text_at_pointer("Tooltip at pointer") + }), ), ( egui::Pos2::new(120.0, 0.0), - Box::new(|ui: &mut egui::Ui, _| ui.button("right top ):")), + Box::new(|ui: &mut egui::Ui, _| { + ui.button("right top") + .on_hover_text_at_pointer("Tooltip at pointer") + }), ), ( egui::Pos2::new(60.0, 60.0), diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Pan Zoom.png b/crates/egui_demo_lib/tests/snapshots/demos/Pan Zoom.png index 7ba225feae8..384840b7101 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Pan Zoom.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Pan Zoom.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:79ce1dbf7627579d4e10de6494e34d8fd9685506d7b35cb3c9148f90f8c01366 -size 25144 +oid sha256:ccfda16ef7cdf94f7fbbd2c0f8df6f6de7904969e2a66337920c32608a6f9f05 +size 25357