Only show tooltips if mouse is still.

This commit is contained in:
Emil Ernerfeldt 2021-02-20 10:45:19 +01:00
parent 4354f7582f
commit 741f0bfe8a
3 changed files with 34 additions and 4 deletions

View file

@ -11,12 +11,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added ⭐
* Add `egui::plot::Plot` to plot some 2D data
* Add `egui::plot::Plot` to plot some 2D data.
* Add `Ui::hyperlink_to(label, url)`.
### Changed 🔧
* Improve the positioning of tooltips.
* Only show tooltips if mouse is still.
## 0.9.0 - 2021-02-07 - Light Mode and much more

View file

@ -190,9 +190,7 @@ impl Response {
/// Show this UI if the item was hovered (i.e. a tooltip).
/// If you call this multiple times the tooltips will stack underneath the previous ones.
pub fn on_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
if (self.hovered() && self.ctx.input().pointer.tooltip_pos().is_some())
|| self.ctx.memory().everything_is_visible()
{
if self.should_show_hover_ui() {
crate::containers::show_tooltip_under(
&self.ctx,
self.id.with("__tooltip"),
@ -203,6 +201,28 @@ impl Response {
self
}
fn should_show_hover_ui(&self) -> bool {
if self.ctx.memory().everything_is_visible() {
true
} else if self.hovered && self.ctx.input().pointer.has_pointer() {
let show_tooltips_only_when_still =
self.ctx.style().interaction.show_tooltips_only_when_still;
if show_tooltips_only_when_still {
if self.ctx.input().pointer.is_still() {
true
} else {
// wait for mouse to stop
self.ctx.request_repaint();
false
}
} else {
true
}
} else {
false
}
}
/// Show this text if the item was hovered (i.e. a tooltip).
/// If you call this multiple times the tooltips will stack underneath the previous ones.
pub fn on_hover_text(self, text: impl Into<String>) -> Self {

View file

@ -110,6 +110,9 @@ pub struct Interaction {
/// Mouse must be the close to the corner of a window to resize
pub resize_grab_radius_corner: f32,
/// If `false`, tooltips will show up anytime you hover anything, even is mouse is still moving
pub show_tooltips_only_when_still: bool,
}
#[derive(Clone, Debug, PartialEq)]
@ -303,6 +306,7 @@ impl Default for Interaction {
Self {
resize_grab_radius_side: 5.0,
resize_grab_radius_corner: 10.0,
show_tooltips_only_when_still: true,
}
}
}
@ -516,11 +520,16 @@ impl Interaction {
let Self {
resize_grab_radius_side,
resize_grab_radius_corner,
show_tooltips_only_when_still,
} = self;
ui.add(Slider::f32(resize_grab_radius_side, 0.0..=20.0).text("resize_grab_radius_side"));
ui.add(
Slider::f32(resize_grab_radius_corner, 0.0..=20.0).text("resize_grab_radius_corner"),
);
ui.checkbox(
show_tooltips_only_when_still,
"Only show tooltips if mouse is still",
);
ui.vertical_centered(|ui| reset_button(ui, self));
}