Skip to content

Commit

Permalink
Fix on_hover_text_at_pointer for transformed layers (#5429)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk authored Dec 4, 2024
1 parent cd0f585 commit c5ac7d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
12 changes: 9 additions & 3 deletions crates/egui/src/containers/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,22 @@ pub fn show_tooltip_at_pointer<R>(

// 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),
)
})
Expand Down Expand Up @@ -155,6 +160,7 @@ fn show_tooltip_at_dyn<'c, R>(
widget_rect: &Rect,
add_contents: Box<dyn FnOnce(&mut Ui) -> 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;
Expand Down
19 changes: 14 additions & 5 deletions crates/egui_demo_lib/src/demo/pan_zoom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Fn(&mut egui::Ui, &mut Self) -> egui::Response>,
Box::new(|ui: &mut egui::Ui, _: &mut Self| {
ui.button("top left").on_hover_text("Normal tooltip")
}) as Box<dyn Fn(&mut egui::Ui, &mut Self) -> 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),
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/Pan Zoom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c5ac7d3

Please sign in to comment.