Skip to content

Commit

Permalink
Use WheelEvent.deltaMode when available and 0
Browse files Browse the repository at this point in the history
Works around a regression on Chrome 94, where non-linear scrolling greatly
confused the wheel unit estimation code.

See https://discuss.codemirror.net/t/scrolling-is-badly-impacted-by-chrome-94-0-4606-61/3567/3
  • Loading branch information
marijnh committed Sep 29, 2021
1 parent 73a5c21 commit 3ddd828
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/display/scroll_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export function wheelEventPixels(e) {

export function onScrollWheel(cm, e) {
let delta = wheelEventDelta(e), dx = delta.x, dy = delta.y
let pixelsPerUnit = wheelPixelsPerUnit
if (event.deltaMode === 0) {
dx = e.deltaX
dy = e.deltaY
pixelsPerUnit = 1
}

let display = cm.display, scroll = display.scroller
// Quit if there's nothing to scroll here
Expand Down Expand Up @@ -69,10 +75,10 @@ export function onScrollWheel(cm, e) {
// estimated pixels/delta value, we just handle horizontal
// scrolling entirely here. It'll be slightly off from native, but
// better than glitching out.
if (dx && !gecko && !presto && wheelPixelsPerUnit != null) {
if (dx && !gecko && !presto && pixelsPerUnit != null) {
if (dy && canScrollY)
updateScrollTop(cm, Math.max(0, scroll.scrollTop + dy * wheelPixelsPerUnit))
setScrollLeft(cm, Math.max(0, scroll.scrollLeft + dx * wheelPixelsPerUnit))
updateScrollTop(cm, Math.max(0, scroll.scrollTop + dy * pixelsPerUnit))
setScrollLeft(cm, Math.max(0, scroll.scrollLeft + dx * pixelsPerUnit))
// Only prevent default scrolling if vertical scrolling is
// actually possible. Otherwise, it causes vertical scroll
// jitter on OSX trackpads when deltaX is small and deltaY
Expand All @@ -85,15 +91,15 @@ export function onScrollWheel(cm, e) {

// 'Project' the visible viewport to cover the area that is being
// scrolled into view (if we know enough to estimate it).
if (dy && wheelPixelsPerUnit != null) {
let pixels = dy * wheelPixelsPerUnit
if (dy && pixelsPerUnit != null) {
let pixels = dy * pixelsPerUnit
let top = cm.doc.scrollTop, bot = top + display.wrapper.clientHeight
if (pixels < 0) top = Math.max(0, top + pixels - 50)
else bot = Math.min(cm.doc.height, bot + pixels + 50)
updateDisplaySimple(cm, {top: top, bottom: bot})
}

if (wheelSamples < 20) {
if (wheelSamples < 20 && e.deltaMode !== 0) {
if (display.wheelStartX == null) {
display.wheelStartX = scroll.scrollLeft; display.wheelStartY = scroll.scrollTop
display.wheelDX = dx; display.wheelDY = dy
Expand Down

0 comments on commit 3ddd828

Please sign in to comment.