[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /** |
Henrique Nakashima | 585c7af0 | 2018-03-27 04:55:21 | [diff] [blame] | 6 | * @typedef {{ |
| 7 | * x: number, |
| 8 | * y: number |
| 9 | * }} |
| 10 | */ |
| 11 | let Point; |
| 12 | |
| 13 | /** |
| 14 | * @typedef {{ |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 15 | * x: (number|undefined), |
| 16 | * y: (number|undefined), |
Henrique Nakashima | 585c7af0 | 2018-03-27 04:55:21 | [diff] [blame] | 17 | * }} |
| 18 | */ |
| 19 | let PartialPoint; |
| 20 | |
| 21 | /** |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 22 | * @typedef {{ |
| 23 | * x: number, |
| 24 | * y: number, |
| 25 | * width: number, |
| 26 | * heigh: number, |
| 27 | * }} |
| 28 | */ |
| 29 | let Rect; |
| 30 | |
| 31 | /** |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 32 | * Returns the height of the intersection of two rectangles. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 33 | * |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 34 | * @param {Rect} rect1 the first rect |
| 35 | * @param {Rect} rect2 the second rect |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 36 | * @return {number} the height of the intersection of the rects |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 37 | */ |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 38 | function getIntersectionHeight(rect1, rect2) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 39 | return Math.max( |
| 40 | 0, |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 41 | Math.min(rect1.y + rect1.height, rect2.y + rect2.height) - |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 42 | Math.max(rect1.y, rect2.y)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 46 | * Computes vector between two points. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 47 | * |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 48 | * @param {!Object} p1 The first point. |
| 49 | * @param {!Object} p2 The second point. |
| 50 | * @return {!Object} The vector. |
| 51 | */ |
| 52 | function vectorDelta(p1, p2) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 53 | return {x: p2.x - p1.x, y: p2.y - p1.y}; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | function frameToPluginCoordinate(coordinateInFrame) { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 57 | const container = $('plugin'); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 58 | return { |
| 59 | x: coordinateInFrame.x - container.getBoundingClientRect().left, |
| 60 | y: coordinateInFrame.y - container.getBoundingClientRect().top |
| 61 | }; |
| 62 | } |
| 63 | |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 64 | // TODO: convert Viewport to ES6 class syntax |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 65 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 66 | * Create a new viewport. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 67 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 68 | * @param {Window} window the window |
| 69 | * @param {Object} sizer is the element which represents the size of the |
| 70 | * document in the viewport |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 71 | * @param {Function} viewportChangedCallback is run when the viewport changes |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 72 | * @param {Function} beforeZoomCallback is run before a change in zoom |
| 73 | * @param {Function} afterZoomCallback is run after a change in zoom |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 74 | * @param {Function} setUserInitiatedCallback is run to indicate whether a zoom |
| 75 | * event is user initiated. |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 76 | * @param {number} scrollbarWidth the width of scrollbars on the page |
sammc | 43d4982f | 2015-04-22 07:46:51 | [diff] [blame] | 77 | * @param {number} defaultZoom The default zoom level. |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 78 | * @param {number} topToolbarHeight The number of pixels that should initially |
| 79 | * be left blank above the document for the toolbar. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 80 | * @constructor |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 81 | */ |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 82 | function Viewport( |
| 83 | window, sizer, viewportChangedCallback, beforeZoomCallback, |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 84 | afterZoomCallback, setUserInitiatedCallback, scrollbarWidth, defaultZoom, |
| 85 | topToolbarHeight) { |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 86 | this.window_ = window; |
| 87 | this.sizer_ = sizer; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 88 | this.viewportChangedCallback_ = viewportChangedCallback; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 89 | this.beforeZoomCallback_ = beforeZoomCallback; |
| 90 | this.afterZoomCallback_ = afterZoomCallback; |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 91 | this.setUserInitiatedCallback_ = setUserInitiatedCallback; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 92 | this.allowedToChangeZoom_ = false; |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 93 | this.internalZoom_ = 1; |
| 94 | this.zoomManager_ = new InactiveZoomManager(this, 1); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 95 | this.documentDimensions_ = null; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 96 | this.pageDimensions_ = []; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 97 | this.scrollbarWidth_ = scrollbarWidth; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 98 | this.fittingType_ = FittingType.NONE; |
sammc | 43d4982f | 2015-04-22 07:46:51 | [diff] [blame] | 99 | this.defaultZoom_ = defaultZoom; |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 100 | this.topToolbarHeight_ = topToolbarHeight; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 101 | this.prevScale_ = 1; |
| 102 | this.pinchPhase_ = Viewport.PinchPhase.PINCH_NONE; |
| 103 | this.pinchPanVector_ = null; |
| 104 | this.pinchCenter_ = null; |
| 105 | this.firstPinchCenterInFrame_ = null; |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 106 | this.rotations_ = 0; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 107 | |
| 108 | window.addEventListener('scroll', this.updateViewport_.bind(this)); |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 109 | window.addEventListener('resize', this.resizeWrapper_.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 112 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 113 | * Enumeration of pinch states. |
| 114 | * This should match PinchPhase enum in pdf/out_of_process_instance.h |
| 115 | * @enum {number} |
| 116 | */ |
| 117 | Viewport.PinchPhase = { |
| 118 | PINCH_NONE: 0, |
| 119 | PINCH_START: 1, |
| 120 | PINCH_UPDATE_ZOOM_OUT: 2, |
| 121 | PINCH_UPDATE_ZOOM_IN: 3, |
| 122 | PINCH_END: 4 |
| 123 | }; |
| 124 | |
| 125 | /** |
[email protected] | 8dcaa26 | 2014-05-30 13:33:37 | [diff] [blame] | 126 | * The increment to scroll a page by in pixels when up/down/left/right arrow |
| 127 | * keys are pressed. Usually we just let the browser handle scrolling on the |
| 128 | * window when these keys are pressed but in certain cases we need to simulate |
| 129 | * these events. |
| 130 | */ |
| 131 | Viewport.SCROLL_INCREMENT = 40; |
| 132 | |
| 133 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 134 | * Predefined zoom factors to be used when zooming in/out. These are in |
bsep | 24e7ec4 | 2016-08-25 02:11:20 | [diff] [blame] | 135 | * ascending order. This should match the lists in |
| 136 | * components/ui/zoom/page_zoom_constants.h and |
| 137 | * chrome/browser/resources/settings/appearance_page/appearance_page.js |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 138 | */ |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 139 | Viewport.ZOOM_FACTORS = [ |
| 140 | 0.25, 1 / 3, 0.5, 2 / 3, 0.75, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, |
| 141 | 4, 5 |
| 142 | ]; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 143 | |
| 144 | /** |
[email protected] | 4271e16b | 2014-08-22 12:18:59 | [diff] [blame] | 145 | * The minimum and maximum range to be used to clip zoom factor. |
| 146 | */ |
| 147 | Viewport.ZOOM_FACTOR_RANGE = { |
| 148 | min: Viewport.ZOOM_FACTORS[0], |
| 149 | max: Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1] |
| 150 | }; |
| 151 | |
| 152 | /** |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 153 | * Clamps the zoom factor (or page scale factor) to be within the limits. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 154 | * |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 155 | * @param {number} factor The zoom/scale factor. |
| 156 | * @return {number} The factor clamped within the limits. |
| 157 | */ |
| 158 | Viewport.clampZoom = function(factor) { |
| 159 | return Math.max( |
| 160 | Viewport.ZOOM_FACTOR_RANGE.min, |
| 161 | Math.min(factor, Viewport.ZOOM_FACTOR_RANGE.max)); |
| 162 | }; |
| 163 | |
| 164 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 165 | * The width of the page shadow around pages in pixels. |
| 166 | */ |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 167 | Viewport.PAGE_SHADOW = { |
| 168 | top: 3, |
| 169 | bottom: 7, |
| 170 | left: 5, |
| 171 | right: 5 |
| 172 | }; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 173 | |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 174 | Viewport.prototype = { |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 175 | |
| 176 | /** |
| 177 | * @param {number} n the number of clockwise 90-degree rotations to |
| 178 | * increment by. |
| 179 | */ |
| 180 | rotateClockwise: function(n) { |
| 181 | this.rotations_ = (this.rotations_ + n) % 4; |
| 182 | }, |
| 183 | |
| 184 | /** |
| 185 | * @return {number} the number of clockwise 90-degree rotations that have been |
| 186 | * applied. |
| 187 | */ |
| 188 | getClockwiseRotations: function() { |
| 189 | return this.rotations_; |
| 190 | }, |
| 191 | |
| 192 | /** |
| 193 | * Converts a page position (e.g. the location of a bookmark) to a screen |
| 194 | * position. |
| 195 | * |
| 196 | * @param {number} page |
| 197 | * @param {Point} point The position on `page`. |
| 198 | * @return The screen position. |
| 199 | */ |
| 200 | convertPageToScreen: function(page, point) { |
| 201 | const dimensions = this.getPageInsetDimensions(page); |
| 202 | |
| 203 | // width & height are already rotated. |
| 204 | const height = dimensions.height; |
| 205 | const width = dimensions.width; |
| 206 | |
| 207 | const matrix = new DOMMatrix(); |
| 208 | |
| 209 | const rotation = this.rotations_ * 90; |
| 210 | // Set origin for rotation. |
| 211 | if (rotation == 90) { |
| 212 | matrix.translateSelf(width, 0); |
| 213 | } else if (rotation == 180) { |
| 214 | matrix.translateSelf(width, height); |
| 215 | } else if (rotation == 270) { |
| 216 | matrix.translateSelf(0, height); |
| 217 | } |
| 218 | matrix.rotateSelf(0, 0, rotation); |
| 219 | |
| 220 | // Invert Y position with respect to height as page coordinates are |
| 221 | // measured from the bottom left. |
| 222 | matrix.translateSelf(0, height); |
| 223 | matrix.scaleSelf(1, -1); |
| 224 | |
| 225 | const pointsToPixels = 96 / 72; |
| 226 | const result = matrix.transformPoint({ |
| 227 | x: point.x * pointsToPixels, |
| 228 | y: point.y * pointsToPixels, |
| 229 | }); |
| 230 | return { |
| 231 | x: result.x + Viewport.PAGE_SHADOW.left, |
| 232 | y: result.y + Viewport.PAGE_SHADOW.top, |
| 233 | }; |
| 234 | }, |
| 235 | |
| 236 | |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 237 | /** |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 238 | * Returns the zoomed and rounded document dimensions for the given zoom. |
| 239 | * Rounding is necessary when interacting with the renderer which tends to |
| 240 | * operate in integral values (for example for determining if scrollbars |
| 241 | * should be shown). |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 242 | * |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 243 | * @param {number} zoom The zoom to use to compute the scaled dimensions. |
| 244 | * @return {Object} A dictionary with scaled 'width'/'height' of the document. |
| 245 | * @private |
| 246 | */ |
| 247 | getZoomedDocumentDimensions_: function(zoom) { |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 248 | if (!this.documentDimensions_) { |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 249 | return null; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 250 | } |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 251 | return { |
| 252 | width: Math.round(this.documentDimensions_.width * zoom), |
| 253 | height: Math.round(this.documentDimensions_.height * zoom) |
| 254 | }; |
| 255 | }, |
| 256 | |
| 257 | /** |
dstockwell | 118c536 | 2019-01-03 03:01:34 | [diff] [blame] | 258 | * Returns the document dimensions. |
| 259 | * |
| 260 | * @return {Point} A dictionary with the 'width'/'height' of the document. |
| 261 | */ |
| 262 | getDocumentDimensions: function() { |
| 263 | return { |
| 264 | width: this.documentDimensions_.width, |
| 265 | height: this.documentDimensions_.height |
| 266 | }; |
| 267 | }, |
| 268 | |
| 269 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 270 | * Returns true if the document needs scrollbars at the given zoom level. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 271 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 272 | * @param {number} zoom compute whether scrollbars are needed at this zoom |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 273 | * @return {Object} with 'horizontal' and 'vertical' keys which map to bool |
| 274 | * values indicating if the horizontal and vertical scrollbars are needed |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 275 | * respectively. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 276 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 277 | */ |
| 278 | documentNeedsScrollbars_: function(zoom) { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 279 | const zoomedDimensions = this.getZoomedDocumentDimensions_(zoom); |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 280 | if (!zoomedDimensions) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 281 | return {horizontal: false, vertical: false}; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 282 | } |
sammc | 63334fed | 2014-11-10 03:07:35 | [diff] [blame] | 283 | |
| 284 | // If scrollbars are required for one direction, expand the document in the |
| 285 | // other direction to take the width of the scrollbars into account when |
| 286 | // deciding whether the other direction needs scrollbars. |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 287 | if (zoomedDimensions.width > this.window_.innerWidth) { |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 288 | zoomedDimensions.height += this.scrollbarWidth_; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 289 | } else if (zoomedDimensions.height > this.window_.innerHeight) { |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 290 | zoomedDimensions.width += this.scrollbarWidth_; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 291 | } |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 292 | return { |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 293 | horizontal: zoomedDimensions.width > this.window_.innerWidth, |
raymes | 051fb2c | 2015-09-21 04:56:41 | [diff] [blame] | 294 | vertical: zoomedDimensions.height + this.topToolbarHeight_ > |
| 295 | this.window_.innerHeight |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 296 | }; |
| 297 | }, |
| 298 | |
| 299 | /** |
| 300 | * Returns true if the document needs scrollbars at the current zoom level. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 301 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 302 | * @return {Object} with 'x' and 'y' keys which map to bool values |
| 303 | * indicating if the horizontal and vertical scrollbars are needed |
| 304 | * respectively. |
| 305 | */ |
| 306 | documentHasScrollbars: function() { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 307 | return this.documentNeedsScrollbars_(this.zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 308 | }, |
| 309 | |
| 310 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 311 | * Helper function called when the zoomed document size changes. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 312 | * |
| 313 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 314 | */ |
| 315 | contentSizeChanged_: function() { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 316 | const zoomedDimensions = this.getZoomedDocumentDimensions_(this.zoom); |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 317 | if (zoomedDimensions) { |
| 318 | this.sizer_.style.width = zoomedDimensions.width + 'px'; |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 319 | this.sizer_.style.height = |
| 320 | zoomedDimensions.height + this.topToolbarHeight_ + 'px'; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 321 | } |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 322 | }, |
| 323 | |
| 324 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 325 | * Called when the viewport should be updated. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 326 | * |
| 327 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 328 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 329 | updateViewport_: function() { |
| 330 | this.viewportChangedCallback_(); |
| 331 | }, |
| 332 | |
| 333 | /** |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 334 | * Called when the browser window size changes. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 335 | * |
| 336 | * @private |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 337 | */ |
| 338 | resizeWrapper_: function() { |
| 339 | this.setUserInitiatedCallback_(false); |
| 340 | this.resize_(); |
| 341 | this.setUserInitiatedCallback_(true); |
| 342 | }, |
| 343 | |
| 344 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 345 | * Called when the viewport size changes. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 346 | * |
| 347 | * @private |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 348 | */ |
| 349 | resize_: function() { |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 350 | if (this.fittingType_ == FittingType.FIT_TO_PAGE) { |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 351 | this.fitToPageInternal_(false); |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 352 | } else if (this.fittingType_ == FittingType.FIT_TO_WIDTH) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 353 | this.fitToWidth(); |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 354 | } else if (this.fittingType_ == FittingType.FIT_TO_HEIGHT) { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 355 | this.fitToHeightInternal_(false); |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 356 | } else if (this.internalZoom_ == 0) { |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 357 | this.fitToNone(); |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 358 | } else { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 359 | this.updateViewport_(); |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 360 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 361 | }, |
| 362 | |
| 363 | /** |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 364 | * @type {Point} the scroll position of the viewport. |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 365 | */ |
| 366 | get position() { |
| 367 | return { |
| 368 | x: this.window_.pageXOffset, |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 369 | y: this.window_.pageYOffset - this.topToolbarHeight_ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 370 | }; |
| 371 | }, |
| 372 | |
| 373 | /** |
| 374 | * Scroll the viewport to the specified position. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 375 | * |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 376 | * @type {Point} position The position to scroll to. |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 377 | */ |
| 378 | set position(position) { |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 379 | this.window_.scrollTo(position.x, position.y + this.topToolbarHeight_); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 380 | }, |
| 381 | |
| 382 | /** |
| 383 | * @type {Object} the size of the viewport excluding scrollbars. |
| 384 | */ |
| 385 | get size() { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 386 | const needsScrollbars = this.documentNeedsScrollbars_(this.zoom); |
| 387 | const scrollbarWidth = needsScrollbars.vertical ? this.scrollbarWidth_ : 0; |
| 388 | const scrollbarHeight = |
| 389 | needsScrollbars.horizontal ? this.scrollbarWidth_ : 0; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 390 | return { |
| 391 | width: this.window_.innerWidth - scrollbarWidth, |
| 392 | height: this.window_.innerHeight - scrollbarHeight |
| 393 | }; |
| 394 | }, |
| 395 | |
| 396 | /** |
| 397 | * @type {number} the zoom level of the viewport. |
| 398 | */ |
| 399 | get zoom() { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 400 | return this.zoomManager_.applyBrowserZoom(this.internalZoom_); |
| 401 | }, |
| 402 | |
| 403 | /** |
| 404 | * Set the zoom manager. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 405 | * |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 406 | * @type {ZoomManager} manager the zoom manager to set. |
| 407 | */ |
| 408 | set zoomManager(manager) { |
| 409 | this.zoomManager_ = manager; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 410 | }, |
| 411 | |
| 412 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 413 | * @type {Viewport.PinchPhase} The phase of the current pinch gesture for |
| 414 | * the viewport. |
| 415 | */ |
| 416 | get pinchPhase() { |
| 417 | return this.pinchPhase_; |
| 418 | }, |
| 419 | |
| 420 | /** |
| 421 | * @type {Object} The panning caused by the current pinch gesture (as |
| 422 | * the deltas of the x and y coordinates). |
| 423 | */ |
| 424 | get pinchPanVector() { |
| 425 | return this.pinchPanVector_; |
| 426 | }, |
| 427 | |
| 428 | /** |
| 429 | * @type {Object} The coordinates of the center of the current pinch gesture. |
| 430 | */ |
| 431 | get pinchCenter() { |
| 432 | return this.pinchCenter_; |
| 433 | }, |
| 434 | |
| 435 | /** |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 436 | * Used to wrap a function that might perform zooming on the viewport. This is |
| 437 | * required so that we can notify the plugin that zooming is in progress |
| 438 | * so that while zooming is taking place it can stop reacting to scroll events |
| 439 | * from the viewport. This is to avoid flickering. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 440 | * |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 441 | * @param {Function} f Function to wrap |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 442 | * @private |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 443 | */ |
| 444 | mightZoom_: function(f) { |
| 445 | this.beforeZoomCallback_(); |
| 446 | this.allowedToChangeZoom_ = true; |
| 447 | f(); |
| 448 | this.allowedToChangeZoom_ = false; |
| 449 | this.afterZoomCallback_(); |
| 450 | }, |
| 451 | |
| 452 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 453 | * Sets the zoom of the viewport. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 454 | * |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 455 | * @param {number} newZoom the zoom level to zoom to. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 456 | * @private |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 457 | */ |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 458 | setZoomInternal_: function(newZoom) { |
dstockwell | 154f9c4 | 2019-01-01 23:36:34 | [diff] [blame] | 459 | assert( |
| 460 | this.allowedToChangeZoom_, |
| 461 | 'Called Viewport.setZoomInternal_ without calling ' + |
| 462 | 'Viewport.mightZoom_.'); |
sammc | 5c33b7e | 2014-11-07 04:03:09 | [diff] [blame] | 463 | // Record the scroll position (relative to the top-left of the window). |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 464 | const currentScrollPos = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 465 | x: this.position.x / this.zoom, |
| 466 | y: this.position.y / this.zoom |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 467 | }; |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 468 | |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 469 | this.internalZoom_ = newZoom; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 470 | this.contentSizeChanged_(); |
| 471 | // Scroll to the scaled scroll position. |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 472 | this.position = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 473 | x: currentScrollPos.x * this.zoom, |
| 474 | y: currentScrollPos.y * this.zoom |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 475 | }; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 476 | }, |
| 477 | |
| 478 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 479 | * Sets the zoom of the viewport. |
| 480 | * Same as setZoomInternal_ but for pinch zoom we have some more operations. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 481 | * |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 482 | * @param {number} scaleDelta The zoom delta. |
| 483 | * @param {!Object} center The pinch center in content coordinates. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 484 | * @private |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 485 | */ |
| 486 | setPinchZoomInternal_: function(scaleDelta, center) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 487 | assert( |
| 488 | this.allowedToChangeZoom_, |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 489 | 'Called Viewport.setPinchZoomInternal_ without calling ' + |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 490 | 'Viewport.mightZoom_.'); |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 491 | this.internalZoom_ = Viewport.clampZoom(this.internalZoom_ * scaleDelta); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 492 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 493 | const newCenterInContent = this.frameToContent(center); |
| 494 | const delta = { |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 495 | x: (newCenterInContent.x - this.oldCenterInContent.x), |
| 496 | y: (newCenterInContent.y - this.oldCenterInContent.y) |
| 497 | }; |
| 498 | |
| 499 | // Record the scroll position (relative to the pinch center). |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 500 | const currentScrollPos = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 501 | x: this.position.x - delta.x * this.zoom, |
| 502 | y: this.position.y - delta.y * this.zoom |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 503 | }; |
| 504 | |
| 505 | this.contentSizeChanged_(); |
| 506 | // Scroll to the scaled scroll position. |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 507 | this.position = {x: currentScrollPos.x, y: currentScrollPos.y}; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 508 | }, |
| 509 | |
| 510 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 511 | * Converts a point from frame to content coordinates. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 512 | * |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 513 | * @param {!Object} framePoint The frame coordinates. |
| 514 | * @return {!Object} The content coordinates. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 515 | * @private |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 516 | */ |
| 517 | frameToContent: function(framePoint) { |
| 518 | // TODO(mcnee) Add a helper Point class to avoid duplicating operations |
| 519 | // on plain {x,y} objects. |
| 520 | return { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 521 | x: (framePoint.x + this.position.x) / this.zoom, |
| 522 | y: (framePoint.y + this.position.y) / this.zoom |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 523 | }; |
| 524 | }, |
| 525 | |
| 526 | /** |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 527 | * Sets the zoom to the given zoom level. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 528 | * |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 529 | * @param {number} newZoom the zoom level to zoom to. |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 530 | */ |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 531 | setZoom: function(newZoom) { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 532 | this.fittingType_ = FittingType.NONE; |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 533 | this.mightZoom_(() => { |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 534 | this.setZoomInternal_(Viewport.clampZoom(newZoom)); |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 535 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 536 | }); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 537 | }, |
| 538 | |
| 539 | /** |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 540 | * Gets notified of the browser zoom changing seperately from the |
| 541 | * internal zoom. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 542 | * |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 543 | * @param {number} oldBrowserZoom the previous value of the browser zoom. |
| 544 | */ |
| 545 | updateZoomFromBrowserChange: function(oldBrowserZoom) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 546 | this.mightZoom_(() => { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 547 | // Record the scroll position (relative to the top-left of the window). |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 548 | const oldZoom = oldBrowserZoom * this.internalZoom_; |
| 549 | const currentScrollPos = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 550 | x: this.position.x / oldZoom, |
| 551 | y: this.position.y / oldZoom |
| 552 | }; |
| 553 | this.contentSizeChanged_(); |
| 554 | // Scroll to the scaled scroll position. |
| 555 | this.position = { |
| 556 | x: currentScrollPos.x * this.zoom, |
| 557 | y: currentScrollPos.y * this.zoom |
| 558 | }; |
| 559 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 560 | }); |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 561 | }, |
| 562 | |
| 563 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 564 | * @type {number} the width of scrollbars in the viewport in pixels. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 565 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 566 | get scrollbarWidth() { |
| 567 | return this.scrollbarWidth_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 568 | }, |
| 569 | |
| 570 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 571 | * @type {FittingType} the fitting type the viewport is currently in. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 572 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 573 | get fittingType() { |
| 574 | return this.fittingType_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 575 | }, |
| 576 | |
| 577 | /** |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 578 | * Get the which page is at a given y position. |
| 579 | * |
dpapad | d2d6477 | 2017-05-19 02:30:38 | [diff] [blame] | 580 | * @param {number} y the y-coordinate to get the page at. |
| 581 | * @return {number} the index of a page overlapping the given y-coordinate. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 582 | * @private |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 583 | */ |
| 584 | getPageAtY_: function(y) { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 585 | let min = 0; |
| 586 | let max = this.pageDimensions_.length - 1; |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 587 | while (max >= min) { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 588 | const page = Math.floor(min + ((max - min) / 2)); |
[email protected] | 13df2a4 | 2014-02-27 03:50:41 | [diff] [blame] | 589 | // There might be a gap between the pages, in which case use the bottom |
| 590 | // of the previous page as the top for finding the page. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 591 | let top = 0; |
[email protected] | 13df2a4 | 2014-02-27 03:50:41 | [diff] [blame] | 592 | if (page > 0) { |
| 593 | top = this.pageDimensions_[page - 1].y + |
| 594 | this.pageDimensions_[page - 1].height; |
| 595 | } |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 596 | const bottom = |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 597 | this.pageDimensions_[page].y + this.pageDimensions_[page].height; |
[email protected] | 13df2a4 | 2014-02-27 03:50:41 | [diff] [blame] | 598 | |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 599 | if (top <= y && bottom > y) { |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 600 | return page; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 601 | } |
Lei Zhang | ddc0ef03 | 2017-11-22 02:14:24 | [diff] [blame] | 602 | |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 603 | if (top > y) { |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 604 | max = page - 1; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 605 | } else { |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 606 | min = page + 1; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 607 | } |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 608 | } |
| 609 | return 0; |
| 610 | }, |
| 611 | |
| 612 | /** |
dstockwell | 09815ba | 2019-01-16 06:44:24 | [diff] [blame^] | 613 | * @param {Point} point |
| 614 | * @return {boolean} Whether |point| (in screen coordinates) is inside a page |
| 615 | */ |
| 616 | isPointInsidePage(point) { |
| 617 | const zoom = this.zoom; |
| 618 | const size = this.size; |
| 619 | const position = this.position; |
| 620 | const page = this.getPageAtY_((position.y + point.y) / zoom); |
| 621 | const pageWidth = this.pageDimensions_[page].width * zoom; |
| 622 | const documentWidth = this.getDocumentDimensions().width * zoom; |
| 623 | |
| 624 | const outerWidth = Math.max(size.width, documentWidth); |
| 625 | |
| 626 | if (pageWidth >= outerWidth) { |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | const x = point.x + position.x; |
| 631 | |
| 632 | const minX = (outerWidth - pageWidth) / 2; |
| 633 | const maxX = outerWidth - minX; |
| 634 | return x >= minX && x <= maxX; |
| 635 | }, |
| 636 | |
| 637 | /** |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 638 | * Returns the page with the greatest proportion of its height in the current |
| 639 | * viewport. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 640 | * |
dpapad | d2d6477 | 2017-05-19 02:30:38 | [diff] [blame] | 641 | * @return {number} the index of the most visible page. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 642 | */ |
| 643 | getMostVisiblePage: function() { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 644 | const firstVisiblePage = this.getPageAtY_(this.position.y / this.zoom); |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 645 | if (firstVisiblePage == this.pageDimensions_.length - 1) { |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 646 | return firstVisiblePage; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 647 | } |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 648 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 649 | const viewportRect = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 650 | x: this.position.x / this.zoom, |
| 651 | y: this.position.y / this.zoom, |
| 652 | width: this.size.width / this.zoom, |
| 653 | height: this.size.height / this.zoom |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 654 | }; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 655 | const firstVisiblePageVisibility = |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 656 | getIntersectionHeight( |
| 657 | this.pageDimensions_[firstVisiblePage], viewportRect) / |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 658 | this.pageDimensions_[firstVisiblePage].height; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 659 | const nextPageVisibility = |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 660 | getIntersectionHeight( |
| 661 | this.pageDimensions_[firstVisiblePage + 1], viewportRect) / |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 662 | this.pageDimensions_[firstVisiblePage + 1].height; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 663 | if (nextPageVisibility > firstVisiblePageVisibility) { |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 664 | return firstVisiblePage + 1; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 665 | } |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 666 | return firstVisiblePage; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 667 | }, |
| 668 | |
| 669 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 670 | * Compute the zoom level for fit-to-page, fit-to-width or fit-to-height. |
| 671 | * |
| 672 | * At least one of {fitWidth, fitHeight} must be true. |
| 673 | * |
| 674 | * @param {Object} pageDimensions the dimensions of a given page in px. |
| 675 | * @param {boolean} fitWidth a bool indicating whether the whole width of the |
| 676 | * page needs to be in the viewport. |
| 677 | * @param {boolean} fitHeight a bool indicating whether the whole height of |
| 678 | * the page needs to be in the viewport. |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 679 | * @return {number} the internal zoom to set |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 680 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 681 | */ |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 682 | computeFittingZoom_: function(pageDimensions, fitWidth, fitHeight) { |
| 683 | assert( |
| 684 | fitWidth || fitHeight, |
| 685 | 'Invalid parameters. At least one of fitWidth and fitHeight must be ' + |
| 686 | 'true.'); |
| 687 | |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 688 | // First compute the zoom without scrollbars. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 689 | let zoom = this.computeFittingZoomGivenDimensions_( |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 690 | fitWidth, fitHeight, this.window_.innerWidth, this.window_.innerHeight, |
| 691 | pageDimensions.width, pageDimensions.height); |
| 692 | |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 693 | // Check if there needs to be any scrollbars. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 694 | const needsScrollbars = this.documentNeedsScrollbars_(zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 695 | |
| 696 | // If the document fits, just return the zoom. |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 697 | if (!needsScrollbars.horizontal && !needsScrollbars.vertical) { |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 698 | return zoom; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 699 | } |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 700 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 701 | const zoomedDimensions = this.getZoomedDocumentDimensions_(zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 702 | |
| 703 | // Check if adding a scrollbar will result in needing the other scrollbar. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 704 | const scrollbarWidth = this.scrollbarWidth_; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 705 | if (needsScrollbars.horizontal && |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 706 | zoomedDimensions.height > this.window_.innerHeight - scrollbarWidth) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 707 | needsScrollbars.vertical = true; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 708 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 709 | if (needsScrollbars.vertical && |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 710 | zoomedDimensions.width > this.window_.innerWidth - scrollbarWidth) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 711 | needsScrollbars.horizontal = true; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | // Compute available window space. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 715 | const windowWithScrollbars = { |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 716 | width: this.window_.innerWidth, |
| 717 | height: this.window_.innerHeight |
| 718 | }; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 719 | if (needsScrollbars.horizontal) { |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 720 | windowWithScrollbars.height -= scrollbarWidth; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 721 | } |
| 722 | if (needsScrollbars.vertical) { |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 723 | windowWithScrollbars.width -= scrollbarWidth; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 724 | } |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 725 | |
| 726 | // Recompute the zoom. |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 727 | zoom = this.computeFittingZoomGivenDimensions_( |
| 728 | fitWidth, fitHeight, windowWithScrollbars.width, |
| 729 | windowWithScrollbars.height, pageDimensions.width, |
| 730 | pageDimensions.height); |
| 731 | |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 732 | return this.zoomManager_.internalZoomComponent(zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 733 | }, |
| 734 | |
| 735 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 736 | * Compute a zoom level given the dimensions to fit and the actual numbers |
| 737 | * in those dimensions. |
| 738 | * |
| 739 | * @param {boolean} fitWidth make sure the page width is totally contained in |
| 740 | * the window. |
| 741 | * @param {boolean} fitHeight make sure the page height is totally contained |
| 742 | * in the window. |
| 743 | * @param {number} windowWidth the width of the window in px. |
| 744 | * @param {number} windowHeight the height of the window in px. |
| 745 | * @param {number} pageWidth the width of the page in px. |
| 746 | * @param {number} pageHeight the height of the page in px. |
| 747 | * @return {number} the internal zoom to set |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 748 | * @private |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 749 | */ |
| 750 | computeFittingZoomGivenDimensions_: function( |
| 751 | fitWidth, fitHeight, windowWidth, windowHeight, pageWidth, pageHeight) { |
| 752 | // Assumes at least one of {fitWidth, fitHeight} is set. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 753 | let zoomWidth; |
| 754 | let zoomHeight; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 755 | |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 756 | if (fitWidth) { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 757 | zoomWidth = windowWidth / pageWidth; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 758 | } |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 759 | |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 760 | if (fitHeight) { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 761 | zoomHeight = windowHeight / pageHeight; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 762 | } |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 763 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 764 | let zoom; |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 765 | if (!fitWidth && fitHeight) { |
| 766 | zoom = zoomHeight; |
| 767 | } else if (fitWidth && !fitHeight) { |
| 768 | zoom = zoomWidth; |
| 769 | } else { |
| 770 | // Assume fitWidth && fitHeight |
| 771 | zoom = Math.min(zoomWidth, zoomHeight); |
| 772 | } |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 773 | |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 774 | return Math.max(zoom, 0); |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 775 | }, |
| 776 | |
| 777 | /** |
| 778 | * Zoom the viewport so that the page width consumes the entire viewport. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 779 | */ |
| 780 | fitToWidth: function() { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 781 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 782 | this.fittingType_ = FittingType.FIT_TO_WIDTH; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 783 | if (!this.documentDimensions_) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 784 | return; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 785 | } |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 786 | // When computing fit-to-width, the maximum width of a page in the |
| 787 | // document is used, which is equal to the size of the document width. |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 788 | this.setZoomInternal_( |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 789 | this.computeFittingZoom_(this.documentDimensions_, true, false)); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 790 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 791 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 792 | }, |
| 793 | |
| 794 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 795 | * Zoom the viewport so that the page height consumes the entire viewport. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 796 | * |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 797 | * @param {boolean} scrollToTopOfPage Set to true if the viewport should be |
| 798 | * scrolled to the top of the current page. Set to false if the viewport |
| 799 | * should remain at the current scroll position. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 800 | * @private |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 801 | */ |
| 802 | fitToHeightInternal_: function(scrollToTopOfPage) { |
| 803 | this.mightZoom_(() => { |
| 804 | this.fittingType_ = FittingType.FIT_TO_HEIGHT; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 805 | if (!this.documentDimensions_) { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 806 | return; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 807 | } |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 808 | const page = this.getMostVisiblePage(); |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 809 | // When computing fit-to-height, the maximum height of the current page |
| 810 | // is used. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 811 | const dimensions = { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 812 | width: 0, |
| 813 | height: this.pageDimensions_[page].height, |
| 814 | }; |
| 815 | this.setZoomInternal_(this.computeFittingZoom_(dimensions, false, true)); |
| 816 | if (scrollToTopOfPage) { |
| 817 | this.position = {x: 0, y: this.pageDimensions_[page].y * this.zoom}; |
| 818 | } |
| 819 | this.updateViewport_(); |
| 820 | }); |
| 821 | }, |
| 822 | |
| 823 | /** |
| 824 | * Zoom the viewport so that the page height consumes the entire viewport. |
| 825 | */ |
| 826 | fitToHeight: function() { |
| 827 | this.fitToHeightInternal_(true); |
| 828 | }, |
| 829 | |
| 830 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 831 | * Zoom the viewport so that a page consumes as much as possible of the it. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 832 | * |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 833 | * @param {boolean} scrollToTopOfPage Set to true if the viewport should be |
| 834 | * scrolled to the top of the current page. Set to false if the viewport |
| 835 | * should remain at the current scroll position. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 836 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 837 | */ |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 838 | fitToPageInternal_: function(scrollToTopOfPage) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 839 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 840 | this.fittingType_ = FittingType.FIT_TO_PAGE; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 841 | if (!this.documentDimensions_) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 842 | return; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 843 | } |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 844 | const page = this.getMostVisiblePage(); |
sammc | 07f53aa | 2014-11-06 06:57:46 | [diff] [blame] | 845 | // Fit to the current page's height and the widest page's width. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 846 | const dimensions = { |
sammc | 07f53aa | 2014-11-06 06:57:46 | [diff] [blame] | 847 | width: this.documentDimensions_.width, |
| 848 | height: this.pageDimensions_[page].height, |
| 849 | }; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 850 | this.setZoomInternal_(this.computeFittingZoom_(dimensions, true, true)); |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 851 | if (scrollToTopOfPage) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 852 | this.position = {x: 0, y: this.pageDimensions_[page].y * this.zoom}; |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 853 | } |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 854 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 855 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 856 | }, |
| 857 | |
| 858 | /** |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 859 | * Zoom the viewport so that a page consumes the entire viewport. Also scrolls |
| 860 | * the viewport to the top of the current page. |
| 861 | */ |
| 862 | fitToPage: function() { |
| 863 | this.fitToPageInternal_(true); |
| 864 | }, |
| 865 | |
| 866 | /** |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 867 | * Zoom the viewport to the default zoom policy. |
| 868 | */ |
| 869 | fitToNone: function() { |
| 870 | this.mightZoom_(() => { |
| 871 | this.fittingType_ = FittingType.NONE; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 872 | if (!this.documentDimensions_) { |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 873 | return; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 874 | } |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 875 | this.setZoomInternal_(Math.min( |
| 876 | this.defaultZoom_, |
| 877 | this.computeFittingZoom_(this.documentDimensions_, true, false))); |
| 878 | this.updateViewport_(); |
| 879 | }); |
| 880 | }, |
| 881 | |
| 882 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 883 | * Zoom out to the next predefined zoom level. |
| 884 | */ |
| 885 | zoomOut: function() { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 886 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 887 | this.fittingType_ = FittingType.NONE; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 888 | let nextZoom = Viewport.ZOOM_FACTORS[0]; |
| 889 | for (let i = 0; i < Viewport.ZOOM_FACTORS.length; i++) { |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 890 | if (Viewport.ZOOM_FACTORS[i] < this.internalZoom_) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 891 | nextZoom = Viewport.ZOOM_FACTORS[i]; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 892 | } |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 893 | } |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 894 | this.setZoomInternal_(nextZoom); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 895 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 896 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 897 | }, |
| 898 | |
| 899 | /** |
| 900 | * Zoom in to the next predefined zoom level. |
| 901 | */ |
| 902 | zoomIn: function() { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 903 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 904 | this.fittingType_ = FittingType.NONE; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 905 | let nextZoom = Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1]; |
| 906 | for (let i = Viewport.ZOOM_FACTORS.length - 1; i >= 0; i--) { |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 907 | if (Viewport.ZOOM_FACTORS[i] > this.internalZoom_) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 908 | nextZoom = Viewport.ZOOM_FACTORS[i]; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 909 | } |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 910 | } |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 911 | this.setZoomInternal_(nextZoom); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 912 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 913 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 914 | }, |
| 915 | |
| 916 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 917 | * Pinch zoom event handler. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 918 | * |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 919 | * @param {!Object} e The pinch event. |
| 920 | */ |
| 921 | pinchZoom: function(e) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 922 | this.mightZoom_(() => { |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 923 | this.pinchPhase_ = e.direction == 'out' ? |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 924 | Viewport.PinchPhase.PINCH_UPDATE_ZOOM_OUT : |
| 925 | Viewport.PinchPhase.PINCH_UPDATE_ZOOM_IN; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 926 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 927 | const scaleDelta = e.startScaleRatio / this.prevScale_; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 928 | this.pinchPanVector_ = |
| 929 | vectorDelta(e.center, this.firstPinchCenterInFrame_); |
| 930 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 931 | const needsScrollbars = |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 932 | this.documentNeedsScrollbars_(this.zoomManager_.applyBrowserZoom( |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 933 | Viewport.clampZoom(this.internalZoom_ * scaleDelta))); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 934 | |
| 935 | this.pinchCenter_ = e.center; |
| 936 | |
| 937 | // If there's no horizontal scrolling, keep the content centered so the |
| 938 | // user can't zoom in on the non-content area. |
| 939 | // TODO(mcnee) Investigate other ways of scaling when we don't have |
| 940 | // horizontal scrolling. We want to keep the document centered, |
| 941 | // but this causes a potentially awkward transition when we start |
| 942 | // using the gesture center. |
| 943 | if (!needsScrollbars.horizontal) { |
| 944 | this.pinchCenter_ = { |
| 945 | x: this.window_.innerWidth / 2, |
| 946 | y: this.window_.innerHeight / 2 |
| 947 | }; |
| 948 | } else if (this.keepContentCentered_) { |
| 949 | this.oldCenterInContent = |
| 950 | this.frameToContent(frameToPluginCoordinate(e.center)); |
| 951 | this.keepContentCentered_ = false; |
| 952 | } |
| 953 | |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 954 | this.setPinchZoomInternal_(scaleDelta, frameToPluginCoordinate(e.center)); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 955 | this.updateViewport_(); |
| 956 | this.prevScale_ = e.startScaleRatio; |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 957 | }); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 958 | }, |
| 959 | |
| 960 | pinchZoomStart: function(e) { |
| 961 | this.pinchPhase_ = Viewport.PinchPhase.PINCH_START; |
| 962 | this.prevScale_ = 1; |
| 963 | this.oldCenterInContent = |
| 964 | this.frameToContent(frameToPluginCoordinate(e.center)); |
| 965 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 966 | const needsScrollbars = this.documentNeedsScrollbars_(this.zoom); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 967 | this.keepContentCentered_ = !needsScrollbars.horizontal; |
| 968 | // We keep track of begining of the pinch. |
| 969 | // By doing so we will be able to compute the pan distance. |
| 970 | this.firstPinchCenterInFrame_ = e.center; |
| 971 | }, |
| 972 | |
| 973 | pinchZoomEnd: function(e) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 974 | this.mightZoom_(() => { |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 975 | this.pinchPhase_ = Viewport.PinchPhase.PINCH_END; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 976 | const scaleDelta = e.startScaleRatio / this.prevScale_; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 977 | this.pinchCenter_ = e.center; |
| 978 | |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 979 | this.setPinchZoomInternal_(scaleDelta, frameToPluginCoordinate(e.center)); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 980 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 981 | }); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 982 | |
| 983 | this.pinchPhase_ = Viewport.PinchPhase.PINCH_NONE; |
| 984 | this.pinchPanVector_ = null; |
| 985 | this.pinchCenter_ = null; |
| 986 | this.firstPinchCenterInFrame_ = null; |
| 987 | }, |
| 988 | |
| 989 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 990 | * Go to the given page index. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 991 | * |
[email protected] | f90b9a4 | 2014-08-20 05:37:34 | [diff] [blame] | 992 | * @param {number} page the index of the page to go to. zero-based. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 993 | */ |
| 994 | goToPage: function(page) { |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 995 | this.goToPageAndXY(page, 0, 0); |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 996 | }, |
| 997 | |
| 998 | /** |
| 999 | * Go to the given y position in the given page index. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 1000 | * |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 1001 | * @param {number} page the index of the page to go to. zero-based. |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 1002 | * @param {number} x the x position in the page to go to. |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 1003 | * @param {number} y the y position in the page to go to. |
| 1004 | */ |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 1005 | goToPageAndXY: function(page, x, y) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 1006 | this.mightZoom_(() => { |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 1007 | if (this.pageDimensions_.length === 0) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 1008 | return; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 1009 | } |
| 1010 | if (page < 0) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 1011 | page = 0; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 1012 | } |
| 1013 | if (page >= this.pageDimensions_.length) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 1014 | page = this.pageDimensions_.length - 1; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 1015 | } |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 1016 | const dimensions = this.pageDimensions_[page]; |
| 1017 | let toolbarOffset = 0; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 1018 | // Unless we're in fit to page or fit to height mode, scroll above the |
| 1019 | // page by |this.topToolbarHeight_| so that the toolbar isn't covering it |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 1020 | // initially. |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 1021 | if (!this.isPagedMode()) { |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 1022 | toolbarOffset = this.topToolbarHeight_; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 1023 | } |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 1024 | this.position = { |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 1025 | x: (dimensions.x + x) * this.zoom, |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 1026 | y: (dimensions.y + y) * this.zoom - toolbarOffset |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 1027 | }; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 1028 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 1029 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 1030 | }, |
| 1031 | |
| 1032 | /** |
| 1033 | * Set the dimensions of the document. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 1034 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 1035 | * @param {Object} documentDimensions the dimensions of the document |
| 1036 | */ |
| 1037 | setDocumentDimensions: function(documentDimensions) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 1038 | this.mightZoom_(() => { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 1039 | const initialDimensions = !this.documentDimensions_; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 1040 | this.documentDimensions_ = documentDimensions; |
| 1041 | this.pageDimensions_ = this.documentDimensions_.pageDimensions; |
| 1042 | if (initialDimensions) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 1043 | this.setZoomInternal_(Math.min( |
| 1044 | this.defaultZoom_, |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 1045 | this.computeFittingZoom_(this.documentDimensions_, true, false))); |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 1046 | this.position = {x: 0, y: -this.topToolbarHeight_}; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 1047 | } |
| 1048 | this.contentSizeChanged_(); |
| 1049 | this.resize_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 1050 | }); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1051 | }, |
| 1052 | |
| 1053 | /** |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 1054 | * @param {number} page |
| 1055 | * @return {Rect} The bounds for page `page` minus the shadows. |
| 1056 | */ |
| 1057 | getPageInsetDimensions: function(page) { |
| 1058 | const pageDimensions = this.pageDimensions_[page]; |
| 1059 | const shadow = Viewport.PAGE_SHADOW; |
| 1060 | return { |
| 1061 | x: pageDimensions.x + shadow.left, |
| 1062 | y: pageDimensions.y + shadow.top, |
| 1063 | width: pageDimensions.width - shadow.left - shadow.right, |
| 1064 | height: pageDimensions.height - shadow.top - shadow.bottom, |
| 1065 | }; |
| 1066 | }, |
| 1067 | |
| 1068 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1069 | * Get the coordinates of the page contents (excluding the page shadow) |
| 1070 | * relative to the screen. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 1071 | * |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1072 | * @param {number} page the index of the page to get the rect for. |
| 1073 | * @return {Object} a rect representing the page in screen coordinates. |
| 1074 | */ |
| 1075 | getPageScreenRect: function(page) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 1076 | if (!this.documentDimensions_) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 1077 | return {x: 0, y: 0, width: 0, height: 0}; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 1078 | } |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 1079 | if (page >= this.pageDimensions_.length) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1080 | page = this.pageDimensions_.length - 1; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 1081 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1082 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 1083 | const pageDimensions = this.pageDimensions_[page]; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1084 | |
| 1085 | // Compute the page dimensions minus the shadows. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 1086 | const insetDimensions = this.getPageInsetDimensions(page); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1087 | |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 1088 | // Compute the x-coordinate of the page within the document. |
| 1089 | // TODO(raymes): This should really be set when the PDF plugin passes the |
| 1090 | // page coordinates, but it isn't yet. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 1091 | const x = (this.documentDimensions_.width - pageDimensions.width) / 2 + |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 1092 | Viewport.PAGE_SHADOW.left; |
| 1093 | // Compute the space on the left of the document if the document fits |
| 1094 | // completely in the screen. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 1095 | let spaceOnLeft = |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 1096 | (this.size.width - this.documentDimensions_.width * this.zoom) / 2; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 1097 | spaceOnLeft = Math.max(spaceOnLeft, 0); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1098 | |
| 1099 | return { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 1100 | x: x * this.zoom + spaceOnLeft - this.window_.pageXOffset, |
| 1101 | y: insetDimensions.y * this.zoom - this.window_.pageYOffset, |
| 1102 | width: insetDimensions.width * this.zoom, |
| 1103 | height: insetDimensions.height * this.zoom |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1104 | }; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 1105 | }, |
| 1106 | |
| 1107 | /** |
| 1108 | * Check if the current fitting type is a paged mode. |
| 1109 | * |
| 1110 | * In a paged mode, page up and page down scroll to the top of the |
| 1111 | * previous/next page and part of the page is under the toolbar. |
| 1112 | * |
| 1113 | * @return {boolean} Whether the current fitting type is a paged mode. |
| 1114 | */ |
| 1115 | isPagedMode: function(page) { |
| 1116 | return ( |
| 1117 | this.fittingType_ == FittingType.FIT_TO_PAGE || |
| 1118 | this.fittingType_ == FittingType.FIT_TO_HEIGHT); |
Henrique Nakashima | 585c7af0 | 2018-03-27 04:55:21 | [diff] [blame] | 1119 | }, |
| 1120 | |
| 1121 | /** |
| 1122 | * Scroll the viewport to the specified position. |
| 1123 | * |
| 1124 | * @param {!PartialPoint} point The position to which to move the viewport. |
| 1125 | */ |
| 1126 | scrollTo: function(point) { |
| 1127 | let changed = false; |
| 1128 | const newPosition = this.position; |
| 1129 | if (point.x !== undefined && point.x != newPosition.x) { |
| 1130 | newPosition.x = point.x; |
| 1131 | changed = true; |
| 1132 | } |
| 1133 | if (point.y !== undefined && point.y != newPosition.y) { |
| 1134 | newPosition.y = point.y; |
| 1135 | changed = true; |
| 1136 | } |
| 1137 | |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 1138 | if (changed) { |
Henrique Nakashima | 585c7af0 | 2018-03-27 04:55:21 | [diff] [blame] | 1139 | this.position = newPosition; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame] | 1140 | } |
Henrique Nakashima | 585c7af0 | 2018-03-27 04:55:21 | [diff] [blame] | 1141 | }, |
| 1142 | |
| 1143 | /** |
| 1144 | * Scroll the viewport by the specified delta. |
| 1145 | * |
| 1146 | * @param {!Point} delta The delta by which to move the viewport. |
| 1147 | */ |
| 1148 | scrollBy: function(delta) { |
| 1149 | const newPosition = this.position; |
| 1150 | newPosition.x += delta.x; |
| 1151 | newPosition.y += delta.y; |
| 1152 | this.scrollTo(newPosition); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 1153 | } |
| 1154 | }; |