|
3 | 3 | import numpy.typing
|
4 | 4 | import PIL.Image
|
5 | 5 | import PIL.ImageTk
|
| 6 | +import platform |
6 | 7 | import tkinter as tk
|
7 | 8 | from typing import Optional
|
8 | 9 |
|
@@ -44,6 +45,11 @@ def __init__(
|
44 | 45 | # every function will be Callable: some are methods and some are
|
45 | 46 | # functools.partial objects.
|
46 | 47 | self.bind(button_name, function) # type: ignore
|
| 48 | + if platform.system() == "Darwin": |
| 49 | + # Macs use Tcl/Tk version 9.0.0 or later to get touchpad support, |
| 50 | + # whereas (as of January 2025) Ubuntu uses Tcl/Tk version 8.6.14, |
| 51 | + # which lacks this event type. |
| 52 | + self.bind("<TouchpadScroll>", self._zoom_touchpad) |
47 | 53 |
|
48 | 54 | def _set_image(self) -> None:
|
49 | 55 | """
|
@@ -82,11 +88,31 @@ def _set_image(self) -> None:
|
82 | 88 | image=self._cached_image)
|
83 | 89 |
|
84 | 90 | def _zoom_mac(self, event: tk.Event) -> None:
|
85 |
| - self._zoom(-event.delta, event) |
| 91 | + sign = 1 if event.delta > 0 else -1 |
| 92 | + self._zoom(-sign, event) |
| 93 | + |
| 94 | + _zoom_touchpad_amount: int = 0 |
| 95 | + def _zoom_touchpad(self, event: tk.Event) -> None: |
| 96 | + # The event's delta is two 2-byte signed words packed together. |
| 97 | + delta_x = (event.delta >> 16) & 0xFFFF |
| 98 | + delta_y = (event.delta >> 0) & 0xFFFF |
| 99 | + if delta_x >= 2 ** 15: |
| 100 | + delta_x -= 2 ** 16 |
| 101 | + if delta_y >= 2 ** 15: |
| 102 | + delta_y -= 2 ** 16 |
| 103 | + |
| 104 | + # We get lots of events about tiny movements. Sum them up and only |
| 105 | + # change zoom levels if there have been a bunch all in the same |
| 106 | + # direction. |
| 107 | + self._zoom_touchpad_amount += delta_x + delta_y |
| 108 | + if abs(self._zoom_touchpad_amount) > 20: |
| 109 | + sign = 1 if self._zoom_touchpad_amount > 0 else -1 |
| 110 | + self._zoom_touchpad_amount = 0 |
| 111 | + self._zoom(-sign, event) |
86 | 112 |
|
87 | 113 | def _zoom(self, amount: int, event: tk.Event) -> None:
|
88 | 114 | if not self._pyramid.zoom(amount):
|
89 |
| - return |
| 115 | + return # We're at an extreme level, and didn't actually zoom. |
90 | 116 |
|
91 | 117 | # Otherwise, we changed zoom levels, so adjust everything accordingly.
|
92 | 118 | # We need to move the map so the pixels that started under the mouse are
|
|
0 commit comments