From 213676b5e8f5981a7cba6b736370d18b63ace93b Mon Sep 17 00:00:00 2001 From: Kartikaya Gupta Date: Wed, 12 Aug 2020 14:30:45 -0400 Subject: [PATCH] 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. --- web/app.js | 48 +++++++++++++++++++++++++++++++++++++++++++----- web/ui_utils.js | 8 +++++++- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/web/app.js b/web/app.js index d568e049bf3cf..f0eba37195fb0 100644 --- a/web/app.js +++ b/web/app.js @@ -26,7 +26,7 @@ import { MAX_SCALE, MIN_SCALE, noContextMenuHandler, - normalizeWheelEventDelta, + normalizeWheelEventDirection, parseQueryString, PresentationModeState, ProgressBar, @@ -237,6 +237,7 @@ const PDFViewerApplication = { contentDispositionFilename: null, triggerDelayedFallback: null, _saveInProgress: false, + _wheelUnusedTicks: 0, // Called once when the document is loaded. async initialize(appConfig) { @@ -1835,6 +1836,22 @@ const PDFViewerApplication = { _boundEvents.windowBeforePrint = null; _boundEvents.windowAfterPrint = null; }, + + accumulateWheelTicks(ticks) { + // If the scroll direction changed, reset the accumulated wheel ticks. + if ( + (this._wheelUnusedTicks > 0 && ticks < 0) || + (this._wheelUnusedTicks < 0 && ticks > 0) + ) { + this._wheelUnusedTicks = 0; + } + this._wheelUnusedTicks += ticks; + const wholeTicks = + Math.sign(this._wheelUnusedTicks) * + Math.floor(Math.abs(this._wheelUnusedTicks)); + this._wheelUnusedTicks -= wholeTicks; + return wholeTicks; + }, }; let validateFileURL; @@ -2492,13 +2509,34 @@ function webViewerWheel(evt) { const previousScale = pdfViewer.currentScale; - const delta = normalizeWheelEventDelta(evt); + const delta = normalizeWheelEventDirection(evt); + let ticks = 0; + if ( + evt.deltaMode === WheelEvent.DOM_DELTA_LINE || + evt.deltaMode === WheelEvent.DOM_DELTA_PAGE + ) { + // For line-based devices, use one tick per event, because different + // OSs have different defaults for the number lines. But we generally + // want one "clicky" roll of the wheel (which produces one event) to + // adjust the zoom by one step. + if (Math.abs(delta) >= 1) { + ticks = Math.sign(delta); + } else { + // If we're getting fractional lines (I can't think of a scenario + // this might actually happen), be safe and use the accumulator. + ticks = PDFViewerApplication.accumulateWheelTicks(delta); + } + } else { + // pixel-based devices + const PIXELS_PER_LINE_SCALE = 30; + ticks = PDFViewerApplication.accumulateWheelTicks( + delta / PIXELS_PER_LINE_SCALE + ); + } - const MOUSE_WHEEL_DELTA_PER_PAGE_SCALE = 3.0; - const ticks = delta * MOUSE_WHEEL_DELTA_PER_PAGE_SCALE; if (ticks < 0) { PDFViewerApplication.zoomOut(-ticks); - } else { + } else if (ticks > 0) { PDFViewerApplication.zoomIn(ticks); } diff --git a/web/ui_utils.js b/web/ui_utils.js index c24ee99077ec5..3ad6e8ae2cc49 100644 --- a/web/ui_utils.js +++ b/web/ui_utils.js @@ -636,13 +636,18 @@ function getPDFFileNameFromURL(url, defaultFilename = "document.pdf") { return suggestedFilename || defaultFilename; } -function normalizeWheelEventDelta(evt) { +function normalizeWheelEventDirection(evt) { let delta = Math.sqrt(evt.deltaX * evt.deltaX + evt.deltaY * evt.deltaY); const angle = Math.atan2(evt.deltaY, evt.deltaX); if (-0.25 * Math.PI < angle && angle < 0.75 * Math.PI) { // All that is left-up oriented has to change the sign. delta = -delta; } + return delta; +} + +function normalizeWheelEventDelta(evt) { + let delta = normalizeWheelEventDirection(evt); const MOUSE_DOM_DELTA_PIXEL_MODE = 0; const MOUSE_DOM_DELTA_LINE_MODE = 1; @@ -1017,6 +1022,7 @@ export { scrollIntoView, watchScroll, binarySearchFirstItem, + normalizeWheelEventDirection, normalizeWheelEventDelta, animationStarted, WaitOnType,