Skip to content

Commit

Permalink
Refactor: move some state
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jul 3, 2024
1 parent cd38752 commit 84adbc5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
7 changes: 4 additions & 3 deletions crates/egui/src/containers/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ fn show_tooltip_at_dyn<'c, R>(

let mut state = ctx.frame_state_mut(|fs| {
// Remember that this is the widget showing the tooltip:
fs.tooltips
.per_layer_tooltip_widget
.insert(parent_layer, widget_id);
fs.layers
.entry(parent_layer)
.or_default()
.widget_with_tooltip = Some(widget_id);

fs.tooltips
.widget_tooltips
Expand Down
18 changes: 7 additions & 11 deletions crates/egui/src/frame_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,12 @@ pub struct TooltipFrameState {
/// If a tooltip has been shown this frame, where was it?
/// This is used to prevent multiple tooltips to cover each other.
pub widget_tooltips: IdMap<PerWidgetTooltipState>,

/// For each layer, which widget is showing a tooltip (if any)?
///
/// Only one widget per layer may show a tooltip.
/// But if a tooltip contains a tooltip, you can show a tooltip on top of a tooltip.
pub per_layer_tooltip_widget: ahash::HashMap<LayerId, Id>,
}

impl TooltipFrameState {
pub fn clear(&mut self) {
let Self {
widget_tooltips,
per_layer_tooltip_widget,
} = self;
let Self { widget_tooltips } = self;
widget_tooltips.clear();
per_layer_tooltip_widget.clear();
}
}

Expand All @@ -42,6 +32,12 @@ pub struct PerLayerState {
///
/// Does NOT include tooltips.
pub open_popups: HashSet<Id>,

/// Which widget is showing a tooltip (if any)?
///
/// Only one widget per layer may show a tooltip.
/// But if a tooltip contains a tooltip, you can show a tooltip on top of a tooltip.
pub widget_with_tooltip: Option<Id>,
}

#[cfg(feature = "accesskit")]
Expand Down
8 changes: 5 additions & 3 deletions crates/egui/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,10 +656,12 @@ impl Response {
}

let is_other_tooltip_open = self.ctx.prev_frame_state(|fs| {
if let Some(already_open_tooltip) =
fs.tooltips.per_layer_tooltip_widget.get(&self.layer_id)
if let Some(already_open_tooltip) = fs
.layers
.get(&self.layer_id)
.and_then(|layer| layer.widget_with_tooltip)
{
already_open_tooltip != &self.id
already_open_tooltip != self.id
} else {
false
}
Expand Down

0 comments on commit 84adbc5

Please sign in to comment.