Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Check pressed mouse buttons in pan/zoom drag handlers. #29066

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

Merged
merged 1 commit into from
Nov 15, 2024
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
32 changes: 25 additions & 7 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3059,6 +3059,11 @@

def drag_pan(self, event):
"""Callback for dragging in pan/zoom mode."""
if event.buttons != {self._pan_info.button}:
# Zoom ended while canvas not in focus (it did not receive a
# button_release_event); cancel it.
self.release_pan(None) # release_pan doesn't actually use event.
return

Check warning on line 3066 in lib/matplotlib/backend_bases.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backend_bases.py#L3065-L3066

Added lines #L3065 - L3066 were not covered by tests
for ax in self._pan_info.axes:
# Using the recorded button at the press is safer than the current
# button, as multiple buttons can get pressed during motion.
Expand Down Expand Up @@ -3092,7 +3097,7 @@
for a in self.canvas.figure.get_axes():
a.set_navigate_mode(self.mode._navigate_mode)

_ZoomInfo = namedtuple("_ZoomInfo", "direction start_xy axes cid cbar")
_ZoomInfo = namedtuple("_ZoomInfo", "button start_xy axes cid cbar")

def press_zoom(self, event):
"""Callback for mouse button press in zoom to rect mode."""
Expand All @@ -3117,11 +3122,17 @@
cbar = None

self._zoom_info = self._ZoomInfo(
direction="in" if event.button == 1 else "out",
start_xy=(event.x, event.y), axes=axes, cid=id_zoom, cbar=cbar)
button=event.button, start_xy=(event.x, event.y), axes=axes,
cid=id_zoom, cbar=cbar)

def drag_zoom(self, event):
"""Callback for dragging in zoom mode."""
if event.buttons != {self._zoom_info.button}:
# Zoom ended while canvas not in focus (it did not receive a
# button_release_event); cancel it.
self._cleanup_post_zoom()
return

Check warning on line 3134 in lib/matplotlib/backend_bases.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backend_bases.py#L3133-L3134

Added lines #L3133 - L3134 were not covered by tests

start_xy = self._zoom_info.start_xy
ax = self._zoom_info.axes[0]
(x1, y1), (x2, y2) = np.clip(
Expand Down Expand Up @@ -3150,6 +3161,7 @@
self.remove_rubberband()

start_x, start_y = self._zoom_info.start_xy
direction = "in" if self._zoom_info.button == 1 else "out"
key = event.key
# Force the key on colorbars to ignore the zoom-cancel on the
# short-axis side
Expand All @@ -3161,8 +3173,7 @@
# "cancel" a zoom action by zooming by less than 5 pixels.
if ((abs(event.x - start_x) < 5 and key != "y") or
(abs(event.y - start_y) < 5 and key != "x")):
self.canvas.draw_idle()
self._zoom_info = None
self._cleanup_post_zoom()

Check warning on line 3176 in lib/matplotlib/backend_bases.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backend_bases.py#L3176

Added line #L3176 was not covered by tests
return

for i, ax in enumerate(self._zoom_info.axes):
Expand All @@ -3174,11 +3185,18 @@
for prev in self._zoom_info.axes[:i])
ax._set_view_from_bbox(
(start_x, start_y, event.x, event.y),
self._zoom_info.direction, key, twinx, twiny)
direction, key, twinx, twiny)

self._cleanup_post_zoom()
self.push_current()

def _cleanup_post_zoom(self):
# We don't check the event button here, so that zooms can be cancelled
# by (pressing and) releasing another mouse button.
self.canvas.mpl_disconnect(self._zoom_info.cid)
self.remove_rubberband()
self.canvas.draw_idle()
self._zoom_info = None
self.push_current()

def push_current(self):
"""Push the current view limits and position onto the stack."""
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backend_bases.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ class NavigationToolbar2:
def zoom(self, *args) -> None: ...

