Trigger events via standard callbacks in widget testing. by anntzer · Pull Request #29993 · matplotlib/matplotlib · GitHub
Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/deprecations/29993-AL.rst
33 changes: 33 additions & 0 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,23 @@ def __init__(self, name, canvas, x, y, button=None, key=None,
self.step = step
self.dblclick = dblclick

@classmethod
def _from_ax_coords(cls, name, ax, xy, *args, **kwargs):
"""
Generate a synthetic event at a given axes coordinate.

This method is intended for creating events during testing. The event
can be emitted by calling its ``_process()`` method.

args and kwargs are mapped to `.MouseEvent.__init__` parameters,
starting with `button`.
"""
x, y = ax.transData.transform(xy)
event = cls(name, ax.figure.canvas, x, y, *args, **kwargs)
event.inaxes = ax
event.xdata, event.ydata = xy # Force exact xy to avoid fp roundtrip issues.
return event

def __str__(self):
return (f"{self.name}: "
f"xy=({self.x}, {self.y}) xydata=({self.xdata}, {self.ydata}) "
Expand Down Expand Up @@ -1503,6 +1520,22 @@ def __init__(self, name, canvas, key, x=0, y=0, guiEvent=None):
super().__init__(name, canvas, x, y, guiEvent=guiEvent)
self.key = key

@classmethod
def _from_ax_coords(cls, name, ax, xy, key, *args, **kwargs):
"""
Generate a synthetic event at a given axes coordinate.

This method is intended for creating events during testing. The event
can be emitted by calling its ``_process()`` method.
"""
# Separate from MouseEvent._from_ax_coords instead of being defined in the base
# class, due to different parameter order in the constructor signature.
x, y = ax.transData.transform(xy)
event = cls(name, ax.figure.canvas, key, x, y, *args, **kwargs)
event.inaxes = ax
event.xdata, event.ydata = xy # Force exact xy to avoid fp roundtrip issues.
return event


# Default callback for key events.
def _key_handler(event):
Expand Down
23 changes: 12 additions & 11 deletions lib/matplotlib/testing/widgets.py
Loading