Merge pull request #15891 from Snuffleupagus/accumulateTicks

Reduce duplication in the `accumulateWheelTicks`/`accumulateTouchTicks` methods (PR 15886 follow-up)
This commit is contained in:
Jonas Jenwald 2023-01-04 19:30:47 +01:00 committed by GitHub
commit 1e1a2fce49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2042,31 +2042,14 @@ const PDFViewerApplication = {
_boundEvents.windowUpdateFromSandbox = null; _boundEvents.windowUpdateFromSandbox = null;
}, },
accumulateWheelTicks(ticks) { _accumulateTicks(ticks, prop) {
// If the scroll direction changed, reset the accumulated wheel ticks. // If the direction changed, reset the accumulated ticks.
if ( if ((this[prop] > 0 && ticks < 0) || (this[prop] < 0 && ticks > 0)) {
(this._wheelUnusedTicks > 0 && ticks < 0) || this[prop] = 0;
(this._wheelUnusedTicks < 0 && ticks > 0)
) {
this._wheelUnusedTicks = 0;
} }
this._wheelUnusedTicks += ticks; this[prop] += ticks;
const wholeTicks = Math.trunc(this._wheelUnusedTicks); const wholeTicks = Math.trunc(this[prop]);
this._wheelUnusedTicks -= wholeTicks; this[prop] -= wholeTicks;
return wholeTicks;
},
accumulateTouchTicks(ticks) {
// If the scroll direction changed, reset the accumulated touch ticks.
if (
(this._touchUnusedTicks > 0 && ticks < 0) ||
(this._touchUnusedTicks < 0 && ticks > 0)
) {
this._touchUnusedTicks = 0;
}
this._touchUnusedTicks += ticks;
const wholeTicks = Math.trunc(this._touchUnusedTicks);
this._touchUnusedTicks -= wholeTicks;
return wholeTicks; return wholeTicks;
}, },
@ -2679,13 +2662,17 @@ function webViewerWheel(evt) {
} else { } else {
// If we're getting fractional lines (I can't think of a scenario // If we're getting fractional lines (I can't think of a scenario
// this might actually happen), be safe and use the accumulator. // this might actually happen), be safe and use the accumulator.
ticks = PDFViewerApplication.accumulateWheelTicks(delta); ticks = PDFViewerApplication._accumulateTicks(
delta,
"_wheelUnusedTicks"
);
} }
} else { } else {
// pixel-based devices // pixel-based devices
const PIXELS_PER_LINE_SCALE = 30; const PIXELS_PER_LINE_SCALE = 30;
ticks = PDFViewerApplication.accumulateWheelTicks( ticks = PDFViewerApplication._accumulateTicks(
delta / PIXELS_PER_LINE_SCALE delta / PIXELS_PER_LINE_SCALE,
"_wheelUnusedTicks"
); );
} }
@ -2775,8 +2762,10 @@ function webViewerTouchMove(evt) {
} }
} else { } else {
const PIXELS_PER_LINE_SCALE = 30; const PIXELS_PER_LINE_SCALE = 30;
const delta = (distance - previousDistance) / PIXELS_PER_LINE_SCALE; const ticks = PDFViewerApplication._accumulateTicks(
const ticks = PDFViewerApplication.accumulateTouchTicks(delta); (distance - previousDistance) / PIXELS_PER_LINE_SCALE,
"_touchUnusedTicks"
);
if (ticks < 0) { if (ticks < 0) {
PDFViewerApplication.zoomOut(-ticks); PDFViewerApplication.zoomOut(-ticks);
} else if (ticks > 0) { } else if (ticks > 0) {