From 223a18ccc88787b5d673aa8104a1d313ced8979d Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Tue, 27 Sep 2016 15:27:42 -0500 Subject: [PATCH] Simplified wheel processing. --- web/app.js | 37 +++++++++++++++--------------------- web/pdf_presentation_mode.js | 27 +++++++++++++++++--------- web/ui_utils.js | 23 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 31 deletions(-) diff --git a/web/app.js b/web/app.js index 51d50f666..e9f64c231 100644 --- a/web/app.js +++ b/web/app.js @@ -93,6 +93,7 @@ var PDFAttachmentViewer = pdfAttachmentViewerLib.PDFAttachmentViewer; var PDFFindController = pdfFindControllerLib.PDFFindController; var PDFFindBar = pdfFindBarLib.PDFFindBar; var getGlobalEventBus = domEventsLib.getGlobalEventBus; +var normalizeWheelEventDelta = uiUtilsLib.normalizeWheelEventDelta; var DEFAULT_SCALE_DELTA = 1.1; var MIN_SCALE = 0.25; @@ -1217,16 +1218,6 @@ var PDFViewerApplication = { this.pdfPresentationMode.request(); }, - /** - * @param {number} delta - The delta value from the mouse event. - */ - scrollPresentationMode: function pdfViewScrollPresentationMode(delta) { - if (!this.pdfPresentationMode) { - return; - } - this.pdfPresentationMode.mouseScroll(delta); - }, - /** * @typedef UpdateUIToolbarParameters * @property {number} pageNumber @@ -1998,17 +1989,12 @@ function webViewerPageChanging(e) { var zoomDisabled = false, zoomDisabledTimeout; function handleMouseWheel(evt) { - var MOUSE_WHEEL_DELTA_FACTOR = 40; - var ticks = (evt.type === 'DOMMouseScroll') ? -evt.detail : - evt.wheelDelta / MOUSE_WHEEL_DELTA_FACTOR; - var direction = (ticks < 0) ? 'zoomOut' : 'zoomIn'; - var pdfViewer = PDFViewerApplication.pdfViewer; if (pdfViewer.isInPresentationMode) { - evt.preventDefault(); - PDFViewerApplication.scrollPresentationMode(ticks * - MOUSE_WHEEL_DELTA_FACTOR); - } else if (evt.ctrlKey || evt.metaKey) { + return; + } + + if (evt.ctrlKey || evt.metaKey) { var support = PDFViewerApplication.supportedMouseWheelZoomModifierKeys; if ((evt.ctrlKey && !support.ctrlKey) || (evt.metaKey && !support.metaKey)) { @@ -2023,7 +2009,15 @@ function handleMouseWheel(evt) { var previousScale = pdfViewer.currentScale; - PDFViewerApplication[direction](Math.abs(ticks)); + var delta = normalizeWheelEventDelta(evt); + + var MOUSE_WHEEL_DELTA_PER_PAGE_SCALE = 3.0; + var ticks = delta * MOUSE_WHEEL_DELTA_PER_PAGE_SCALE; + if (ticks < 0) { + PDFViewerApplication.zoomOut(-ticks); + } else { + PDFViewerApplication.zoomIn(ticks); + } var currentScale = pdfViewer.currentScale; if (previousScale !== currentScale) { @@ -2046,8 +2040,7 @@ function handleMouseWheel(evt) { } } -window.addEventListener('DOMMouseScroll', handleMouseWheel); -window.addEventListener('mousewheel', handleMouseWheel); +window.addEventListener('wheel', handleMouseWheel); window.addEventListener('click', function click(evt) { if (!PDFViewerApplication.secondaryToolbar.isOpen) { diff --git a/web/pdf_presentation_mode.js b/web/pdf_presentation_mode.js index 50dfe761e..bfc3b535c 100644 --- a/web/pdf_presentation_mode.js +++ b/web/pdf_presentation_mode.js @@ -17,13 +17,15 @@ (function (root, factory) { if (typeof define === 'function' && define.amd) { - define('pdfjs-web/pdf_presentation_mode', ['exports'], factory); + define('pdfjs-web/pdf_presentation_mode', ['exports', 'pdfjs-web/ui_utils'], + factory); } else if (typeof exports !== 'undefined') { - factory(exports); + factory(exports, require('./ui_utils.js')); } else { - factory((root.pdfjsWebPDFPresentationMode = {})); + factory((root.pdfjsWebPDFPresentationMode = {}), root.pdfjsWebUIUtils); } -}(this, function (exports) { +}(this, function (exports, uiUtils) { +var normalizeWheelEventDelta = uiUtils.normalizeWheelEventDelta; var DELAY_BEFORE_RESETTING_SWITCH_IN_PROGRESS = 1500; // in ms var DELAY_BEFORE_HIDING_CONTROLS = 3000; // in ms @@ -120,16 +122,19 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { }, /** - * Switches page when the user scrolls (using a scroll wheel or a touchpad) - * with large enough motion, to prevent accidental page switches. - * @param {number} delta - The delta value from the mouse event. + * @private */ - mouseScroll: function PDFPresentationMode_mouseScroll(delta) { + _mouseWheel: function PDFPresentationMode_mouseWheel(evt) { if (!this.active) { return; } + + evt.preventDefault(); + + var delta = normalizeWheelEventDelta(evt); + var MOUSE_SCROLL_COOLDOWN_TIME = 50; - var PAGE_SWITCH_THRESHOLD = 120; + var PAGE_SWITCH_THRESHOLD = 0.1; var PageSwitchDirection = { UP: -1, DOWN: 1 @@ -340,11 +345,13 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { _addWindowListeners: function PDFPresentationMode_addWindowListeners() { this.showControlsBind = this._showControls.bind(this); this.mouseDownBind = this._mouseDown.bind(this); + this.mouseWheelBind = this._mouseWheel.bind(this); this.resetMouseScrollStateBind = this._resetMouseScrollState.bind(this); this.contextMenuBind = this._contextMenu.bind(this); window.addEventListener('mousemove', this.showControlsBind); window.addEventListener('mousedown', this.mouseDownBind); + window.addEventListener('wheel', this.mouseWheelBind); window.addEventListener('keydown', this.resetMouseScrollStateBind); window.addEventListener('contextmenu', this.contextMenuBind); }, @@ -356,11 +363,13 @@ var PDFPresentationMode = (function PDFPresentationModeClosure() { function PDFPresentationMode_removeWindowListeners() { window.removeEventListener('mousemove', this.showControlsBind); window.removeEventListener('mousedown', this.mouseDownBind); + window.removeEventListener('wheel', this.mouseWheelBind); window.removeEventListener('keydown', this.resetMouseScrollStateBind); window.removeEventListener('contextmenu', this.contextMenuBind); delete this.showControlsBind; delete this.mouseDownBind; + delete this.mouseWheelBind; delete this.resetMouseScrollStateBind; delete this.contextMenuBind; }, diff --git a/web/ui_utils.js b/web/ui_utils.js index 3aba18154..52357d5cf 100644 --- a/web/ui_utils.js +++ b/web/ui_utils.js @@ -384,6 +384,28 @@ function getPDFFileNameFromURL(url) { return suggestedFilename || 'document.pdf'; } +function normalizeWheelEventDelta(evt) { + var delta = Math.sqrt(evt.deltaX * evt.deltaX + evt.deltaY * evt.deltaY); + var 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; + } + + var MOUSE_DOM_DELTA_PIXEL_MODE = 0; + var MOUSE_DOM_DELTA_LINE_MODE = 1; + var MOUSE_PIXELS_PER_LINE = 30; + var MOUSE_LINES_PER_PAGE = 30; + + // Converts delta to per-page units + if (evt.deltaMode === MOUSE_DOM_DELTA_PIXEL_MODE) { + delta /= MOUSE_PIXELS_PER_LINE * MOUSE_LINES_PER_PAGE; + } else if (evt.deltaMode === MOUSE_DOM_DELTA_LINE_MODE) { + delta /= MOUSE_LINES_PER_PAGE; + } + return delta; +} + /** * Simple event bus for an application. Listeners are attached using the * `on` and `off` methods. To raise an event, the `dispatch` method shall be @@ -529,4 +551,5 @@ exports.getOutputScale = getOutputScale; exports.scrollIntoView = scrollIntoView; exports.watchScroll = watchScroll; exports.binarySearchFirstItem = binarySearchFirstItem; +exports.normalizeWheelEventDelta = normalizeWheelEventDelta; }));