From 344284e01331667c018e6ade5d791a20598a3a5c Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 29 Dec 2024 21:37:04 +0200 Subject: [PATCH] Invalidate tooltips when mouse leaves element's hitbox (#22488) Closes https://github.com/zed-industries/zed/issues/21657 In case of the task rerun button tooltip from https://github.com/zed-industries/zed/blob/f6dabadaf79bd29c89c8d55a1e9f1d33236f736e/crates/terminal_view/src/terminal_view.rs#L1051-L1070 , the actual button element is not styled as invisible, only its parent. Zed won't render such element since it's parent is hidden, but will consider it "visible" all the time its `paint` is called, spawning a task with the delay, that will create the tooltip: https://github.com/zed-industries/zed/blob/f6dabadaf79bd29c89c8d55a1e9f1d33236f736e/crates/gpui/src/elements/div.rs#L1949-L1959 When the parent is hidden, the child won't be painted anymore, and no mouse listeners will be able to detect this fact and hide the tooltip. Hence, check such cases separately, during `prepaint`, and invalidate the tooltips that are not valid anymore. We cannot use `hitbox.is_hovered(cx)` as it's not really hovered during prepaint, so a mouse position check is used instead. Release Notes: - Fixed tooltips getting stuck --- crates/gpui/src/elements/div.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/gpui/src/elements/div.rs b/crates/gpui/src/elements/div.rs index 36f42c137c1f9..c4143a13caab6 100644 --- a/crates/gpui/src/elements/div.rs +++ b/crates/gpui/src/elements/div.rs @@ -1417,6 +1417,19 @@ impl Interactivity { None }; + let invalidate_tooltip = hitbox + .as_ref() + .map_or(true, |hitbox| !hitbox.bounds.contains(&cx.mouse_position())); + if invalidate_tooltip { + if let Some(active_tooltip) = element_state + .as_ref() + .and_then(|state| state.active_tooltip.as_ref()) + { + *active_tooltip.borrow_mut() = None; + self.tooltip_id = None; + } + } + let scroll_offset = self.clamp_scroll_position(bounds, &style, cx); let result = f(&style, scroll_offset, hitbox, cx); (result, element_state)