Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support on hover tooltip that is noninteractable even with interactable content #5543

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading