Constrain tooltips to fit within the screen

This commit is contained in:
Emil Ernerfeldt 2020-12-19 16:16:36 +01:00
parent 03d09a9b57
commit f9b1e88fe6

View file

@ -7,7 +7,11 @@ pub fn show_tooltip(ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui)) {
let window_pos = if let Some(tooltip_rect) = tooltip_rect {
tooltip_rect.left_bottom()
} else if let Some(mouse_pos) = ctx.input().mouse.pos {
mouse_pos + vec2(16.0, 16.0)
let expected_size = vec2(ctx.style().spacing.tooltip_width, 32.0);
let position = mouse_pos + vec2(16.0, 16.0);
let position = position.min(ctx.input().screen_rect().right_bottom() - expected_size);
let position = position.max(ctx.input().screen_rect().left_top());
position
} else {
return; // No good place for a tooltip :(
};