Skip to content

Commit ebbf493

Browse files
committed
Support on hover tooltip that is noninteractable even with interactable content
1 parent a5d7cf5 commit ebbf493

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

crates/egui/src/response.rs

+44-10
Original file line numberDiff line numberDiff line change
@@ -550,15 +550,37 @@ impl Response {
550550
/// ```
551551
#[doc(alias = "tooltip")]
552552
pub fn on_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
553-
if self.flags.contains(Flags::ENABLED) && self.should_show_hover_ui() {
553+
self.on_hover_ui_interactive(true, add_contents)
554+
}
555+
556+
/// Show this UI if the widget was hovered.
557+
///
558+
/// Argument `interactive` controls whether mouse can interact with interactive tooltip.
559+
pub fn on_hover_ui_interactive(
560+
self,
561+
interactive: bool,
562+
add_contents: impl FnOnce(&mut Ui),
563+
) -> Self {
564+
if self.flags.contains(Flags::ENABLED) && self.should_show_hover_ui(interactive) {
554565
self.show_tooltip_ui(add_contents);
555566
}
556567
self
557568
}
558569

559570
/// Show this UI when hovering if the widget is disabled.
560571
pub fn on_disabled_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
561-
if !self.enabled() && self.should_show_hover_ui() {
572+
self.on_disabled_hover_ui_interactive(true, add_contents)
573+
}
574+
575+
/// Show this UI when hovering if the widget is disabled.
576+
///
577+
/// Argument `interactive` controls whether mouse can interact with interactive tooltip.
578+
pub fn on_disabled_hover_ui_interactive(
579+
self,
580+
interactive: bool,
581+
add_contents: impl FnOnce(&mut Ui),
582+
) -> Self {
583+
if !self.enabled() && self.should_show_hover_ui(interactive) {
562584
crate::containers::show_tooltip_for(
563585
&self.ctx,
564586
self.layer_id,
@@ -572,7 +594,18 @@ impl Response {
572594

573595
/// Like `on_hover_ui`, but show the ui next to cursor.
574596
pub fn on_hover_ui_at_pointer(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
575-
if self.enabled() && self.should_show_hover_ui() {
597+
self.on_hover_ui_at_pointer_interactive(true, add_contents)
598+
}
599+
600+
/// Like `on_hover_ui`, but show the ui next to cursor.
601+
///
602+
/// Argument `interactive` controls whether mouse can interact with interactive tooltip.
603+
pub fn on_hover_ui_at_pointer_interactive(
604+
self,
605+
interactive: bool,
606+
add_contents: impl FnOnce(&mut Ui),
607+
) -> Self {
608+
if self.enabled() && self.should_show_hover_ui(interactive) {
576609
crate::containers::show_tooltip_at_pointer(
577610
&self.ctx,
578611
self.layer_id,
@@ -610,7 +643,7 @@ impl Response {
610643
crate::popup::was_tooltip_open_last_frame(&self.ctx, self.id)
611644
}
612645

613-
fn should_show_hover_ui(&self) -> bool {
646+
fn should_show_hover_ui(&self, allow_interactive: bool) -> bool {
614647
if self.ctx.memory(|mem| mem.everything_is_visible()) {
615648
return true;
616649
}
@@ -662,12 +695,13 @@ impl Response {
662695
let tooltip_id = crate::next_tooltip_id(&self.ctx, self.id);
663696
let tooltip_layer_id = LayerId::new(Order::Tooltip, tooltip_id);
664697

665-
let tooltip_has_interactive_widget = self.ctx.viewport(|vp| {
666-
vp.prev_pass
667-
.widgets
668-
.get_layer(tooltip_layer_id)
669-
.any(|w| w.enabled && w.sense.interactive())
670-
});
698+
let tooltip_has_interactive_widget = allow_interactive
699+
&& self.ctx.viewport(|vp| {
700+
vp.prev_pass
701+
.widgets
702+
.get_layer(tooltip_layer_id)
703+
.any(|w| w.enabled && w.sense.interactive())
704+
});
671705

672706
if tooltip_has_interactive_widget {
673707
// We keep the tooltip open if hovered,

0 commit comments

Comments
 (0)