Chromium Code Reviews| Index: chrome/browser/resources/pdf/pdf.js |
| diff --git a/chrome/browser/resources/pdf/pdf.js b/chrome/browser/resources/pdf/pdf.js |
| index 9805660b562c34e82cae189e4670abaace68eb8e..2f87a614fcb6b7583bcbf2b63790bed6f60f3809 100644 |
| --- a/chrome/browser/resources/pdf/pdf.js |
| +++ b/chrome/browser/resources/pdf/pdf.js |
| @@ -191,6 +191,46 @@ function PDFViewer(browserApi) { |
| this.zoomToolbar_.addEventListener('zoom-out', |
| this.viewport_.zoomOut.bind(this.viewport_)); |
| + // We add Hammer.js in order to handle touch events like pinch-zoom. |
| + this.hammertime_ = new Hammer($('plugin')); |
| + |
| + // We must preventDefault if there is a two finger touch. By doing so browser |
|
bokan
2016/10/17 22:20:15
Please replace "browser zoom" with "native pinch-z
Kevin McNee - google account
2016/10/24 21:11:47
Done.
|
| + // zoom does not interfere with our way of handling the event. |
| + this.plugin_.addEventListener('touchstart', function(e) { |
| + if (e.touches.length >= 2) { |
| + e.preventDefault(); |
| + this.hammertime_.get('pinch').set({enable: true}); |
| + } |
| + }.bind(this)); |
| + |
| + this.plugin_.addEventListener('touchend', function(e) { |
| + this.hammertime_.get('pinch').set({enable: false}); |
| + }.bind(this)); |
| + |
| + this.hammertime_.on('pinchstart', |
| + this.viewport_.pinchZoomStart.bind(this.viewport_)); |
| + |
| + // Throttle number of pinch events to one per frame. |
| + this.sentPinchEvent_ = false; |
| + |
| + this.hammertime_.on('pinch', function(ev) { |
| + if (!this.sentPinchEvent_) { |
| + this.sentPinchEvent_ = true; |
| + window.requestAnimationFrame(function() { |
| + this.sentPinchEvent_ = false; |
| + this.viewport_.pinchZoom(ev); |
| + }.bind(this)); |
| + } |
| + }.bind(this)); |
| + |
| + this.hammertime_.on('pinchend', function(ev) { |
| + // Using rAF for pinch end prevents pinch updates scheduled by rAF getting |
| + // sent after the pinch end. |
| + window.requestAnimationFrame(function() { |
| + this.viewport_.pinchZoomEnd(ev); |
| + }.bind(this)); |
| + }.bind(this)); |
| + |
| if (toolbarEnabled) { |
| this.toolbar_ = $('toolbar'); |
| this.toolbar_.hidden = false; |
| @@ -659,6 +699,19 @@ PDFViewer.prototype = { |
| this.plugin_.postMessage({ |
| type: 'stopScrolling' |
| }); |
| + |
| + if (this.viewport_.pinchPhase == Viewport.PinchPhase.PINCH_START) { |
| + var position = this.viewport_.position; |
| + var zoom = this.viewport_.zoom; |
| + var pinchPhase = this.viewport_.pinchPhase; |
| + this.plugin_.postMessage({ |
| + type: 'viewport', |
| + zoom: zoom, |
| + xOffset: position.x, |
| + yOffset: position.y, |
| + pinchPhase: pinchPhase |
| + }); |
| + } |
| }, |
| /** |
| @@ -669,11 +722,24 @@ PDFViewer.prototype = { |
| afterZoom_: function() { |
| var position = this.viewport_.position; |
| var zoom = this.viewport_.zoom; |
| + var pinchVector = this.viewport_.pinchPanVector; |
| + var pinchCenter = this.viewport_.pinchCenter; |
| + var pinchPhase = this.viewport_.pinchPhase; |
| + |
| + if (!pinchVector) |
| + pinchVector = {x: 0, y: 0}; |
| + if (!pinchCenter) |
| + pinchCenter = {x: 0, y: 0}; |
| this.plugin_.postMessage({ |
| type: 'viewport', |
| zoom: zoom, |
| xOffset: position.x, |
| - yOffset: position.y |
| + yOffset: position.y, |
| + pinchPhase: pinchPhase, |
| + pinchX: pinchCenter.x, |
| + pinchY: pinchCenter.y, |
| + pinchVectorX: pinchVector.x, |
| + pinchVectorY: pinchVector.y |
| }); |
| this.zoomManager_.onPdfZoomChange(); |
| }, |