Chromium Code Reviews| Index: chrome/browser/resources/pdf/zoom_manager.js |
| diff --git a/chrome/browser/resources/pdf/zoom_manager.js b/chrome/browser/resources/pdf/zoom_manager.js |
| index 2b52c54aba365b95ecfc84ddfbc38ced89971912..c661178dfb958d768fd3c246fd03d0d2966aa714 100644 |
| --- a/chrome/browser/resources/pdf/zoom_manager.js |
| +++ b/chrome/browser/resources/pdf/zoom_manager.js |
| @@ -5,20 +5,109 @@ |
| 'use strict'; |
| /** |
| - * A class that manages updating the browser with zoom changes. |
| + * Abstract parent of classes that manage updating the browser |
| + * with zoom changes and/or updating the viewer's zoom when |
| + * the browser zoom changes. |
| */ |
| class ZoomManager { |
| /** |
| - * Constructs a ZoomManager |
| + * Constructs a ZoomManager. |
| + * @param {!Viewport} viewport A Viewport for which to manage zoom. |
| + * @param {number} initialZoom The initial browser zoom level. |
| + */ |
| + constructor(viewport, initialZoom) { |
| + if (this.constructor === ZoomManager) { |
| + throw new TypeError('Instantiated abstract class: ZoomManager'); |
| + } |
| + this.viewport_ = viewport; |
| + this.browserZoom_ = initialZoom; |
| + } |
| + |
| + /** |
| + * Creates the appropriate kind of zoom manager given the zoom behaviour. |
| + * @param {BrowserApi.ZoomBehaviour} zoomBehaviour How to manage zoom. |
| + * @param {!Viewport} viewport A Viewport for which to manage zoom. |
| + * @param {Function} setBrowserZoomFunction A function that sets the browser |
| + * zoom to the provided value. |
| + * @param {number} initialZoom The initial browser zoom level. |
| + */ |
| + static create(zoomBehaviour, viewport, setBrowserZoomFunction, initialZoom) { |
| + switch (zoomBehaviour) { |
| + case BrowserApi.ZoomBehaviour.MANAGE: |
| + return new ActiveZoomManager( |
| + viewport, setBrowserZoomFunction, initialZoom); |
| + case BrowserApi.ZoomBehaviour.PROPAGATE_PARENT: |
| + return new EmbeddedZoomManager(viewport, initialZoom); |
| + default: |
| + return new InactiveZoomManager(viewport, initialZoom); |
| + } |
| + } |
| + |
| + /** |
| + * Invoked when a browser-initiated zoom-level change occurs. |
| + * @param {number} newZoom the zoom level to zoom to. |
| + */ |
| + onBrowserZoomChange(newZoom) {} |
| + |
| + /** |
| + * Invoked when an extension-initiated zoom-level change occurs. |
| + */ |
| + onPdfZoomChange() {} |
| + |
| + /** |
| + * Combines the internal pdf zoom and the browser zoom to |
| + * produce the total zoom level for the viewer. |
| + * @param {number} internalZoom the zoom level internal to the viewer. |
| + * @return {number} the total zoom level. |
| + */ |
| + applyBrowserZoom(internalZoom) { |
| + return this.browserZoom_ * internalZoom; |
| + } |
| + |
| + /** |
| + * Given a zoom level, return the internal zoom level needed to |
| + * produce that zoom level. |
| + * @param {number} totalZoom the total zoom level. |
| + * @return {number} the zoom level internal to the viewer. |
| + */ |
| + internalZoomComponent(totalZoom) { |
| + return totalZoom / this.browserZoom_; |
| + } |
| + |
| + /** |
| + * Returns whether two numbers are approximately equal. |
| + * @param {number} a The first number. |
| + * @param {number} b The second number. |
| + */ |
| + floatingPointEquals(a, b) { |
| + let MIN_ZOOM_DELTA = 0.01; |
| + // If the zoom level is close enough to the current zoom level, don't |
| + // change it. This avoids us getting into an infinite loop of zoom changes |
| + // due to floating point error. |
| + return Math.abs(a - b) <= MIN_ZOOM_DELTA; |
| + } |
| +}; |
| + |
| +/** |
| + * InactiveZoomManager has no control over the browser's zoom |
| + * and does not respond to browser zoom changes. |
| + */ |
| +class InactiveZoomManager extends ZoomManager {}; |
| + |
| +/** |
| + * ActiveZoomManager controls the browser's zoom. |
| + */ |
| +class ActiveZoomManager extends ZoomManager { |
| + /** |
| + * Constructs a ActiveZoomManager. |
| * @param {!Viewport} viewport A Viewport for which to manage zoom. |
| * @param {Function} setBrowserZoomFunction A function that sets the browser |
| * zoom to the provided value. |
| * @param {number} initialZoom The initial browser zoom level. |
| */ |
| constructor(viewport, setBrowserZoomFunction, initialZoom) { |
| - this.viewport_ = viewport; |
| + super(viewport, initialZoom); |
| this.setBrowserZoomFunction_ = setBrowserZoomFunction; |
| - this.browserZoom_ = initialZoom; |
| this.changingBrowserZoom_ = null; |
| } |
| @@ -65,19 +154,48 @@ class ZoomManager { |
| // change was in progress. We call back into onPdfZoomChange to ensure the |
| // browser zoom is up to date. |
| this.onPdfZoomChange(); |
| + }.bind(this), function() { |
| + this.changingBrowserZoom_ = null; |
|
Sam McNally
2016/12/04 23:56:45
Is this expected to occur?
Kevin McNee
2016/12/05 22:21:00
Ah, this only affected a previous patch. Since the
|
| }.bind(this)); |
| } |
| /** |
| - * Returns whether two numbers are approximately equal. |
| - * @param {number} a The first number. |
| - * @param {number} b The second number. |
| + * Combines the internal pdf zoom and the browser zoom to |
| + * produce the total zoom level for the viewer. |
| + * @param {number} internalZoom the zoom level internal to the viewer. |
| + * @return {number} the total zoom level. |
| */ |
| - floatingPointEquals(a, b) { |
| - let MIN_ZOOM_DELTA = 0.01; |
| - // If the zoom level is close enough to the current zoom level, don't |
| - // change it. This avoids us getting into an infinite loop of zoom changes |
| - // due to floating point error. |
| - return Math.abs(a - b) <= MIN_ZOOM_DELTA; |
| + applyBrowserZoom(internalZoom) { |
| + // The internal zoom and browser zoom are changed together, so the |
| + // browser zoom is already applied. |
| + return internalZoom; |
| + } |
| + |
| + /** |
| + * Given a zoom level, return the internal zoom level needed to |
| + * produce that zoom level. |
| + * @param {number} totalZoom the total zoom level. |
| + * @return {number} the zoom level internal to the viewer. |
| + */ |
| + internalZoomComponent(totalZoom) { |
| + // The internal zoom and browser zoom are changed together, so the |
| + // internal zoom is the total zoom. |
| + return totalZoom; |
| + } |
| +}; |
| + |
| +/** |
| + * This EmbeddedZoomManager responds to changes in the browser zoom, |
| + * but does not control the browser zoom. |
| + */ |
| +class EmbeddedZoomManager extends ZoomManager { |
| + /** |
| + * Invoked when a browser-initiated zoom-level change occurs. |
| + * @param {number} newZoom the new browser zoom level. |
| + */ |
| + onBrowserZoomChange(newZoom) { |
| + let oldZoom = this.browserZoom_; |
| + this.browserZoom_ = newZoom; |
| + this.viewport_.updateZoomFromBrowserChange(oldZoom); |
| } |
| }; |