Skip to content

Commit

Permalink
Support on hover tooltip that is noninteractable even with interactab…
Browse files Browse the repository at this point in the history
…le content
  • Loading branch information
PPakalns committed Jan 16, 2025
1 parent a5d7cf5 commit ebbf493
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions crates/egui/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,15 +550,37 @@ impl Response {
/// ```
#[doc(alias = "tooltip")]
pub fn on_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
if self.flags.contains(Flags::ENABLED) && self.should_show_hover_ui() {
self.on_hover_ui_interactive(true, add_contents)
}

/// Show this UI if the widget was hovered.
///
/// Argument `interactive` controls whether mouse can interact with interactive tooltip.
pub fn on_hover_ui_interactive(
self,
interactive: bool,
add_contents: impl FnOnce(&mut Ui),
) -> Self {
if self.flags.contains(Flags::ENABLED) && self.should_show_hover_ui(interactive) {
self.show_tooltip_ui(add_contents);
}
self
}

/// Show this UI when hovering if the widget is disabled.
pub fn on_disabled_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
if !self.enabled() && self.should_show_hover_ui() {
self.on_disabled_hover_ui_interactive(true, add_contents)
}

/// Show this UI when hovering if the widget is disabled.
///
/// Argument `interactive` controls whether mouse can interact with interactive tooltip.
pub fn on_disabled_hover_ui_interactive(
self,
interactive: bool,
add_contents: impl FnOnce(&mut Ui),
) -> Self {
if !self.enabled() && self.should_show_hover_ui(interactive) {
crate::containers::show_tooltip_for(
&self.ctx,
self.layer_id,
Expand All @@ -572,7 +594,18 @@ impl Response {

/// Like `on_hover_ui`, but show the ui next to cursor.
pub fn on_hover_ui_at_pointer(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
if self.enabled() && self.should_show_hover_ui() {
self.on_hover_ui_at_pointer_interactive(true, add_contents)
}

/// Like `on_hover_ui`, but show the ui next to cursor.
///
/// Argument `interactive` controls whether mouse can interact with interactive tooltip.
pub fn on_hover_ui_at_pointer_interactive(
self,
interactive: bool,
add_contents: impl FnOnce(&mut Ui),
) -> Self {
if self.enabled() && self.should_show_hover_ui(interactive) {
crate::containers::show_tooltip_at_pointer(
&self.ctx,
self.layer_id,
Expand Down Expand Up @@ -610,7 +643,7 @@ impl Response {
crate::popup::was_tooltip_open_last_frame(&self.ctx, self.id)
}

fn should_show_hover_ui(&self) -> bool {
fn should_show_hover_ui(&self, allow_interactive: bool) -> bool {
if self.ctx.memory(|mem| mem.everything_is_visible()) {
return true;
}
Expand Down Expand Up @@ -662,12 +695,13 @@ impl Response {
let tooltip_id = crate::next_tooltip_id(&self.ctx, self.id);
let tooltip_layer_id = LayerId::new(Order::Tooltip, tooltip_id);

let tooltip_has_interactive_widget = self.ctx.viewport(|vp| {
vp.prev_pass
.widgets
.get_layer(tooltip_layer_id)
.any(|w| w.enabled && w.sense.interactive())
});
let tooltip_has_interactive_widget = allow_interactive
&& self.ctx.viewport(|vp| {
vp.prev_pass
.widgets
.get_layer(tooltip_layer_id)
.any(|w| w.enabled && w.sense.interactive())
});

if tooltip_has_interactive_widget {
// We keep the tooltip open if hovered,
Expand Down

0 comments on commit ebbf493

Please sign in to comment.