Fix hover-to-zoom of font texture in demo

Closes https://github.com/emilk/egui/issues/220
This commit is contained in:
Emil Ernerfeldt 2021-03-13 12:55:29 +01:00
parent b1883d5d48
commit 6442d254a6
3 changed files with 32 additions and 19 deletions

View file

@ -29,10 +29,10 @@ impl Widget for &epaint::Texture {
let (tex_w, tex_h) = (self.width as f32, self.height as f32);
let pointer_pos = response.interact_pointer_pos();
response.on_hover_ui(|ui| {
let pos = pointer_pos.unwrap_or_else(|| ui.min_rect().left_top());
response
.on_hover_cursor(CursorIcon::ZoomIn)
.on_hover_ui_at_pointer(|ui| {
if let Some(pos) = ui.input().pointer.latest_pos() {
let (_id, zoom_rect) = ui.allocate_space(vec2(128.0, 128.0));
let u = remap_clamp(pos.x, rect.x_range(), 0.0..=tex_w);
let v = remap_clamp(pos.y, rect.y_range(), 0.0..=tex_h);
@ -48,6 +48,7 @@ impl Widget for &epaint::Texture {
let mut mesh = Mesh::default();
mesh.add_rect_with_uv(zoom_rect, uv_rect, Color32::WHITE);
ui.painter().add(Shape::mesh(mesh));
}
});
})
.response

View file

@ -267,6 +267,18 @@ impl Response {
self
}
/// 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() {
crate::containers::show_tooltip_at_pointer(
&self.ctx,
self.id.with("__tooltip"),
add_contents,
);
}
self
}
fn should_show_hover_ui(&self) -> bool {
if self.ctx.memory().everything_is_visible() {
true

View file

@ -318,7 +318,7 @@ impl Default for Interaction {
Self {
resize_grab_radius_side: 5.0,
resize_grab_radius_corner: 10.0,
show_tooltips_only_when_still: true,
show_tooltips_only_when_still: false,
}
}
}