Chromium Code Reviews| Index: chrome/browser/resources/pdf/viewport.js |
| diff --git a/chrome/browser/resources/pdf/viewport.js b/chrome/browser/resources/pdf/viewport.js |
| index 80a5a06ac646bb69e61dcda69a07bfb53a2ea208..ceda5602a2350dff2cf09a7db9969826666cb502 100644 |
| --- a/chrome/browser/resources/pdf/viewport.js |
| +++ b/chrome/browser/resources/pdf/viewport.js |
| @@ -54,7 +54,7 @@ function frameToPluginCoordinate(coordinateInFrame) { |
| * @param {Function} beforeZoomCallback is run before a change in zoom |
| * @param {Function} afterZoomCallback is run after a change in zoom |
| * @param {number} scrollbarWidth the width of scrollbars on the page |
| - * @param {number} defaultZoom The default zoom level. |
| + * @param {number} initialZoom The zoom level to initialize the viewer to. |
|
Sam McNally
2016/12/04 23:56:45
Let's leave this as it was. It's used for deciding
Kevin McNee
2016/12/05 22:21:00
Done.
|
| * @param {number} topToolbarHeight The number of pixels that should initially |
| * be left blank above the document for the toolbar. |
| */ |
| @@ -64,7 +64,7 @@ function Viewport(window, |
| beforeZoomCallback, |
| afterZoomCallback, |
| scrollbarWidth, |
| - defaultZoom, |
| + initialZoom, |
| topToolbarHeight) { |
| this.window_ = window; |
| this.sizer_ = sizer; |
| @@ -72,12 +72,13 @@ function Viewport(window, |
| this.beforeZoomCallback_ = beforeZoomCallback; |
| this.afterZoomCallback_ = afterZoomCallback; |
| this.allowedToChangeZoom_ = false; |
| - this.zoom_ = 1; |
| + this.internalZoom_ = 1; |
| + this.zoomManager_ = new InactiveZoomManager(this, 1); |
| this.documentDimensions_ = null; |
| this.pageDimensions_ = []; |
| this.scrollbarWidth_ = scrollbarWidth; |
| this.fittingType_ = Viewport.FittingType.NONE; |
| - this.defaultZoom_ = defaultZoom; |
| + this.initialZoom_ = initialZoom; |
| this.topToolbarHeight_ = topToolbarHeight; |
| this.prevScale_ = 1; |
| this.pinchPhase_ = Viewport.PinchPhase.PINCH_NONE; |
| @@ -199,7 +200,7 @@ Viewport.prototype = { |
| * respectively. |
| */ |
| documentHasScrollbars: function() { |
| - return this.documentNeedsScrollbars_(this.zoom_); |
| + return this.documentNeedsScrollbars_(this.zoom); |
| }, |
| /** |
| @@ -207,7 +208,7 @@ Viewport.prototype = { |
| * Helper function called when the zoomed document size changes. |
| */ |
| contentSizeChanged_: function() { |
| - var zoomedDimensions = this.getZoomedDocumentDimensions_(this.zoom_); |
| + var zoomedDimensions = this.getZoomedDocumentDimensions_(this.zoom); |
| if (zoomedDimensions) { |
| this.sizer_.style.width = zoomedDimensions.width + 'px'; |
| this.sizer_.style.height = zoomedDimensions.height + |
| @@ -258,7 +259,7 @@ Viewport.prototype = { |
| * @type {Object} the size of the viewport excluding scrollbars. |
| */ |
| get size() { |
| - var needsScrollbars = this.documentNeedsScrollbars_(this.zoom_); |
| + var needsScrollbars = this.documentNeedsScrollbars_(this.zoom); |
| var scrollbarWidth = needsScrollbars.vertical ? this.scrollbarWidth_ : 0; |
| var scrollbarHeight = needsScrollbars.horizontal ? this.scrollbarWidth_ : 0; |
| return { |
| @@ -271,7 +272,15 @@ Viewport.prototype = { |
| * @type {number} the zoom level of the viewport. |
| */ |
| get zoom() { |
| - return this.zoom_; |
| + return this.zoomManager_.applyBrowserZoom(this.internalZoom_); |
| + }, |
| + |
| + /** |
| + * Set the zoom manager. |
| + * @type {ZoomManager} manager the zoom manager to set. |
| + */ |
| + set zoomManager(manager) { |
| + this.zoomManager_ = manager; |
| }, |
| /** |
| @@ -324,15 +333,15 @@ Viewport.prototype = { |
| } |
| // Record the scroll position (relative to the top-left of the window). |
| var currentScrollPos = { |
| - x: this.position.x / this.zoom_, |
| - y: this.position.y / this.zoom_ |
| + x: this.position.x / this.zoom, |
| + y: this.position.y / this.zoom |
| }; |
| - this.zoom_ = newZoom; |
| + this.internalZoom_ = newZoom; |
| this.contentSizeChanged_(); |
| // Scroll to the scaled scroll position. |
| this.position = { |
| - x: currentScrollPos.x * newZoom, |
| - y: currentScrollPos.y * newZoom |
| + x: currentScrollPos.x * this.zoom, |
| + y: currentScrollPos.y * this.zoom |
| }; |
| }, |
| @@ -347,7 +356,7 @@ Viewport.prototype = { |
| assert(this.allowedToChangeZoom_, |
| 'Called Viewport.setPinchZoomInternal_ without calling ' + |
| 'Viewport.mightZoom_.'); |
| - this.zoom_ = clampScale(this.zoom_ * scaleDelta); |
| + this.internalZoom_ = clampScale(this.internalZoom_ * scaleDelta); |
| var newCenterInContent = this.frameToContent(center); |
| var delta = { |
| @@ -357,8 +366,8 @@ Viewport.prototype = { |
| // Record the scroll position (relative to the pinch center). |
| var currentScrollPos = { |
| - x: this.position.x - delta.x * this.zoom_, |
| - y: this.position.y - delta.y * this.zoom_ |
| + x: this.position.x - delta.x * this.zoom, |
| + y: this.position.y - delta.y * this.zoom |
| }; |
| this.contentSizeChanged_(); |
| @@ -379,8 +388,8 @@ Viewport.prototype = { |
| // TODO(mcnee) Add a helper Point class to avoid duplicating operations |
| // on plain {x,y} objects. |
| return { |
| - x: (framePoint.x + this.position.x) / this.zoom_, |
| - y: (framePoint.y + this.position.y) / this.zoom_ |
| + x: (framePoint.x + this.position.x) / this.zoom, |
| + y: (framePoint.y + this.position.y) / this.zoom |
| }; |
| }, |
| @@ -399,6 +408,29 @@ Viewport.prototype = { |
| }, |
| /** |
| + * Gets notified of the browser zoom changing seperately from the |
| + * internal zoom. |
| + * @param {number} oldBrowserZoom the previous value of the browser zoom. |
| + */ |
| + updateZoomFromBrowserChange: function(oldBrowserZoom) { |
| + this.mightZoom_(function() { |
| + // Record the scroll position (relative to the top-left of the window). |
| + var oldZoom = oldBrowserZoom * this.internalZoom_; |
| + var currentScrollPos = { |
| + x: this.position.x / oldZoom, |
| + y: this.position.y / oldZoom |
| + }; |
| + this.contentSizeChanged_(); |
| + // Scroll to the scaled scroll position. |
| + this.position = { |
| + x: currentScrollPos.x * this.zoom, |
| + y: currentScrollPos.y * this.zoom |
| + }; |
| + this.updateViewport_(); |
| + }.bind(this)); |
| + }, |
| + |
| + /** |
| * @type {number} the width of scrollbars in the viewport in pixels. |
| */ |
| get scrollbarWidth() { |
| @@ -448,15 +480,15 @@ Viewport.prototype = { |
| * @return {int} the index of the most visible page. |
| */ |
| getMostVisiblePage: function() { |
| - var firstVisiblePage = this.getPageAtY_(this.position.y / this.zoom_); |
| + var firstVisiblePage = this.getPageAtY_(this.position.y / this.zoom); |
| if (firstVisiblePage == this.pageDimensions_.length - 1) |
| return firstVisiblePage; |
| var viewportRect = { |
| - x: this.position.x / this.zoom_, |
| - y: this.position.y / this.zoom_, |
| - width: this.size.width / this.zoom_, |
| - height: this.size.height / this.zoom_ |
| + x: this.position.x / this.zoom, |
| + y: this.position.y / this.zoom, |
| + width: this.size.width / this.zoom, |
| + height: this.size.height / this.zoom |
| }; |
| var firstVisiblePageVisibility = getIntersectionHeight( |
| this.pageDimensions_[firstVisiblePage], viewportRect) / |
| @@ -477,7 +509,7 @@ Viewport.prototype = { |
| * @param {Object} pageDimensions the dimensions of a given page |
| * @param {boolean} widthOnly a bool indicating whether fit-to-page or |
| * fit-to-width should be computed. |
| - * @return {number} the zoom to use |
| + * @return {number} the internal zoom to set |
| */ |
| computeFittingZoom_: function(pageDimensions, widthOnly) { |
| // First compute the zoom without scrollbars. |
| @@ -528,7 +560,7 @@ Viewport.prototype = { |
| zoomHeight = windowWithScrollbars.height / pageDimensions.height; |
| zoom = Math.min(zoomWidth, zoomHeight); |
| } |
| - return zoom; |
| + return this.zoomManager_.internalZoomComponent(zoom); |
| }, |
| /** |
| @@ -570,7 +602,7 @@ Viewport.prototype = { |
| if (scrollToTopOfPage) { |
| this.position = { |
| x: 0, |
| - y: this.pageDimensions_[page].y * this.zoom_ |
| + y: this.pageDimensions_[page].y * this.zoom |
| }; |
| } |
| this.updateViewport_(); |
| @@ -593,7 +625,7 @@ Viewport.prototype = { |
| this.fittingType_ = Viewport.FittingType.NONE; |
| var nextZoom = Viewport.ZOOM_FACTORS[0]; |
| for (var i = 0; i < Viewport.ZOOM_FACTORS.length; i++) { |
| - if (Viewport.ZOOM_FACTORS[i] < this.zoom_) |
| + if (Viewport.ZOOM_FACTORS[i] < this.internalZoom_) |
| nextZoom = Viewport.ZOOM_FACTORS[i]; |
| } |
| this.setZoomInternal_(nextZoom); |
| @@ -609,7 +641,7 @@ Viewport.prototype = { |
| this.fittingType_ = Viewport.FittingType.NONE; |
| var nextZoom = Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1]; |
| for (var i = Viewport.ZOOM_FACTORS.length - 1; i >= 0; i--) { |
| - if (Viewport.ZOOM_FACTORS[i] > this.zoom_) |
| + if (Viewport.ZOOM_FACTORS[i] > this.internalZoom_) |
| nextZoom = Viewport.ZOOM_FACTORS[i]; |
| } |
| this.setZoomInternal_(nextZoom); |
| @@ -632,7 +664,8 @@ Viewport.prototype = { |
| vectorDelta(e.center, this.firstPinchCenterInFrame_); |
| var needsScrollbars = this.documentNeedsScrollbars_( |
| - clampScale(this.zoom_ * scaleDelta)); |
| + this.zoomManager_.applyBrowserZoom( |
| + clampScale(this.internalZoom_ * scaleDelta))); |
| this.pinchCenter_ = e.center; |
| @@ -666,7 +699,7 @@ Viewport.prototype = { |
| this.oldCenterInContent = |
| this.frameToContent(frameToPluginCoordinate(e.center)); |
| - var needsScrollbars = this.documentNeedsScrollbars_(this.zoom_); |
| + var needsScrollbars = this.documentNeedsScrollbars_(this.zoom); |
| this.keepContentCentered_ = !needsScrollbars.horizontal; |
| // We keep track of begining of the pinch. |
| // By doing so we will be able to compute the pan distance. |
| @@ -710,8 +743,8 @@ Viewport.prototype = { |
| if (this.fittingType_ != Viewport.FittingType.FIT_TO_PAGE) |
| toolbarOffset = this.topToolbarHeight_; |
| this.position = { |
| - x: dimensions.x * this.zoom_, |
| - y: dimensions.y * this.zoom_ - toolbarOffset |
| + x: dimensions.x * this.zoom, |
| + y: dimensions.y * this.zoom - toolbarOffset |
| }; |
| this.updateViewport_(); |
| }.bind(this)); |
| @@ -728,7 +761,7 @@ Viewport.prototype = { |
| this.pageDimensions_ = this.documentDimensions_.pageDimensions; |
| if (initialDimensions) { |
| this.setZoomInternal_( |
| - Math.min(this.defaultZoom_, |
| + Math.min(this.initialZoom_, |
| this.computeFittingZoom_(this.documentDimensions_, true))); |
| this.position = { |
| x: 0, |
| @@ -778,14 +811,14 @@ Viewport.prototype = { |
| // Compute the space on the left of the document if the document fits |
| // completely in the screen. |
| var spaceOnLeft = (this.size.width - |
| - this.documentDimensions_.width * this.zoom_) / 2; |
| + this.documentDimensions_.width * this.zoom) / 2; |
| spaceOnLeft = Math.max(spaceOnLeft, 0); |
| return { |
| - x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset, |
| - y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset, |
| - width: insetDimensions.width * this.zoom_, |
| - height: insetDimensions.height * this.zoom_ |
| + x: x * this.zoom + spaceOnLeft - this.window_.pageXOffset, |
| + y: insetDimensions.y * this.zoom - this.window_.pageYOffset, |
| + width: insetDimensions.width * this.zoom, |
| + height: insetDimensions.height * this.zoom |
| }; |
| } |
| }; |