| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A class that manages updating the browser with zoom changes. | 8 * Abstract parent of classes that manage updating the browser |
| 9 * with zoom changes and/or updating the viewer's zoom when |
| 10 * the browser zoom changes. |
| 9 */ | 11 */ |
| 10 class ZoomManager { | 12 class ZoomManager { |
| 11 /** | 13 /** |
| 12 * Constructs a ZoomManager | 14 * Constructs a ZoomManager. |
| 15 * @param {!Viewport} viewport A Viewport for which to manage zoom. |
| 16 * @param {number} initialZoom The initial browser zoom level. |
| 17 */ |
| 18 constructor(viewport, initialZoom) { |
| 19 if (this.constructor === ZoomManager) { |
| 20 throw new TypeError('Instantiated abstract class: ZoomManager'); |
| 21 } |
| 22 this.viewport_ = viewport; |
| 23 this.browserZoom_ = initialZoom; |
| 24 } |
| 25 |
| 26 /** |
| 27 * Creates the appropriate kind of zoom manager given the zoom behavior. |
| 28 * @param {BrowserApi.ZoomBehavior} zoomBehavior How to manage zoom. |
| 29 * @param {!Viewport} viewport A Viewport for which to manage zoom. |
| 30 * @param {Function} setBrowserZoomFunction A function that sets the browser |
| 31 * zoom to the provided value. |
| 32 * @param {number} initialZoom The initial browser zoom level. |
| 33 */ |
| 34 static create(zoomBehavior, viewport, setBrowserZoomFunction, initialZoom) { |
| 35 switch (zoomBehavior) { |
| 36 case BrowserApi.ZoomBehavior.MANAGE: |
| 37 return new ActiveZoomManager( |
| 38 viewport, setBrowserZoomFunction, initialZoom); |
| 39 case BrowserApi.ZoomBehavior.PROPAGATE_PARENT: |
| 40 return new EmbeddedZoomManager(viewport, initialZoom); |
| 41 default: |
| 42 return new InactiveZoomManager(viewport, initialZoom); |
| 43 } |
| 44 } |
| 45 |
| 46 /** |
| 47 * Invoked when a browser-initiated zoom-level change occurs. |
| 48 * @param {number} newZoom the zoom level to zoom to. |
| 49 */ |
| 50 onBrowserZoomChange(newZoom) {} |
| 51 |
| 52 /** |
| 53 * Invoked when an extension-initiated zoom-level change occurs. |
| 54 */ |
| 55 onPdfZoomChange() {} |
| 56 |
| 57 /** |
| 58 * Combines the internal pdf zoom and the browser zoom to |
| 59 * produce the total zoom level for the viewer. |
| 60 * @param {number} internalZoom the zoom level internal to the viewer. |
| 61 * @return {number} the total zoom level. |
| 62 */ |
| 63 applyBrowserZoom(internalZoom) { |
| 64 return this.browserZoom_ * internalZoom; |
| 65 } |
| 66 |
| 67 /** |
| 68 * Given a zoom level, return the internal zoom level needed to |
| 69 * produce that zoom level. |
| 70 * @param {number} totalZoom the total zoom level. |
| 71 * @return {number} the zoom level internal to the viewer. |
| 72 */ |
| 73 internalZoomComponent(totalZoom) { |
| 74 return totalZoom / this.browserZoom_; |
| 75 } |
| 76 |
| 77 /** |
| 78 * Returns whether two numbers are approximately equal. |
| 79 * @param {number} a The first number. |
| 80 * @param {number} b The second number. |
| 81 */ |
| 82 floatingPointEquals(a, b) { |
| 83 let MIN_ZOOM_DELTA = 0.01; |
| 84 // If the zoom level is close enough to the current zoom level, don't |
| 85 // change it. This avoids us getting into an infinite loop of zoom changes |
| 86 // due to floating point error. |
| 87 return Math.abs(a - b) <= MIN_ZOOM_DELTA; |
| 88 } |
| 89 }; |
| 90 |
| 91 /** |
| 92 * InactiveZoomManager has no control over the browser's zoom |
| 93 * and does not respond to browser zoom changes. |
| 94 */ |
| 95 class InactiveZoomManager extends ZoomManager {}; |
| 96 |
| 97 /** |
| 98 * ActiveZoomManager controls the browser's zoom. |
| 99 */ |
| 100 class ActiveZoomManager extends ZoomManager { |
| 101 /** |
| 102 * Constructs a ActiveZoomManager. |
| 13 * @param {!Viewport} viewport A Viewport for which to manage zoom. | 103 * @param {!Viewport} viewport A Viewport for which to manage zoom. |
| 14 * @param {Function} setBrowserZoomFunction A function that sets the browser | 104 * @param {Function} setBrowserZoomFunction A function that sets the browser |
| 15 * zoom to the provided value. | 105 * zoom to the provided value. |
| 16 * @param {number} initialZoom The initial browser zoom level. | 106 * @param {number} initialZoom The initial browser zoom level. |
| 17 */ | 107 */ |
| 18 constructor(viewport, setBrowserZoomFunction, initialZoom) { | 108 constructor(viewport, setBrowserZoomFunction, initialZoom) { |
| 19 this.viewport_ = viewport; | 109 super(viewport, initialZoom); |
| 20 this.setBrowserZoomFunction_ = setBrowserZoomFunction; | 110 this.setBrowserZoomFunction_ = setBrowserZoomFunction; |
| 21 this.browserZoom_ = initialZoom; | |
| 22 this.changingBrowserZoom_ = null; | 111 this.changingBrowserZoom_ = null; |
| 23 } | 112 } |
| 24 | 113 |
| 25 /** | 114 /** |
| 26 * Invoked when a browser-initiated zoom-level change occurs. | 115 * Invoked when a browser-initiated zoom-level change occurs. |
| 27 * @param {number} newZoom the zoom level to zoom to. | 116 * @param {number} newZoom the zoom level to zoom to. |
| 28 */ | 117 */ |
| 29 onBrowserZoomChange(newZoom) { | 118 onBrowserZoomChange(newZoom) { |
| 30 // If we are changing the browser zoom level, ignore any browser zoom level | 119 // If we are changing the browser zoom level, ignore any browser zoom level |
| 31 // change events. Either, the change occurred before our update and will be | 120 // change events. Either, the change occurred before our update and will be |
| (...skipping 30 matching lines...) Expand all Loading... |
| 62 this.changingBrowserZoom_ = null; | 151 this.changingBrowserZoom_ = null; |
| 63 | 152 |
| 64 // The extension's zoom level may have changed while the browser zoom | 153 // The extension's zoom level may have changed while the browser zoom |
| 65 // change was in progress. We call back into onPdfZoomChange to ensure the | 154 // change was in progress. We call back into onPdfZoomChange to ensure the |
| 66 // browser zoom is up to date. | 155 // browser zoom is up to date. |
| 67 this.onPdfZoomChange(); | 156 this.onPdfZoomChange(); |
| 68 }.bind(this)); | 157 }.bind(this)); |
| 69 } | 158 } |
| 70 | 159 |
| 71 /** | 160 /** |
| 72 * Returns whether two numbers are approximately equal. | 161 * Combines the internal pdf zoom and the browser zoom to |
| 73 * @param {number} a The first number. | 162 * produce the total zoom level for the viewer. |
| 74 * @param {number} b The second number. | 163 * @param {number} internalZoom the zoom level internal to the viewer. |
| 164 * @return {number} the total zoom level. |
| 75 */ | 165 */ |
| 76 floatingPointEquals(a, b) { | 166 applyBrowserZoom(internalZoom) { |
| 77 let MIN_ZOOM_DELTA = 0.01; | 167 // The internal zoom and browser zoom are changed together, so the |
| 78 // If the zoom level is close enough to the current zoom level, don't | 168 // browser zoom is already applied. |
| 79 // change it. This avoids us getting into an infinite loop of zoom changes | 169 return internalZoom; |
| 80 // due to floating point error. | 170 } |
| 81 return Math.abs(a - b) <= MIN_ZOOM_DELTA; | 171 |
| 172 /** |
| 173 * Given a zoom level, return the internal zoom level needed to |
| 174 * produce that zoom level. |
| 175 * @param {number} totalZoom the total zoom level. |
| 176 * @return {number} the zoom level internal to the viewer. |
| 177 */ |
| 178 internalZoomComponent(totalZoom) { |
| 179 // The internal zoom and browser zoom are changed together, so the |
| 180 // internal zoom is the total zoom. |
| 181 return totalZoom; |
| 82 } | 182 } |
| 83 }; | 183 }; |
| 184 |
| 185 /** |
| 186 * This EmbeddedZoomManager responds to changes in the browser zoom, |
| 187 * but does not control the browser zoom. |
| 188 */ |
| 189 class EmbeddedZoomManager extends ZoomManager { |
| 190 /** |
| 191 * Invoked when a browser-initiated zoom-level change occurs. |
| 192 * @param {number} newZoom the new browser zoom level. |
| 193 */ |
| 194 onBrowserZoomChange(newZoom) { |
| 195 let oldZoom = this.browserZoom_; |
| 196 this.browserZoom_ = newZoom; |
| 197 this.viewport_.updateZoomFromBrowserChange(oldZoom); |
| 198 } |
| 199 }; |
| OLD | NEW |