Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: chrome/browser/resources/pdf/zoom_manager.js

Issue 2503633002: Propagate browser zoom changes to embedded PDFs. (Closed)
Patch Set: Apply internal pdf zoom to browser zoom. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 * Returns whether two numbers are approximately equal.
59 * @param {number} a The first number.
60 * @param {number} b The second number.
61 */
62 floatingPointEquals(a, b) {
63 let MIN_ZOOM_DELTA = 0.01;
64 // If the zoom level is close enough to the current zoom level, don't
65 // change it. This avoids us getting into an infinite loop of zoom changes
66 // due to floating point error.
67 return Math.abs(a - b) <= MIN_ZOOM_DELTA;
68 }
69 };
70
71 /**
72 * InactiveZoomManager has no control over the browser's zoom
73 * and does not respond to browser zoom changes.
74 */
75 class InactiveZoomManager extends ZoomManager {};
76
77 /**
78 * ActiveZoomManager controls the browser's zoom.
79 */
80 class ActiveZoomManager extends ZoomManager {
81 /**
82 * Constructs a ActiveZoomManager.
13 * @param {!Viewport} viewport A Viewport for which to manage zoom. 83 * @param {!Viewport} viewport A Viewport for which to manage zoom.
14 * @param {Function} setBrowserZoomFunction A function that sets the browser 84 * @param {Function} setBrowserZoomFunction A function that sets the browser
15 * zoom to the provided value. 85 * zoom to the provided value.
16 * @param {number} initialZoom The initial browser zoom level. 86 * @param {number} initialZoom The initial browser zoom level.
17 */ 87 */
18 constructor(viewport, setBrowserZoomFunction, initialZoom) { 88 constructor(viewport, setBrowserZoomFunction, initialZoom) {
19 this.viewport_ = viewport; 89 super(viewport, initialZoom);
20 this.setBrowserZoomFunction_ = setBrowserZoomFunction; 90 this.setBrowserZoomFunction_ = setBrowserZoomFunction;
21 this.browserZoom_ = initialZoom;
22 this.changingBrowserZoom_ = null; 91 this.changingBrowserZoom_ = null;
23 } 92 }
24 93
25 /** 94 /**
26 * Invoked when a browser-initiated zoom-level change occurs. 95 * Invoked when a browser-initiated zoom-level change occurs.
27 * @param {number} newZoom the zoom level to zoom to. 96 * @param {number} newZoom the zoom level to zoom to.
28 */ 97 */
29 onBrowserZoomChange(newZoom) { 98 onBrowserZoomChange(newZoom) {
30 // If we are changing the browser zoom level, ignore any browser zoom level 99 // 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 100 // change events. Either, the change occurred before our update and will be
(...skipping 26 matching lines...) Expand all
58 127
59 this.changingBrowserZoom_ = this.setBrowserZoomFunction_(zoom).then( 128 this.changingBrowserZoom_ = this.setBrowserZoomFunction_(zoom).then(
60 function() { 129 function() {
61 this.browserZoom_ = zoom; 130 this.browserZoom_ = zoom;
62 this.changingBrowserZoom_ = null; 131 this.changingBrowserZoom_ = null;
63 132
64 // The extension's zoom level may have changed while the browser zoom 133 // 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 134 // change was in progress. We call back into onPdfZoomChange to ensure the
66 // browser zoom is up to date. 135 // browser zoom is up to date.
67 this.onPdfZoomChange(); 136 this.onPdfZoomChange();
137 }.bind(this), function() {
138 this.changingBrowserZoom_ = null;
68 }.bind(this)); 139 }.bind(this));
69 } 140 }
141 };
70 142
143 /**
144 * This EmbeddedZoomManager responds to changes in the browser zoom,
145 * but does not control the browser zoom.
146 */
147 class EmbeddedZoomManager extends ZoomManager {
71 /** 148 /**
72 * Returns whether two numbers are approximately equal. 149 * Invoked when a browser-initiated zoom-level change occurs.
73 * @param {number} a The first number. 150 * @param {number} newZoom the new browser zoom level.
74 * @param {number} b The second number.
75 */ 151 */
76 floatingPointEquals(a, b) { 152 onBrowserZoomChange(newZoom) {
77 let MIN_ZOOM_DELTA = 0.01; 153 // The viewer may have been zoomed independently of the browser,
78 // If the zoom level is close enough to the current zoom level, don't 154 // so we reapply the viewer's difference in zoom.
79 // change it. This avoids us getting into an infinite loop of zoom changes 155 let internalZoomRatio = this.viewport_.zoom / this.browserZoom_;
raymes 2016/11/17 00:59:57 I'm still not a big fan of the fact that we're wor
Sam McNally 2016/11/18 04:59:23 +1
Kevin McNee 2016/11/22 23:17:32 Done.
80 // due to floating point error. 156 let newViewportZoom = newZoom * internalZoomRatio;
81 return Math.abs(a - b) <= MIN_ZOOM_DELTA; 157
158 this.browserZoom_ = newZoom;
159
160 if (this.floatingPointEquals(this.viewport_.zoom, newViewportZoom))
161 return;
162
163 this.viewport_.setZoom(newViewportZoom);
82 } 164 }
83 }; 165 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698