Chromium Code Reviews| 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 behaviour. | |
| 28 * @param {BrowserApi.ZoomBehaviour} zoomBehaviour 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(zoomBehaviour, viewport, setBrowserZoomFunction, initialZoom) { | |
| 35 switch (zoomBehaviour) { | |
| 36 case BrowserApi.ZoomBehaviour.MANAGE: | |
| 37 return new ActiveZoomManager( | |
| 38 viewport, setBrowserZoomFunction, initialZoom); | |
| 39 case BrowserApi.ZoomBehaviour.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 26 matching lines...) Expand all Loading... | |
| 58 | 147 |
| 59 this.changingBrowserZoom_ = this.setBrowserZoomFunction_(zoom).then( | 148 this.changingBrowserZoom_ = this.setBrowserZoomFunction_(zoom).then( |
| 60 function() { | 149 function() { |
| 61 this.browserZoom_ = zoom; | 150 this.browserZoom_ = zoom; |
| 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(); |
| 157 }.bind(this), function() { | |
| 158 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
| |
| 68 }.bind(this)); | 159 }.bind(this)); |
| 69 } | 160 } |
| 70 | 161 |
| 71 /** | 162 /** |
| 72 * Returns whether two numbers are approximately equal. | 163 * Combines the internal pdf zoom and the browser zoom to |
| 73 * @param {number} a The first number. | 164 * produce the total zoom level for the viewer. |
| 74 * @param {number} b The second number. | 165 * @param {number} internalZoom the zoom level internal to the viewer. |
| 166 * @return {number} the total zoom level. | |
| 75 */ | 167 */ |
| 76 floatingPointEquals(a, b) { | 168 applyBrowserZoom(internalZoom) { |
| 77 let MIN_ZOOM_DELTA = 0.01; | 169 // 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 | 170 // browser zoom is already applied. |
| 79 // change it. This avoids us getting into an infinite loop of zoom changes | 171 return internalZoom; |
| 80 // due to floating point error. | 172 } |
| 81 return Math.abs(a - b) <= MIN_ZOOM_DELTA; | 173 |
| 174 /** | |
| 175 * Given a zoom level, return the internal zoom level needed to | |
| 176 * produce that zoom level. | |
| 177 * @param {number} totalZoom the total zoom level. | |
| 178 * @return {number} the zoom level internal to the viewer. | |
| 179 */ | |
| 180 internalZoomComponent(totalZoom) { | |
| 181 // The internal zoom and browser zoom are changed together, so the | |
| 182 // internal zoom is the total zoom. | |
| 183 return totalZoom; | |
| 82 } | 184 } |
| 83 }; | 185 }; |
| 186 | |
| 187 /** | |
| 188 * This EmbeddedZoomManager responds to changes in the browser zoom, | |
| 189 * but does not control the browser zoom. | |
| 190 */ | |
| 191 class EmbeddedZoomManager extends ZoomManager { | |
| 192 /** | |
| 193 * Invoked when a browser-initiated zoom-level change occurs. | |
| 194 * @param {number} newZoom the new browser zoom level. | |
| 195 */ | |
| 196 onBrowserZoomChange(newZoom) { | |
| 197 let oldZoom = this.browserZoom_; | |
| 198 this.browserZoom_ = newZoom; | |
| 199 this.viewport_.updateZoomFromBrowserChange(oldZoom); | |
| 200 } | |
| 201 }; | |
| OLD | NEW |