class _ZoomInfo(NamedTuple):
direction: Literal["in", "out"]
button: MouseButton
start_xy: tuple[float, float]
axes: list[Axes]
cid: int
Expand Down
17 changes: 12 additions & 5 deletions lib/matplotlib/tests/test_backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,21 @@ def test_interactive_colorbar(plot_func, orientation, tool, button, expected):
# Set up the mouse movements
start_event = MouseEvent(
"button_press_event", fig.canvas, *s0, button)
drag_event = MouseEvent(
"motion_notify_event", fig.canvas, *s1, button, buttons={button})
stop_event = MouseEvent(
"button_release_event", fig.canvas, *s1, button)

tb = NavigationToolbar2(fig.canvas)
if tool == "zoom":
tb.zoom()
tb.press_zoom(start_event)
tb.drag_zoom(stop_event)
tb.drag_zoom(drag_event)
tb.release_zoom(stop_event)
else:
tb.pan()
tb.press_pan(start_event)
tb.drag_pan(stop_event)
tb.drag_pan(drag_event)
tb.release_pan(stop_event)

# Should be close, but won't be exact due to screen integer resolution
Expand Down Expand Up @@ -395,14 +397,17 @@ def test_interactive_pan(key, mouseend, expectedxlim, expectedylim):
start_event = MouseEvent(
"button_press_event", fig.canvas, *sstart, button=MouseButton.LEFT,
key=key)
drag_event = MouseEvent(
"motion_notify_event", fig.canvas, *send, button=MouseButton.LEFT,
buttons={MouseButton.LEFT}, key=key)
stop_event = MouseEvent(
"button_release_event", fig.canvas, *send, button=MouseButton.LEFT,
key=key)

tb = NavigationToolbar2(fig.canvas)
tb.pan()
tb.press_pan(start_event)
tb.drag_pan(stop_event)
tb.drag_pan(drag_event)
tb.release_pan(stop_event)
# Should be close, but won't be exact due to screen integer resolution
assert tuple(ax.get_xlim()) == pytest.approx(expectedxlim, abs=0.02)
Expand Down Expand Up @@ -510,6 +515,8 @@ def test_interactive_pan_zoom_events(tool, button, patch_vis, forward_nav, t_s):

# Set up the mouse movements
start_event = MouseEvent("button_press_event", fig.canvas, *s0, button)
drag_event = MouseEvent(
"motion_notify_event", fig.canvas, *s1, button, buttons={button})
stop_event = MouseEvent("button_release_event", fig.canvas, *s1, button)

tb = NavigationToolbar2(fig.canvas)
Expand All @@ -534,7 +541,7 @@ def test_interactive_pan_zoom_events(tool, button, patch_vis, forward_nav, t_s):

tb.zoom()
tb.press_zoom(start_event)
tb.drag_zoom(stop_event)
tb.drag_zoom(drag_event)
tb.release_zoom(stop_event)

assert ax_t.get_xlim() == pytest.approx(xlim_t, abs=0.15)
Expand Down Expand Up @@ -570,7 +577,7 @@ def test_interactive_pan_zoom_events(tool, button, patch_vis, forward_nav, t_s):

tb.pan()
tb.press_pan(start_event)
tb.drag_pan(stop_event)
tb.drag_pan(drag_event)
tb.release_pan(stop_event)

assert ax_t.get_xlim() == pytest.approx(xlim_t, abs=0.15)
Expand Down
6 changes: 4 additions & 2 deletions lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2130,19 +2130,21 @@ def test_toolbar_zoom_pan(tool, button, key, expected):
# Set up the mouse movements
start_event = MouseEvent(
"button_press_event", fig.canvas, *s0, button, key=key)
drag_event = MouseEvent(
"motion_notify_event", fig.canvas, *s1, button, key=key, buttons={button})
stop_event = MouseEvent(
"button_release_event", fig.canvas, *s1, button, key=key)

tb = NavigationToolbar2(fig.canvas)
if tool == "zoom":
tb.zoom()
tb.press_zoom(start_event)
tb.drag_zoom(stop_event)
tb.drag_zoom(drag_event)
tb.release_zoom(stop_event)
else:
tb.pan()
tb.press_pan(start_event)
tb.drag_pan(stop_event)
tb.drag_pan(drag_event)
tb.release_pan(stop_event)

# Should be close, but won't be exact due to screen integer resolution
Expand Down
Loading