Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guiEvent handling for web backends #4100

Merged
merged 4 commits into from
Feb 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/matplotlib/backends/backend_webagg_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ def get_renderer(self, cleared=None):

def handle_event(self, event):
e_type = event['type']
guiEvent = event.get('guiEvent', None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the payload typically look like. Is its size a concern?


if e_type == 'ack':
# Network latency tends to decrease if traffic is flowing
# in both directions. Therefore, the browser sends back
Expand Down Expand Up @@ -299,23 +301,23 @@ def handle_event(self, event):
button = 3

if e_type == 'button_press':
self.button_press_event(x, y, button)
self.button_press_event(x, y, button, guiEvent=guiEvent)
elif e_type == 'button_release':
self.button_release_event(x, y, button)
self.button_release_event(x, y, button, guiEvent=guiEvent)
elif e_type == 'motion_notify':
self.motion_notify_event(x, y)
self.motion_notify_event(x, y, guiEvent=guiEvent)
elif e_type == 'figure_enter':
self.enter_notify_event(xy=(x, y))
self.enter_notify_event(xy=(x, y), guiEvent=guiEvent)
elif e_type == 'figure_leave':
self.leave_notify_event()
elif e_type == 'scroll':
self.scroll_event(x, y, event['step'])
self.scroll_event(x, y, event['step'], guiEvent=guiEvent)
elif e_type in ('key_press', 'key_release'):
key = _handle_key(event['key'])
if e_type == 'key_press':
self.key_press_event(key)
self.key_press_event(key, guiEvent=guiEvent)
elif e_type == 'key_release':
self.key_release_event(key)
self.key_release_event(key, guiEvent=guiEvent)
elif e_type == 'toolbar_button':
# TODO: Be more suspicious of the input
getattr(self.toolbar, event['name'])()
Expand Down
19 changes: 17 additions & 2 deletions lib/matplotlib/backends/web_backend/mpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,19 @@ mpl.findpos = function(e) {
return {"x": x, "y": y};
};

/*
* return a copy of an object with only non-object keys
* we need this to avoid circular references
* http://stackoverflow.com/a/24161582/3208463
*/
function simpleKeys (original) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stackoverflow link helps, but perhaps an example of the kind of thing this might end up looking like might be a good idea.

return Object.keys(original).reduce(function (obj, key) {
if (typeof original[key] !== 'object')
obj[key] = original[key]
return obj;
}, {});
}

mpl.figure.prototype.mouse_event = function(event, name) {
var canvas_pos = mpl.findpos(event)

Expand All @@ -467,7 +480,8 @@ mpl.figure.prototype.mouse_event = function(event, name) {
var y = canvas_pos.y;

this.send_message(name, {x: x, y: y, button: event.button,
step: event.step});
step: event.step,
guiEvent: simpleKeys(event)});

/* This prevents the web browser from automatically changing to
* the text insertion cursor when the button is pressed. We want
Expand Down Expand Up @@ -507,7 +521,8 @@ mpl.figure.prototype.key_event = function(event, name) {

this._key_event_extra(event, name);

this.send_message(name, {key: value});
this.send_message(name, {key: value,
guiEvent: simpleKeys(event)});
return false;
}

Expand Down