Skip to content

Commit a9a3396

Browse files
Merge pull request mozilla#7181 from yurydelendik/mouse-wheel
Simplified wheel processing
2 parents 7f381c8 + 223a18c commit a9a3396

File tree

3 files changed

+56
-31
lines changed

3 files changed

+56
-31
lines changed

web/app.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ var PDFAttachmentViewer = pdfAttachmentViewerLib.PDFAttachmentViewer;
9393
var PDFFindController = pdfFindControllerLib.PDFFindController;
9494
var PDFFindBar = pdfFindBarLib.PDFFindBar;
9595
var getGlobalEventBus = domEventsLib.getGlobalEventBus;
96+
var normalizeWheelEventDelta = uiUtilsLib.normalizeWheelEventDelta;
9697

9798
var DEFAULT_SCALE_DELTA = 1.1;
9899
var MIN_SCALE = 0.25;
@@ -1217,16 +1218,6 @@ var PDFViewerApplication = {
12171218
this.pdfPresentationMode.request();
12181219
},
12191220

1220-
/**
1221-
* @param {number} delta - The delta value from the mouse event.
1222-
*/
1223-
scrollPresentationMode: function pdfViewScrollPresentationMode(delta) {
1224-
if (!this.pdfPresentationMode) {
1225-
return;
1226-
}
1227-
this.pdfPresentationMode.mouseScroll(delta);
1228-
},
1229-
12301221
/**
12311222
* @typedef UpdateUIToolbarParameters
12321223
* @property {number} pageNumber
@@ -1998,17 +1989,12 @@ function webViewerPageChanging(e) {
19981989

19991990
var zoomDisabled = false, zoomDisabledTimeout;
20001991
function handleMouseWheel(evt) {
2001-
var MOUSE_WHEEL_DELTA_FACTOR = 40;
2002-
var ticks = (evt.type === 'DOMMouseScroll') ? -evt.detail :
2003-
evt.wheelDelta / MOUSE_WHEEL_DELTA_FACTOR;
2004-
var direction = (ticks < 0) ? 'zoomOut' : 'zoomIn';
2005-
20061992
var pdfViewer = PDFViewerApplication.pdfViewer;
20071993
if (pdfViewer.isInPresentationMode) {
2008-
evt.preventDefault();
2009-
PDFViewerApplication.scrollPresentationMode(ticks *
2010-
MOUSE_WHEEL_DELTA_FACTOR);
2011-
} else if (evt.ctrlKey || evt.metaKey) {
1994+
return;
1995+
}
1996+
1997+
if (evt.ctrlKey || evt.metaKey) {
20121998
var support = PDFViewerApplication.supportedMouseWheelZoomModifierKeys;
20131999
if ((evt.ctrlKey && !support.ctrlKey) ||
20142000
(evt.metaKey && !support.metaKey)) {
@@ -2023,7 +2009,15 @@ function handleMouseWheel(evt) {
20232009

20242010
var previousScale = pdfViewer.currentScale;
20252011

2026-
PDFViewerApplication[direction](Math.abs(ticks));
2012+
var delta = normalizeWheelEventDelta(evt);
2013+
2014+
var MOUSE_WHEEL_DELTA_PER_PAGE_SCALE = 3.0;
2015+
var ticks = delta * MOUSE_WHEEL_DELTA_PER_PAGE_SCALE;
2016+
if (ticks < 0) {
2017+
PDFViewerApplication.zoomOut(-ticks);
2018+
} else {
2019+
PDFViewerApplication.zoomIn(ticks);
2020+
}
20272021

20282022
var currentScale = pdfViewer.currentScale;
20292023
if (previousScale !== currentScale) {
@@ -2046,8 +2040,7 @@ function handleMouseWheel(evt) {
20462040
}
20472041
}
20482042

2049-
window.addEventListener('DOMMouseScroll', handleMouseWheel);
2050-
window.addEventListener('mousewheel', handleMouseWheel);
2043+
window.addEventListener('wheel', handleMouseWheel);
20512044

20522045
window.addEventListener('click', function click(evt) {
20532046
if (!PDFViewerApplication.secondaryToolbar.isOpen) {

web/pdf_presentation_mode.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
(function (root, factory) {
1919
if (typeof define === 'function' && define.amd) {
20-
define('pdfjs-web/pdf_presentation_mode', ['exports'], factory);
20+
define('pdfjs-web/pdf_presentation_mode', ['exports', 'pdfjs-web/ui_utils'],
21+
factory);
2122
} else if (typeof exports !== 'undefined') {
22-
factory(exports);
23+
factory(exports, require('./ui_utils.js'));
2324
} else {
24-
factory((root.pdfjsWebPDFPresentationMode = {}));
25+
factory((root.pdfjsWebPDFPresentationMode = {}), root.pdfjsWebUIUtils);
2526
}
26-
}(this, function (exports) {
27+
}(this, function (exports, uiUtils) {
28+
var normalizeWheelEventDelta = uiUtils.normalizeWheelEventDelta;
2729

2830
var DELAY_BEFORE_RESETTING_SWITCH_IN_PROGRESS = 1500; // in ms
2931
var DELAY_BEFORE_HIDING_CONTROLS = 3000; // in ms
@@ -120,16 +122,19 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
120122
},
121123

122124
/**
123-
* Switches page when the user scrolls (using a scroll wheel or a touchpad)
124-
* with large enough motion, to prevent accidental page switches.
125-
* @param {number} delta - The delta value from the mouse event.
125+
* @private
126126
*/
127-
mouseScroll: function PDFPresentationMode_mouseScroll(delta) {
127+
_mouseWheel: function PDFPresentationMode_mouseWheel(evt) {
128128
if (!this.active) {
129129
return;
130130
}
131+
132+
evt.preventDefault();
133+
134+
var delta = normalizeWheelEventDelta(evt);
135+
131136
var MOUSE_SCROLL_COOLDOWN_TIME = 50;
132-
var PAGE_SWITCH_THRESHOLD = 120;
137+
var PAGE_SWITCH_THRESHOLD = 0.1;
133138
var PageSwitchDirection = {
134139
UP: -1,
135140
DOWN: 1
@@ -340,11 +345,13 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
340345
_addWindowListeners: function PDFPresentationMode_addWindowListeners() {
341346
this.showControlsBind = this._showControls.bind(this);
342347
this.mouseDownBind = this._mouseDown.bind(this);
348+
this.mouseWheelBind = this._mouseWheel.bind(this);
343349
this.resetMouseScrollStateBind = this._resetMouseScrollState.bind(this);
344350
this.contextMenuBind = this._contextMenu.bind(this);
345351

346352
window.addEventListener('mousemove', this.showControlsBind);
347353
window.addEventListener('mousedown', this.mouseDownBind);
354+
window.addEventListener('wheel', this.mouseWheelBind);
348355
window.addEventListener('keydown', this.resetMouseScrollStateBind);
349356
window.addEventListener('contextmenu', this.contextMenuBind);
350357
},
@@ -356,11 +363,13 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() {
356363
function PDFPresentationMode_removeWindowListeners() {
357364
window.removeEventListener('mousemove', this.showControlsBind);
358365
window.removeEventListener('mousedown', this.mouseDownBind);
366+
window.removeEventListener('wheel', this.mouseWheelBind);
359367
window.removeEventListener('keydown', this.resetMouseScrollStateBind);
360368
window.removeEventListener('contextmenu', this.contextMenuBind);
361369

362370
delete this.showControlsBind;
363371
delete this.mouseDownBind;
372+
delete this.mouseWheelBind;
364373
delete this.resetMouseScrollStateBind;
365374
delete this.contextMenuBind;
366375
},

web/ui_utils.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,28 @@ function getPDFFileNameFromURL(url) {
384384
return suggestedFilename || 'document.pdf';
385385
}
386386

387+
function normalizeWheelEventDelta(evt) {
388+
var delta = Math.sqrt(evt.deltaX * evt.deltaX + evt.deltaY * evt.deltaY);
389+
var angle = Math.atan2(evt.deltaY, evt.deltaX);
390+
if (-0.25 * Math.PI < angle && angle < 0.75 * Math.PI) {
391+
// All that is left-up oriented has to change the sign.
392+
delta = -delta;
393+
}
394+
395+
var MOUSE_DOM_DELTA_PIXEL_MODE = 0;
396+
var MOUSE_DOM_DELTA_LINE_MODE = 1;
397+
var MOUSE_PIXELS_PER_LINE = 30;
398+
var MOUSE_LINES_PER_PAGE = 30;
399+
400+
// Converts delta to per-page units
401+
if (evt.deltaMode === MOUSE_DOM_DELTA_PIXEL_MODE) {
402+
delta /= MOUSE_PIXELS_PER_LINE * MOUSE_LINES_PER_PAGE;
403+
} else if (evt.deltaMode === MOUSE_DOM_DELTA_LINE_MODE) {
404+
delta /= MOUSE_LINES_PER_PAGE;
405+
}
406+
return delta;
407+
}
408+
387409
/**
388410
* Simple event bus for an application. Listeners are attached using the
389411
* `on` and `off` methods. To raise an event, the `dispatch` method shall be
@@ -529,4 +551,5 @@ exports.getOutputScale = getOutputScale;
529551
exports.scrollIntoView = scrollIntoView;
530552
exports.watchScroll = watchScroll;
531553
exports.binarySearchFirstItem = binarySearchFirstItem;
554+
exports.normalizeWheelEventDelta = normalizeWheelEventDelta;
532555
}));

0 commit comments

Comments
 (0)