on_hover_text/ui many times will stack tooltips beneath the previous
This commit is contained in:
parent
5b0da17d91
commit
273212a63c
5 changed files with 29 additions and 9 deletions
|
@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||||
* Changed default font to [Ubuntu-Light](https://fonts.google.com/specimen/Ubuntu).
|
* Changed default font to [Ubuntu-Light](https://fonts.google.com/specimen/Ubuntu).
|
||||||
* Remove minimum button width
|
* Remove minimum button width
|
||||||
* Refactored `egui::Layout` substantially, changing its interface.
|
* Refactored `egui::Layout` substantially, changing its interface.
|
||||||
|
* Calling ``on_hover_text`/`on_hover_ui` multiple times will stack tooltips underneath the previous ones.
|
||||||
|
|
||||||
### Removed 🔥
|
### Removed 🔥
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,22 @@ use crate::*;
|
||||||
|
|
||||||
/// Show a tooltip at the current mouse position (if any).
|
/// Show a tooltip at the current mouse position (if any).
|
||||||
pub fn show_tooltip(ctx: &Arc<Context>, add_contents: impl FnOnce(&mut Ui)) {
|
pub fn show_tooltip(ctx: &Arc<Context>, add_contents: impl FnOnce(&mut Ui)) {
|
||||||
if let Some(mouse_pos) = ctx.input().mouse.pos {
|
let tooltip_rect = ctx.memory().tooltip_rect;
|
||||||
// TODO: default size
|
|
||||||
let id = Id::tooltip();
|
let window_pos = if let Some(tooltip_rect) = tooltip_rect {
|
||||||
let window_pos = mouse_pos + vec2(16.0, 16.0);
|
tooltip_rect.left_bottom()
|
||||||
show_popup(ctx, id, window_pos, add_contents);
|
} else if let Some(mouse_pos) = ctx.input().mouse.pos {
|
||||||
}
|
mouse_pos + vec2(16.0, 16.0)
|
||||||
|
} else {
|
||||||
|
return; // No good place for a tooltip :(
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: default size
|
||||||
|
let id = Id::tooltip();
|
||||||
|
let response = show_popup(ctx, id, window_pos, add_contents);
|
||||||
|
|
||||||
|
let tooltip_rect = tooltip_rect.unwrap_or_else(Rect::nothing);
|
||||||
|
ctx.memory().tooltip_rect = Some(tooltip_rect.union(response.rect));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Show a tooltip at the current mouse position (if any).
|
/// Show a tooltip at the current mouse position (if any).
|
||||||
|
|
|
@ -54,6 +54,12 @@ pub struct Memory {
|
||||||
#[cfg_attr(feature = "serde", serde(skip))]
|
#[cfg_attr(feature = "serde", serde(skip))]
|
||||||
popup: Option<Id>,
|
popup: Option<Id>,
|
||||||
|
|
||||||
|
/// If a tooltip has been shown this frame, where was it?
|
||||||
|
/// This is used to prevent multiple tooltips to cover each other.
|
||||||
|
/// Initialized to `None` at the start of each frame.
|
||||||
|
#[cfg_attr(feature = "serde", serde(skip))]
|
||||||
|
pub(crate) tooltip_rect: Option<Rect>,
|
||||||
|
|
||||||
/// Useful for debugging, benchmarking etc.
|
/// Useful for debugging, benchmarking etc.
|
||||||
pub all_collpasing_are_open: bool,
|
pub all_collpasing_are_open: bool,
|
||||||
/// Useful for debugging, benchmarking etc.
|
/// Useful for debugging, benchmarking etc.
|
||||||
|
@ -162,6 +168,7 @@ impl Memory {
|
||||||
) {
|
) {
|
||||||
self.used_ids.clear();
|
self.used_ids.clear();
|
||||||
self.interaction.begin_frame(prev_input, new_input);
|
self.interaction.begin_frame(prev_input, new_input);
|
||||||
|
self.tooltip_rect = None;
|
||||||
|
|
||||||
if !prev_input.mouse.down {
|
if !prev_input.mouse.down {
|
||||||
self.window_interaction = None;
|
self.window_interaction = None;
|
||||||
|
|
|
@ -116,7 +116,8 @@ impl std::fmt::Debug for Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
/// Show this UI if the item was hovered (i.e. a tooltip)
|
/// 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 {
|
pub fn on_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
|
||||||
if self.hovered {
|
if self.hovered {
|
||||||
crate::containers::show_tooltip(&self.ctx, add_contents);
|
crate::containers::show_tooltip(&self.ctx, add_contents);
|
||||||
|
@ -124,7 +125,8 @@ impl Response {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Show this text if the item was hovered (i.e. a tooltip)
|
/// 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 {
|
pub fn on_hover_text(self, text: impl Into<String>) -> Self {
|
||||||
self.on_hover_ui(|ui| {
|
self.on_hover_ui(|ui| {
|
||||||
ui.add(crate::widgets::Label::new(text));
|
ui.add(crate::widgets::Label::new(text));
|
||||||
|
|
|
@ -146,7 +146,7 @@ impl<'a> Widget for DragValue<'a> {
|
||||||
.sense(Sense::click_and_drag())
|
.sense(Sense::click_and_drag())
|
||||||
.text_style(TextStyle::Monospace);
|
.text_style(TextStyle::Monospace);
|
||||||
let response = ui.add(button);
|
let response = ui.add(button);
|
||||||
// response.on_hover_text("Drag to edit, click to enter a value"); // TODO: may clash with users own tooltips
|
let response = response.on_hover_text("Drag to edit, click to enter a value");
|
||||||
if response.clicked {
|
if response.clicked {
|
||||||
ui.memory().request_kb_focus(kb_edit_id);
|
ui.memory().request_kb_focus(kb_edit_id);
|
||||||
ui.memory().temp_edit_string = None; // Filled in next frame
|
ui.memory().temp_edit_string = None; // Filled in next frame
|
||||||
|
|
Loading…
Reference in a new issue