diff --git a/tensorboard/components/vz_chart_helpers/plottable-interactions.ts b/tensorboard/components/vz_chart_helpers/plottable-interactions.ts index a86b74a8989..a6c3ea08c1f 100644 --- a/tensorboard/components/vz_chart_helpers/plottable-interactions.ts +++ b/tensorboard/components/vz_chart_helpers/plottable-interactions.ts @@ -196,6 +196,15 @@ class TouchDispatcher extends Plottable.Dispatchers.Touch { }; export class PointerInteraction extends Plottable.Interactions.Pointer { + constructor() { + super(); + // Route mouse events through `handleMouseEvent` below. Replacing the + // callback itself (instead of registering another one) keeps `_unanchor`, + // which removes `_mouseMoveCallback` from the dispatcher, working as is. + (this as any)._mouseMoveCallback = (p: Plottable.Point, e: MouseEvent) => + this.handleMouseEvent(p, e); + } + override _anchor(component) { const anyThis = this as any; anyThis._isAnchored = true; @@ -208,4 +217,36 @@ export class PointerInteraction extends Plottable.Interactions.Pointer { ); anyThis._touchDispatcher.onTouchStart(anyThis._touchStartCallback); } + + /** + * Fixes the tooltip staying on screen after the pointer leaves the window + * while it is over a chart, e.g., when opening macOS Mission Control or + * switching to another app or desktop. + * + * Plottable decides whether the pointer exited a component purely from the + * coordinates of mouse events. Since the MouseDispatcher above listens on + * the chart's rather than on the document, the last event the chart + * receives when the pointer leaves is a `mouseout`. When the pointer leaves + * without moving (the window loses the pointer), that `mouseout` carries + * coordinates that are still inside the chart, so it is treated as an + * ordinary move and the exit callbacks never run. The tooltip then stays + * until the pointer enters and leaves the chart again. + * + * A `mouseout` towards an element outside of the , or towards no + * element at all (`relatedTarget` is null when the pointer leaves the + * window), means the pointer is no longer over the chart regardless of its + * coordinates. + */ + private handleMouseEvent(p: Plottable.Point, e: MouseEvent) { + const anyThis = this as any; + const root = anyThis._componentAttachedTo.root().rootElement().node(); + if ( + e.type === 'mouseout' && + !Plottable.Utils.DOM.contains(root, e.relatedTarget as Element) + ) { + anyThis._handlePointerEvent(p, /* insideSVG= */ false); + return; + } + anyThis._handleMouseEvent(p, e); + } }