Skip to content

Commit 213676b

Browse files
committed
Bug 1392361 - Fix zooming sensitivity on macOS
The original code would get a long sequence of miniscule "tick" values while pinch-zooming, and each tick value would cause a 1.1x zoom. So even the smallest pinch gesture on a trackpad would cause high amounts of zoom. This patch accumulates the wheel deltas until they reach an integer threshold (with a tweak of the scaling factor to make it feel more natural) at which point it triggers the zoom based on the integer component of the accumulated delta. The fractional part is retained in the accumulator.
1 parent 7edc5cb commit 213676b

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

web/app.js

+43-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
MAX_SCALE,
2727
MIN_SCALE,
2828
noContextMenuHandler,
29-
normalizeWheelEventDelta,
29+
normalizeWheelEventDirection,
3030
parseQueryString,
3131
PresentationModeState,
3232
ProgressBar,
@@ -237,6 +237,7 @@ const PDFViewerApplication = {
237237
contentDispositionFilename: null,
238238
triggerDelayedFallback: null,
239239
_saveInProgress: false,
240+
_wheelUnusedTicks: 0,
240241

241242
// Called once when the document is loaded.
242243
async initialize(appConfig) {
@@ -1835,6 +1836,22 @@ const PDFViewerApplication = {
18351836
_boundEvents.windowBeforePrint = null;
18361837
_boundEvents.windowAfterPrint = null;
18371838
},
1839+
1840+
accumulateWheelTicks(ticks) {
1841+
// If the scroll direction changed, reset the accumulated wheel ticks.
1842+
if (
1843+
(this._wheelUnusedTicks > 0 && ticks < 0) ||
1844+
(this._wheelUnusedTicks < 0 && ticks > 0)
1845+
) {
1846+
this._wheelUnusedTicks = 0;
1847+
}
1848+
this._wheelUnusedTicks += ticks;
1849+
const wholeTicks =
1850+
Math.sign(this._wheelUnusedTicks) *
1851+
Math.floor(Math.abs(this._wheelUnusedTicks));
1852+
this._wheelUnusedTicks -= wholeTicks;
1853+
return wholeTicks;
1854+
},
18381855
};
18391856

18401857
let validateFileURL;
@@ -2492,13 +2509,34 @@ function webViewerWheel(evt) {
24922509

24932510
const previousScale = pdfViewer.currentScale;
24942511

2495-
const delta = normalizeWheelEventDelta(evt);
2512+
const delta = normalizeWheelEventDirection(evt);
2513+
let ticks = 0;
2514+
if (
2515+
evt.deltaMode === WheelEvent.DOM_DELTA_LINE ||
2516+
evt.deltaMode === WheelEvent.DOM_DELTA_PAGE
2517+
) {
2518+
// For line-based devices, use one tick per event, because different
2519+
// OSs have different defaults for the number lines. But we generally
2520+
// want one "clicky" roll of the wheel (which produces one event) to
2521+
// adjust the zoom by one step.
2522+
if (Math.abs(delta) >= 1) {
2523+
ticks = Math.sign(delta);
2524+
} else {
2525+
// If we're getting fractional lines (I can't think of a scenario
2526+
// this might actually happen), be safe and use the accumulator.
2527+
ticks = PDFViewerApplication.accumulateWheelTicks(delta);
2528+
}
2529+
} else {
2530+
// pixel-based devices
2531+
const PIXELS_PER_LINE_SCALE = 30;
2532+
ticks = PDFViewerApplication.accumulateWheelTicks(
2533+
delta / PIXELS_PER_LINE_SCALE
2534+
);
2535+
}
24962536

2497-
const MOUSE_WHEEL_DELTA_PER_PAGE_SCALE = 3.0;
2498-
const ticks = delta * MOUSE_WHEEL_DELTA_PER_PAGE_SCALE;
24992537
if (ticks < 0) {
25002538
PDFViewerApplication.zoomOut(-ticks);
2501-
} else {
2539+
} else if (ticks > 0) {
25022540
PDFViewerApplication.zoomIn(ticks);
25032541
}
25042542

web/ui_utils.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -636,13 +636,18 @@ function getPDFFileNameFromURL(url, defaultFilename = "document.pdf") {
636636
return suggestedFilename || defaultFilename;
637637
}
638638

639-
function normalizeWheelEventDelta(evt) {
639+
function normalizeWheelEventDirection(evt) {
640640
let delta = Math.sqrt(evt.deltaX * evt.deltaX + evt.deltaY * evt.deltaY);
641641
const angle = Math.atan2(evt.deltaY, evt.deltaX);
642642
if (-0.25 * Math.PI < angle && angle < 0.75 * Math.PI) {
643643
// All that is left-up oriented has to change the sign.
644644
delta = -delta;
645645
}
646+
return delta;
647+
}
648+
649+
function normalizeWheelEventDelta(evt) {
650+
let delta = normalizeWheelEventDirection(evt);
646651

647652
const MOUSE_DOM_DELTA_PIXEL_MODE = 0;
648653
const MOUSE_DOM_DELTA_LINE_MODE = 1;
@@ -1017,6 +1022,7 @@ export {
10171022
scrollIntoView,
10181023
watchScroll,
10191024
binarySearchFirstItem,
1025+
normalizeWheelEventDirection,
10201026
normalizeWheelEventDelta,
10211027
animationStarted,
10221028
WaitOnType,

0 commit comments

Comments
 (0)