Skip to content

Commit def1394

Browse files
committed
Add the touch event handlers to the webagg and nbagg backends.
1 parent 5ba3911 commit def1394

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,21 @@ def enter_notify_event(self, guiEvent=None, xy=None):
19761976
event = LocationEvent('figure_enter_event', self, x, y, guiEvent)
19771977
self.callbacks.process('figure_enter_event', event)
19781978

1979+
def touch_start_event(self, touches, guiEvent=None):
1980+
# Placeholder implementation awaiting
1981+
# https://github.com/matplotlib/matplotlib/pull/8041.
1982+
pass
1983+
1984+
def touch_move_event(self, touches, guiEvent=None):
1985+
# Placeholder implementation awaiting
1986+
# https://github.com/matplotlib/matplotlib/pull/8041.
1987+
pass
1988+
1989+
def touch_end_event(self, touches, guiEvent=None):
1990+
# Placeholder implementation awaiting
1991+
# https://github.com/matplotlib/matplotlib/pull/8041.
1992+
pass
1993+
19791994
def inaxes(self, xy):
19801995
"""
19811996
Return the topmost visible `~.axes.Axes` containing the point *xy*.

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,17 @@ def _handle_key(self, event):
291291
self.key_release_event(key, guiEvent=guiEvent)
292292
handle_key_press = handle_key_release = _handle_key
293293

294+
def _handle_touch(self, event):
295+
e_type = event['type']
296+
guiEvent = event.get('guiEvent', None)
297+
if e_type == 'touch_start':
298+
self.touch_start_event([], guiEvent=guiEvent)
299+
elif e_type == 'touch_move':
300+
self.touch_move_event([], guiEvent=guiEvent)
301+
elif e_type == 'touch_end':
302+
self.touch_end([], guiEvent=guiEvent)
303+
handle_touch_start = handle_touch_move = handle_touch_end = _handle_touch
304+
294305
def handle_toolbar_button(self, event):
295306
# TODO: Be more suspicious of the input
296307
getattr(self.toolbar, event['name'])()

lib/matplotlib/backends/web_backend/all_figures.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
main_div.appendChild(figure_div);
2525
var websocket_type = mpl.get_websocket_type();
2626
var websocket = new websocket_type("{{ ws_uri }}" + fig_id + "/ws");
27-
var fig = new mpl.figure(fig_id, websocket, mpl_ondownload, figure_div);
28-
29-
fig.focus_on_mouseover = true;
30-
31-
fig.canvas.setAttribute("tabindex", fig_id);
27+
var fig;
28+
websocket.onopen = function ()
29+
{
30+
fig = new mpl.figure(fig_id, websocket, mpl_ondownload, figure_div);
31+
fig.focus_on_mouseover = true;
32+
fig.canvas.setAttribute("tabindex", fig_id);
33+
}
3234
}
3335
};
3436

lib/matplotlib/backends/web_backend/js/mpl.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ mpl.figure.prototype._init_canvas = function () {
230230
};
231231
}
232232

233+
function on_touch_event_closure(name) {
234+
return function (event) {
235+
return fig.touch_event(event, name);
236+
};
237+
}
238+
233239
rubberband_canvas.addEventListener(
234240
'mousedown',
235241
on_mouse_event_closure('button_press')
@@ -242,12 +248,10 @@ mpl.figure.prototype._init_canvas = function () {
242248
'dblclick',
243249
on_mouse_event_closure('dblclick')
244250
);
245-
// Throttle sequential mouse events to 1 every 20ms.
246251
rubberband_canvas.addEventListener(
247252
'mousemove',
248253
on_mouse_event_closure('motion_notify')
249254
);
250-
251255
rubberband_canvas.addEventListener(
252256
'mouseenter',
253257
on_mouse_event_closure('figure_enter')
@@ -256,6 +260,18 @@ mpl.figure.prototype._init_canvas = function () {
256260
'mouseleave',
257261
on_mouse_event_closure('figure_leave')
258262
);
263+
rubberband_canvas.addEventListener(
264+
'touchmove',
265+
on_touch_event_closure('touch_move')
266+
);
267+
rubberband_canvas.addEventListener(
268+
'touchenter',
269+
on_touch_event_closure('touch_begin')
270+
);
271+
rubberband_canvas.addEventListener(
272+
'touchleave',
273+
on_touch_event_closure('touch_end')
274+
);
259275

260276
canvas_div.addEventListener('wheel', function (event) {
261277
if (event.deltaY < 0) {
@@ -628,6 +644,23 @@ mpl.figure.prototype.mouse_event = function (event, name) {
628644
return false;
629645
};
630646

647+
mpl.figure.prototype.touch_event = function (event, name) {
648+
var touches = [];
649+
for (var i = 0; i < event.changedTouches.length; i++) {
650+
var canvas_pos = mpl.findpos(event.changedTouches[i]);
651+
var x = canvas_pos.x * this.ratio;
652+
var y = canvas_pos.y * this.ratio;
653+
touches.push({x: x, y: y});
654+
}
655+
656+
this.send_message(name, {
657+
touches: touches
658+
});
659+
660+
event.preventDefault();
661+
return false;
662+
};
663+
631664
mpl.figure.prototype._key_event_extra = function (_event, _name) {
632665
// Handle any extra behaviour associated with a key event
633666
};

lib/matplotlib/backends/web_backend/single_figure.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@
2020
var websocket_type = mpl.get_websocket_type();
2121
var websocket = new websocket_type(
2222
"{{ ws_uri }}" + {{ str(fig_id) }} + "/ws");
23-
var fig = new mpl.figure(
23+
var fig;
24+
websocket.onopen = function ()
25+
{
26+
fig = new mpl.figure(
2427
{{ str(fig_id) }}, websocket, mpl_ondownload,
2528
document.getElementById("figure"));
29+
}
2630
}
2731
);
2832
</script>

0 commit comments

Comments
 (0)