diff --git a/egui/src/containers/popup.rs b/egui/src/containers/popup.rs index 70ca2319..548d95fc 100644 --- a/egui/src/containers/popup.rs +++ b/egui/src/containers/popup.rs @@ -2,6 +2,39 @@ use crate::*; +// ---------------------------------------------------------------------------- + +/// Same state for all tooltips. +#[derive(Clone, Debug, Default)] +pub(crate) struct MonoState { + last_id: Option, + last_size: Option, +} + +impl MonoState { + fn tooltip_size(&self, id: Id) -> Option { + if self.last_id == Some(id) { + self.last_size + } else { + None + } + } + + fn set_tooltip_size(&mut self, id: Id, size: Vec2) { + if self.last_id == Some(id) { + if let Some(stored_size) = &mut self.last_size { + *stored_size = stored_size.max(size); + return; + } + } + + self.last_id = Some(id); + self.last_size = Some(size); + } +} + +// ---------------------------------------------------------------------------- + /// Show a tooltip at the current pointer position (if any). /// /// Most of the time it is easier to use [`Response::on_hover_ui`]. @@ -59,13 +92,15 @@ pub fn show_tooltip_at( return; // No good place for a tooltip :( }; - let expected_size = ctx.memory().tooltip_size(id); + let expected_size = ctx.memory().tooltip.tooltip_size(id); let expected_size = expected_size.unwrap_or_else(|| vec2(64.0, 32.0)); let position = position.min(ctx.input().screen_rect().right_bottom() - expected_size); let position = position.max(ctx.input().screen_rect().left_top()); let response = show_tooltip_area(ctx, id, position, add_contents); - ctx.memory().set_tooltip_size(id, response.rect.size()); + ctx.memory() + .tooltip + .set_tooltip_size(id, response.rect.size()); ctx.frame_state().tooltip_rect = Some((id, tooltip_rect.union(response.rect))); } diff --git a/egui/src/memory.rs b/egui/src/memory.rs index 99f3c7d7..c2e4f6aa 100644 --- a/egui/src/memory.rs +++ b/egui/src/memory.rs @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet}; use crate::{ area, collapsing_header, menu, resize, scroll_area, util::Cache, widgets::text_edit, window, - Id, InputState, LayerId, Pos2, Rect, Style, Vec2, + Id, InputState, LayerId, Pos2, Rect, Style, }; use epaint::color::{Color32, Hsva}; @@ -38,6 +38,9 @@ pub struct Memory { #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) drag_value: crate::widgets::drag_value::MonoState, + #[cfg_attr(feature = "persistence", serde(skip))] + pub(crate) tooltip: crate::containers::popup::MonoState, + pub(crate) areas: Areas, /// Used by color picker @@ -49,10 +52,6 @@ pub struct Memory { #[cfg_attr(feature = "persistence", serde(skip))] popup: Option, - /// Used to clamp the tooltip to the screen. - #[cfg_attr(feature = "persistence", serde(skip))] - tooltip_size: Option<(Id, Vec2)>, - #[cfg_attr(feature = "persistence", serde(skip))] everything_is_visible: bool, } @@ -262,29 +261,6 @@ impl Memory { pub fn reset_areas(&mut self) { self.areas = Default::default(); } - - pub(crate) fn tooltip_size(&self, id: Id) -> Option { - if let Some((stored_id, stored_size)) = self.tooltip_size { - if stored_id == id { - Some(stored_size) - } else { - None - } - } else { - None - } - } - - pub(crate) fn set_tooltip_size(&mut self, id: Id, size: Vec2) { - if let Some((stored_id, stored_size)) = &mut self.tooltip_size { - if *stored_id == id { - *stored_size = stored_size.max(size); - return; - } - } - - self.tooltip_size = Some((id, size)); - } } /// ## Popups