Skip to content

Commit

Permalink
Add "page_up" and "page_down" keybinds for zoom-in and -out.
Browse files Browse the repository at this point in the history
  • Loading branch information
salt-die committed Oct 15, 2024
1 parent d1df8c4 commit 6cf227d
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/batgrl/gadgets/line_plot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""A 2D line plot gadget."""
"""A 2-D line plot gadget."""

from __future__ import annotations

Expand Down Expand Up @@ -75,7 +75,9 @@ def __set__(self, instance, value):

class LinePlot(Gadget):
r"""
A 2D line plot gadget.
A 2-D line plot gadget.
Zoom in or out with mouse wheel or "page_up"/"page_down".
Parameters
----------
Expand Down Expand Up @@ -592,25 +594,41 @@ def on_size(self):
"""Rebuild plot on resize."""
self._build_plot()

def _zoom_in(self):
if self._traces_zoom_index < len(PLOT_ZOOM) - 1:
self._traces_zoom_index += 1

def _zoom_out(self):
if self._traces_zoom_index > 0:
self._traces_zoom_index -= 1

def on_mouse(self, mouse_event: MouseEvent) -> bool | None:
"""Zoom-in or -out on mouse wheel."""
if not self.collides_point(mouse_event.pos):
return

if mouse_event.event_type == "scroll_up":
if self._traces_zoom_index >= len(PLOT_ZOOM) - 1:
return True
self._traces_zoom_index += 1
self._zoom_in()
elif mouse_event.event_type == "scroll_down":
if self._traces_zoom_index <= 0:
return True
self._traces_zoom_index -= 1
self._zoom_out()
else:
return super().on_mouse(mouse_event)

self._build_plot()
return True

def on_key(self, key_event):
"""Zoom-in or -out with "page_up" or "page_down"."""
if key_event.key == "page_up":
self._zoom_in()
elif key_event.key == "page_down":
self._zoom_out()
else:
return super().on_key(key_event)

self._build_plot()
return True

def on_add(self):
"""Built plot on add."""
super().on_add()
Expand Down

0 comments on commit 6cf227d

Please sign in to comment.