Skip to content

Commit 3e45abd

Browse files
linesightclaude
andcommitted
fix(examples/pysdl2): scale mouse wheel by deviceScaleFactor for HiDPI
Switch from flat addition to multiplication for scroll deltas so that scroll speed is correct on HiDPI displays: - Use event.wheel.preciseX/Y (SDL >= 2.0.18) for smooth-scroll precision; integer x/y truncates sub-pixel Wayland events to 0 causing phantom 40-px scrolls per sub-pixel event with the old adder approach - Multiply delta by scrollEnhance * deviceScaleFactor — at 2× scaling each notch sends 80 physical pixels matching native browser behaviour; at 1× the factor is 1.0 so nothing changes - y=0 events now correctly send 0px instead of ±scrollEnhance Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 2aff2c9 commit 3e45abd

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

examples/pysdl2.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ def main():
156156
headerHeight = 0
157157
browserHeight = height - headerHeight
158158
browserWidth = width
159-
# Mouse wheel fudge to enhance scrolling
159+
# CSS pixels per wheel unit. Used as a multiplier (not an adder) so that
160+
# smooth-scroll sub-pixel events scale proportionally and y=0 events send
161+
# nothing. Multiplied by deviceScaleFactor at event time so the perceived
162+
# scroll distance matches a native browser at any DPI.
160163
scrollEnhance = 40
161164
# desired frame rate
162165
frameRate = 100
@@ -370,18 +373,20 @@ def _detect_device_scale_factor(window, renderer):
370373
width, height, deviceScaleFactor)
371374
elif event.type == sdl2.SDL_MOUSEWHEEL:
372375
logging.debug("SDL2 MOUSEWHEEL event")
373-
# Mouse wheel event
374-
x = event.wheel.x
375-
if x < 0:
376-
x -= scrollEnhance
377-
else:
378-
x += scrollEnhance
379-
y = event.wheel.y
380-
if y < 0:
381-
y -= scrollEnhance
382-
else:
383-
y += scrollEnhance
384-
browser.SendMouseWheelEvent(0, 0, x, y)
376+
# Use sub-pixel precision when available (SDL >= 2.0.18).
377+
# Multiply by deviceScaleFactor: SendMouseWheelEvent takes
378+
# physical pixels, so at 2x a logical-unit delta must be
379+
# doubled to move the same apparent distance as in a native
380+
# browser.
381+
try:
382+
dx = event.wheel.preciseX
383+
dy = event.wheel.preciseY
384+
except AttributeError:
385+
dx = float(event.wheel.x)
386+
dy = float(event.wheel.y)
387+
scale = scrollEnhance * deviceScaleFactor
388+
browser.SendMouseWheelEvent(
389+
0, 0, int(dx * scale), int(dy * scale))
385390
elif event.type == sdl2.SDL_TEXTINPUT:
386391
# Handle text events to get actual characters typed rather
387392
# than the key pressed.

0 commit comments

Comments
 (0)