mwcraig / ipyevents

A custom widget for returning mouse and keyboard events to Python. Documentation:
https://ipyevents.readthedocs.io/en/latest/index.html
BSD 3-Clause "New" or "Revised" License
115 stars 27 forks source link

Add trait for coordinate of mouse #17

Closed mwcraig closed 6 years ago

mwcraig commented 6 years ago

This PR adds the ability to pass the mouse position vi a trait instead a custom event message.

Assuming e is an Event, setting e.xy_trait_coords = 'page' (or any of the other coordinates returned in a mouse event), means that e.xy will be updated with the position of the mouse. Any other event can also be attached and generate event messages like usual.

Setting e.xy_trait_coords = None turns off the xy trait.

A question that should be answered before merging:

Minimum working example:

from ipyevents import Event
from ipywidgets import HTML
h = HTML(value = '<h1>Hello world!</h1>')
e = Event()
e.xy_trait_coords = 'client'
e.source = h
h

In a subsequent cell e.xy will print the mouse position.

@pbugnion -- if you can review this one it would be helpful. I'll add to the documentation before merging, just holding off on that in case you suggest any changes to the interface.

Closes #8

mwcraig commented 6 years ago

Ping @maartenbreddels and @ejeschke in case you are interested in this feature...

pbugnion commented 6 years ago

This looks great. What if I want the mouse position in multiple coordinate systems?

mwcraig commented 6 years ago

This looks great. What if I want the mouse position in multiple coordinate systems?

I'm inclined to say multiple listeners...but am open to suggestions for how to handle that case.

pbugnion commented 6 years ago

I'm inclined to say multiple listeners

That makes sense.

mwcraig commented 6 years ago

I think I've addressed all of the comments. I'll add some documentation a little later, once #20 is merged (there are substantial changes to the doc notebook in there).

mwcraig commented 6 years ago

Thanks for the feedback, merging.