Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1392361 - Fix zooming sensitivity on macOS #12203

Merged
merged 1 commit into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
MAX_SCALE,
MIN_SCALE,
noContextMenuHandler,
normalizeWheelEventDelta,
normalizeWheelEventDirection,
parseQueryString,
PresentationModeState,
ProgressBar,
Expand Down Expand Up @@ -237,6 +237,7 @@ const PDFViewerApplication = {
contentDispositionFilename: null,
triggerDelayedFallback: null,
_saveInProgress: false,
_wheelUnusedTicks: 0,

// Called once when the document is loaded.
async initialize(appConfig) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
8 changes: 7 additions & 1 deletion web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1017,6 +1022,7 @@ export {
scrollIntoView,
watchScroll,
binarySearchFirstItem,
normalizeWheelEventDirection,
normalizeWheelEventDelta,
animationStarted,
WaitOnType,
Expand Down