Skip to content
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
41 changes: 41 additions & 0 deletions tensorboard/components/vz_chart_helpers/plottable-interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <svg> 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 <svg>, 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);
}
}