Skip to content

Commit

Permalink
feat: support button clicks and modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Feb 5, 2021
1 parent 3227348 commit 6ffb217
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions examples/mouse.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@
"def on_mouse_msg(interaction, data, buffers):\n",
" # it might be a good idea to throttle on the Python side as well, for instance when many computations\n",
" # happen, we can effectively ignore the queue of messages\n",
" if data['event'] in ['click', 'dblclick', 'contextmenu']:\n",
" widget_label.value = f'click {data}'\n",
" if data['event'] == 'mousemove':\n",
" domain_x = data['domain']['x']\n",
" domain_y = data['domain']['y']\n",
Expand Down
11 changes: 7 additions & 4 deletions js/lib/MouseInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ class MouseInteraction extends Interaction_1.Interaction {
this._emit('dragend', { x: e.x, y: e.y });
}));
// and click events
['click', 'dblclick', 'mouseenter', 'mouseleave'].forEach(eventName => {
['click', 'dblclick', 'mouseenter', 'mouseleave', 'contextmenu'].forEach(eventName => {
eventElement.on(eventName, () => {
this._emitThrottled.flush(); // we don't want mousemove events to come after enter/leave
const e = d3GetEvent();
// to be consistent with drag events, we need to user clientPoint
const [x, y] = d3_selection_1.clientPoint(eventElement.node(), e);
this._emit(eventName, { x, y });
e.preventDefault();
e.stopPropagation();
this._emit(eventName, { x, y }, {button: e.button, altKey: e.altKey, ctrlKey: e.ctrlKey, metaKey: e.metaKey});
return false
});
});
// throttled events
Expand All @@ -88,9 +91,9 @@ class MouseInteraction extends Interaction_1.Interaction {
super.remove();
this.parent.off('margin_updated', this.updateScaleRanges);
}
_emit(name, { x, y }) {
_emit(name, { x, y }, extra) {
let domain = { x: this.x_scale.scale.invert(x), y: this.y_scale.scale.invert(y) };
this.send({ event: name, pixel: { x, y }, domain: domain });
this.send({ event: name, pixel: { x, y }, domain: domain, ...extra });
}
}
exports.MouseInteraction = MouseInteraction;

0 comments on commit 6ffb217

Please sign in to comment.