[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) { |
| 248 | if (!this.documentDimensions_) |
| 249 | return null; |
| 250 | return { |
| 251 | width: Math.round(this.documentDimensions_.width * zoom), |
| 252 | height: Math.round(this.documentDimensions_.height * zoom) |
| 253 | }; |
| 254 | }, |
| 255 | |
| 256 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 257 | * Returns true if the document needs scrollbars at the given zoom level. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 258 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 259 | * @param {number} zoom compute whether scrollbars are needed at this zoom |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 260 | * @return {Object} with 'horizontal' and 'vertical' keys which map to bool |
| 261 | * values indicating if the horizontal and vertical scrollbars are needed |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 262 | * respectively. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 263 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 264 | */ |
| 265 | documentNeedsScrollbars_: function(zoom) { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 266 | const zoomedDimensions = this.getZoomedDocumentDimensions_(zoom); |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 267 | if (!zoomedDimensions) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 268 | return {horizontal: false, vertical: false}; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 269 | } |
sammc | 63334fed | 2014-11-10 03:07:35 | [diff] [blame] | 270 | |
| 271 | // If scrollbars are required for one direction, expand the document in the |
| 272 | // other direction to take the width of the scrollbars into account when |
| 273 | // deciding whether the other direction needs scrollbars. |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 274 | if (zoomedDimensions.width > this.window_.innerWidth) |
| 275 | zoomedDimensions.height += this.scrollbarWidth_; |
| 276 | else if (zoomedDimensions.height > this.window_.innerHeight) |
| 277 | zoomedDimensions.width += this.scrollbarWidth_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 278 | return { |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 279 | horizontal: zoomedDimensions.width > this.window_.innerWidth, |
raymes | 051fb2c | 2015-09-21 04:56:41 | [diff] [blame] | 280 | vertical: zoomedDimensions.height + this.topToolbarHeight_ > |
| 281 | this.window_.innerHeight |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 282 | }; |
| 283 | }, |
| 284 | |
| 285 | /** |
| 286 | * Returns true if the document needs scrollbars at the current zoom level. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 287 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 288 | * @return {Object} with 'x' and 'y' keys which map to bool values |
| 289 | * indicating if the horizontal and vertical scrollbars are needed |
| 290 | * respectively. |
| 291 | */ |
| 292 | documentHasScrollbars: function() { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 293 | return this.documentNeedsScrollbars_(this.zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 294 | }, |
| 295 | |
| 296 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 297 | * Helper function called when the zoomed document size changes. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 298 | * |
| 299 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 300 | */ |
| 301 | contentSizeChanged_: function() { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 302 | const zoomedDimensions = this.getZoomedDocumentDimensions_(this.zoom); |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 303 | if (zoomedDimensions) { |
| 304 | this.sizer_.style.width = zoomedDimensions.width + 'px'; |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 305 | this.sizer_.style.height = |
| 306 | zoomedDimensions.height + this.topToolbarHeight_ + 'px'; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 307 | } |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 308 | }, |
| 309 | |
| 310 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 311 | * Called when the viewport should be updated. |
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 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 315 | updateViewport_: function() { |
| 316 | this.viewportChangedCallback_(); |
| 317 | }, |
| 318 | |
| 319 | /** |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 320 | * Called when the browser window size changes. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 321 | * |
| 322 | * @private |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 323 | */ |
| 324 | resizeWrapper_: function() { |
| 325 | this.setUserInitiatedCallback_(false); |
| 326 | this.resize_(); |
| 327 | this.setUserInitiatedCallback_(true); |
| 328 | }, |
| 329 | |
| 330 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 331 | * Called when the viewport size changes. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 332 | * |
| 333 | * @private |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 334 | */ |
| 335 | resize_: function() { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 336 | if (this.fittingType_ == FittingType.FIT_TO_PAGE) |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 337 | this.fitToPageInternal_(false); |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 338 | else if (this.fittingType_ == FittingType.FIT_TO_WIDTH) |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 339 | this.fitToWidth(); |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 340 | else if (this.fittingType_ == FittingType.FIT_TO_HEIGHT) |
| 341 | this.fitToHeightInternal_(false); |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 342 | else if (this.internalZoom_ == 0) |
| 343 | this.fitToNone(); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 344 | else |
| 345 | this.updateViewport_(); |
| 346 | }, |
| 347 | |
| 348 | /** |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 349 | * @type {Point} the scroll position of the viewport. |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 350 | */ |
| 351 | get position() { |
| 352 | return { |
| 353 | x: this.window_.pageXOffset, |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 354 | y: this.window_.pageYOffset - this.topToolbarHeight_ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 355 | }; |
| 356 | }, |
| 357 | |
| 358 | /** |
| 359 | * Scroll the viewport to the specified position. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 360 | * |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 361 | * @type {Point} position The position to scroll to. |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 362 | */ |
| 363 | set position(position) { |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 364 | this.window_.scrollTo(position.x, position.y + this.topToolbarHeight_); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 365 | }, |
| 366 | |
| 367 | /** |
| 368 | * @type {Object} the size of the viewport excluding scrollbars. |
| 369 | */ |
| 370 | get size() { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 371 | const needsScrollbars = this.documentNeedsScrollbars_(this.zoom); |
| 372 | const scrollbarWidth = needsScrollbars.vertical ? this.scrollbarWidth_ : 0; |
| 373 | const scrollbarHeight = |
| 374 | needsScrollbars.horizontal ? this.scrollbarWidth_ : 0; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 375 | return { |
| 376 | width: this.window_.innerWidth - scrollbarWidth, |
| 377 | height: this.window_.innerHeight - scrollbarHeight |
| 378 | }; |
| 379 | }, |
| 380 | |
| 381 | /** |
| 382 | * @type {number} the zoom level of the viewport. |
| 383 | */ |
| 384 | get zoom() { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 385 | return this.zoomManager_.applyBrowserZoom(this.internalZoom_); |
| 386 | }, |
| 387 | |
| 388 | /** |
| 389 | * Set the zoom manager. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 390 | * |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 391 | * @type {ZoomManager} manager the zoom manager to set. |
| 392 | */ |
| 393 | set zoomManager(manager) { |
| 394 | this.zoomManager_ = manager; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 395 | }, |
| 396 | |
| 397 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 398 | * @type {Viewport.PinchPhase} The phase of the current pinch gesture for |
| 399 | * the viewport. |
| 400 | */ |
| 401 | get pinchPhase() { |
| 402 | return this.pinchPhase_; |
| 403 | }, |
| 404 | |
| 405 | /** |
| 406 | * @type {Object} The panning caused by the current pinch gesture (as |
| 407 | * the deltas of the x and y coordinates). |
| 408 | */ |
| 409 | get pinchPanVector() { |
| 410 | return this.pinchPanVector_; |
| 411 | }, |
| 412 | |
| 413 | /** |
| 414 | * @type {Object} The coordinates of the center of the current pinch gesture. |
| 415 | */ |
| 416 | get pinchCenter() { |
| 417 | return this.pinchCenter_; |
| 418 | }, |
| 419 | |
| 420 | /** |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 421 | * Used to wrap a function that might perform zooming on the viewport. This is |
| 422 | * required so that we can notify the plugin that zooming is in progress |
| 423 | * so that while zooming is taking place it can stop reacting to scroll events |
| 424 | * from the viewport. This is to avoid flickering. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 425 | * |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 426 | * @param {Function} f Function to wrap |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 427 | * @private |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 428 | */ |
| 429 | mightZoom_: function(f) { |
| 430 | this.beforeZoomCallback_(); |
| 431 | this.allowedToChangeZoom_ = true; |
| 432 | f(); |
| 433 | this.allowedToChangeZoom_ = false; |
| 434 | this.afterZoomCallback_(); |
| 435 | }, |
| 436 | |
| 437 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 438 | * Sets the zoom of the viewport. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 439 | * |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 440 | * @param {number} newZoom the zoom level to zoom to. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 441 | * @private |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 442 | */ |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 443 | setZoomInternal_: function(newZoom) { |
dstockwell | 154f9c4 | 2019-01-01 23:36:34 | [diff] [blame^] | 444 | assert( |
| 445 | this.allowedToChangeZoom_, |
| 446 | 'Called Viewport.setZoomInternal_ without calling ' + |
| 447 | 'Viewport.mightZoom_.'); |
sammc | 5c33b7e | 2014-11-07 04:03:09 | [diff] [blame] | 448 | // Record the scroll position (relative to the top-left of the window). |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 449 | const currentScrollPos = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 450 | x: this.position.x / this.zoom, |
| 451 | y: this.position.y / this.zoom |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 452 | }; |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 453 | |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 454 | this.internalZoom_ = newZoom; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 455 | this.contentSizeChanged_(); |
| 456 | // Scroll to the scaled scroll position. |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 457 | this.position = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 458 | x: currentScrollPos.x * this.zoom, |
| 459 | y: currentScrollPos.y * this.zoom |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 460 | }; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 461 | }, |
| 462 | |
| 463 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 464 | * Sets the zoom of the viewport. |
| 465 | * Same as setZoomInternal_ but for pinch zoom we have some more operations. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 466 | * |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 467 | * @param {number} scaleDelta The zoom delta. |
| 468 | * @param {!Object} center The pinch center in content coordinates. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 469 | * @private |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 470 | */ |
| 471 | setPinchZoomInternal_: function(scaleDelta, center) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 472 | assert( |
| 473 | this.allowedToChangeZoom_, |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 474 | 'Called Viewport.setPinchZoomInternal_ without calling ' + |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 475 | 'Viewport.mightZoom_.'); |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 476 | this.internalZoom_ = Viewport.clampZoom(this.internalZoom_ * scaleDelta); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 477 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 478 | const newCenterInContent = this.frameToContent(center); |
| 479 | const delta = { |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 480 | x: (newCenterInContent.x - this.oldCenterInContent.x), |
| 481 | y: (newCenterInContent.y - this.oldCenterInContent.y) |
| 482 | }; |
| 483 | |
| 484 | // Record the scroll position (relative to the pinch center). |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 485 | const currentScrollPos = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 486 | x: this.position.x - delta.x * this.zoom, |
| 487 | y: this.position.y - delta.y * this.zoom |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 488 | }; |
| 489 | |
| 490 | this.contentSizeChanged_(); |
| 491 | // Scroll to the scaled scroll position. |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 492 | this.position = {x: currentScrollPos.x, y: currentScrollPos.y}; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 493 | }, |
| 494 | |
| 495 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 496 | * Converts a point from frame to content coordinates. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 497 | * |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 498 | * @param {!Object} framePoint The frame coordinates. |
| 499 | * @return {!Object} The content coordinates. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 500 | * @private |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 501 | */ |
| 502 | frameToContent: function(framePoint) { |
| 503 | // TODO(mcnee) Add a helper Point class to avoid duplicating operations |
| 504 | // on plain {x,y} objects. |
| 505 | return { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 506 | x: (framePoint.x + this.position.x) / this.zoom, |
| 507 | y: (framePoint.y + this.position.y) / this.zoom |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 508 | }; |
| 509 | }, |
| 510 | |
| 511 | /** |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 512 | * Sets the zoom to the given zoom level. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 513 | * |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 514 | * @param {number} newZoom the zoom level to zoom to. |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 515 | */ |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 516 | setZoom: function(newZoom) { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 517 | this.fittingType_ = FittingType.NONE; |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 518 | this.mightZoom_(() => { |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 519 | this.setZoomInternal_(Viewport.clampZoom(newZoom)); |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 520 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 521 | }); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 522 | }, |
| 523 | |
| 524 | /** |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 525 | * Gets notified of the browser zoom changing seperately from the |
| 526 | * internal zoom. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 527 | * |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 528 | * @param {number} oldBrowserZoom the previous value of the browser zoom. |
| 529 | */ |
| 530 | updateZoomFromBrowserChange: function(oldBrowserZoom) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 531 | this.mightZoom_(() => { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 532 | // Record the scroll position (relative to the top-left of the window). |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 533 | const oldZoom = oldBrowserZoom * this.internalZoom_; |
| 534 | const currentScrollPos = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 535 | x: this.position.x / oldZoom, |
| 536 | y: this.position.y / oldZoom |
| 537 | }; |
| 538 | this.contentSizeChanged_(); |
| 539 | // Scroll to the scaled scroll position. |
| 540 | this.position = { |
| 541 | x: currentScrollPos.x * this.zoom, |
| 542 | y: currentScrollPos.y * this.zoom |
| 543 | }; |
| 544 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 545 | }); |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 546 | }, |
| 547 | |
| 548 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 549 | * @type {number} the width of scrollbars in the viewport in pixels. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 550 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 551 | get scrollbarWidth() { |
| 552 | return this.scrollbarWidth_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 553 | }, |
| 554 | |
| 555 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 556 | * @type {FittingType} the fitting type the viewport is currently in. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 557 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 558 | get fittingType() { |
| 559 | return this.fittingType_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 560 | }, |
| 561 | |
| 562 | /** |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 563 | * Get the which page is at a given y position. |
| 564 | * |
dpapad | d2d6477 | 2017-05-19 02:30:38 | [diff] [blame] | 565 | * @param {number} y the y-coordinate to get the page at. |
| 566 | * @return {number} the index of a page overlapping the given y-coordinate. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 567 | * @private |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 568 | */ |
| 569 | getPageAtY_: function(y) { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 570 | let min = 0; |
| 571 | let max = this.pageDimensions_.length - 1; |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 572 | while (max >= min) { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 573 | const page = Math.floor(min + ((max - min) / 2)); |
[email protected] | 13df2a4 | 2014-02-27 03:50:41 | [diff] [blame] | 574 | // There might be a gap between the pages, in which case use the bottom |
| 575 | // of the previous page as the top for finding the page. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 576 | let top = 0; |
[email protected] | 13df2a4 | 2014-02-27 03:50:41 | [diff] [blame] | 577 | if (page > 0) { |
| 578 | top = this.pageDimensions_[page - 1].y + |
| 579 | this.pageDimensions_[page - 1].height; |
| 580 | } |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 581 | const bottom = |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 582 | this.pageDimensions_[page].y + this.pageDimensions_[page].height; |
[email protected] | 13df2a4 | 2014-02-27 03:50:41 | [diff] [blame] | 583 | |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 584 | if (top <= y && bottom > y) |
| 585 | return page; |
Lei Zhang | ddc0ef03 | 2017-11-22 02:14:24 | [diff] [blame] | 586 | |
| 587 | if (top > y) |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 588 | max = page - 1; |
| 589 | else |
| 590 | min = page + 1; |
| 591 | } |
| 592 | return 0; |
| 593 | }, |
| 594 | |
| 595 | /** |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 596 | * Returns the page with the greatest proportion of its height in the current |
| 597 | * viewport. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 598 | * |
dpapad | d2d6477 | 2017-05-19 02:30:38 | [diff] [blame] | 599 | * @return {number} the index of the most visible page. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 600 | */ |
| 601 | getMostVisiblePage: function() { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 602 | const firstVisiblePage = this.getPageAtY_(this.position.y / this.zoom); |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 603 | if (firstVisiblePage == this.pageDimensions_.length - 1) |
| 604 | return firstVisiblePage; |
| 605 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 606 | const viewportRect = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 607 | x: this.position.x / this.zoom, |
| 608 | y: this.position.y / this.zoom, |
| 609 | width: this.size.width / this.zoom, |
| 610 | height: this.size.height / this.zoom |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 611 | }; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 612 | const firstVisiblePageVisibility = |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 613 | getIntersectionHeight( |
| 614 | this.pageDimensions_[firstVisiblePage], viewportRect) / |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 615 | this.pageDimensions_[firstVisiblePage].height; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 616 | const nextPageVisibility = |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 617 | getIntersectionHeight( |
| 618 | this.pageDimensions_[firstVisiblePage + 1], viewportRect) / |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 619 | this.pageDimensions_[firstVisiblePage + 1].height; |
| 620 | if (nextPageVisibility > firstVisiblePageVisibility) |
| 621 | return firstVisiblePage + 1; |
| 622 | return firstVisiblePage; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 623 | }, |
| 624 | |
| 625 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 626 | * Compute the zoom level for fit-to-page, fit-to-width or fit-to-height. |
| 627 | * |
| 628 | * At least one of {fitWidth, fitHeight} must be true. |
| 629 | * |
| 630 | * @param {Object} pageDimensions the dimensions of a given page in px. |
| 631 | * @param {boolean} fitWidth a bool indicating whether the whole width of the |
| 632 | * page needs to be in the viewport. |
| 633 | * @param {boolean} fitHeight a bool indicating whether the whole height of |
| 634 | * the page needs to be in the viewport. |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 635 | * @return {number} the internal zoom to set |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 636 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 637 | */ |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 638 | computeFittingZoom_: function(pageDimensions, fitWidth, fitHeight) { |
| 639 | assert( |
| 640 | fitWidth || fitHeight, |
| 641 | 'Invalid parameters. At least one of fitWidth and fitHeight must be ' + |
| 642 | 'true.'); |
| 643 | |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 644 | // First compute the zoom without scrollbars. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 645 | let zoom = this.computeFittingZoomGivenDimensions_( |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 646 | fitWidth, fitHeight, this.window_.innerWidth, this.window_.innerHeight, |
| 647 | pageDimensions.width, pageDimensions.height); |
| 648 | |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 649 | // Check if there needs to be any scrollbars. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 650 | const needsScrollbars = this.documentNeedsScrollbars_(zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 651 | |
| 652 | // If the document fits, just return the zoom. |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 653 | if (!needsScrollbars.horizontal && !needsScrollbars.vertical) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 654 | return zoom; |
| 655 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 656 | const zoomedDimensions = this.getZoomedDocumentDimensions_(zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 657 | |
| 658 | // Check if adding a scrollbar will result in needing the other scrollbar. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 659 | const scrollbarWidth = this.scrollbarWidth_; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 660 | if (needsScrollbars.horizontal && |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 661 | zoomedDimensions.height > this.window_.innerHeight - scrollbarWidth) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 662 | needsScrollbars.vertical = true; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 663 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 664 | if (needsScrollbars.vertical && |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 665 | zoomedDimensions.width > this.window_.innerWidth - scrollbarWidth) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 666 | needsScrollbars.horizontal = true; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | // Compute available window space. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 670 | const windowWithScrollbars = { |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 671 | width: this.window_.innerWidth, |
| 672 | height: this.window_.innerHeight |
| 673 | }; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 674 | if (needsScrollbars.horizontal) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 675 | windowWithScrollbars.height -= scrollbarWidth; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 676 | if (needsScrollbars.vertical) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 677 | windowWithScrollbars.width -= scrollbarWidth; |
| 678 | |
| 679 | // Recompute the zoom. |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 680 | zoom = this.computeFittingZoomGivenDimensions_( |
| 681 | fitWidth, fitHeight, windowWithScrollbars.width, |
| 682 | windowWithScrollbars.height, pageDimensions.width, |
| 683 | pageDimensions.height); |
| 684 | |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 685 | return this.zoomManager_.internalZoomComponent(zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 686 | }, |
| 687 | |
| 688 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 689 | * Compute a zoom level given the dimensions to fit and the actual numbers |
| 690 | * in those dimensions. |
| 691 | * |
| 692 | * @param {boolean} fitWidth make sure the page width is totally contained in |
| 693 | * the window. |
| 694 | * @param {boolean} fitHeight make sure the page height is totally contained |
| 695 | * in the window. |
| 696 | * @param {number} windowWidth the width of the window in px. |
| 697 | * @param {number} windowHeight the height of the window in px. |
| 698 | * @param {number} pageWidth the width of the page in px. |
| 699 | * @param {number} pageHeight the height of the page in px. |
| 700 | * @return {number} the internal zoom to set |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 701 | * @private |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 702 | */ |
| 703 | computeFittingZoomGivenDimensions_: function( |
| 704 | fitWidth, fitHeight, windowWidth, windowHeight, pageWidth, pageHeight) { |
| 705 | // Assumes at least one of {fitWidth, fitHeight} is set. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 706 | let zoomWidth; |
| 707 | let zoomHeight; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 708 | |
| 709 | if (fitWidth) |
| 710 | zoomWidth = windowWidth / pageWidth; |
| 711 | |
| 712 | if (fitHeight) |
| 713 | zoomHeight = windowHeight / pageHeight; |
| 714 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 715 | let zoom; |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 716 | if (!fitWidth && fitHeight) { |
| 717 | zoom = zoomHeight; |
| 718 | } else if (fitWidth && !fitHeight) { |
| 719 | zoom = zoomWidth; |
| 720 | } else { |
| 721 | // Assume fitWidth && fitHeight |
| 722 | zoom = Math.min(zoomWidth, zoomHeight); |
| 723 | } |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 724 | |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 725 | return Math.max(zoom, 0); |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 726 | }, |
| 727 | |
| 728 | /** |
| 729 | * Zoom the viewport so that the page width consumes the entire viewport. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 730 | */ |
| 731 | fitToWidth: function() { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 732 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 733 | this.fittingType_ = FittingType.FIT_TO_WIDTH; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 734 | if (!this.documentDimensions_) |
| 735 | return; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 736 | // When computing fit-to-width, the maximum width of a page in the |
| 737 | // document is used, which is equal to the size of the document width. |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 738 | this.setZoomInternal_( |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 739 | this.computeFittingZoom_(this.documentDimensions_, true, false)); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 740 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 741 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 742 | }, |
| 743 | |
| 744 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 745 | * Zoom the viewport so that the page height consumes the entire viewport. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 746 | * |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 747 | * @param {boolean} scrollToTopOfPage Set to true if the viewport should be |
| 748 | * scrolled to the top of the current page. Set to false if the viewport |
| 749 | * should remain at the current scroll position. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 750 | * @private |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 751 | */ |
| 752 | fitToHeightInternal_: function(scrollToTopOfPage) { |
| 753 | this.mightZoom_(() => { |
| 754 | this.fittingType_ = FittingType.FIT_TO_HEIGHT; |
| 755 | if (!this.documentDimensions_) |
| 756 | return; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 757 | const page = this.getMostVisiblePage(); |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 758 | // When computing fit-to-height, the maximum height of the current page |
| 759 | // is used. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 760 | const dimensions = { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 761 | width: 0, |
| 762 | height: this.pageDimensions_[page].height, |
| 763 | }; |
| 764 | this.setZoomInternal_(this.computeFittingZoom_(dimensions, false, true)); |
| 765 | if (scrollToTopOfPage) { |
| 766 | this.position = {x: 0, y: this.pageDimensions_[page].y * this.zoom}; |
| 767 | } |
| 768 | this.updateViewport_(); |
| 769 | }); |
| 770 | }, |
| 771 | |
| 772 | /** |
| 773 | * Zoom the viewport so that the page height consumes the entire viewport. |
| 774 | */ |
| 775 | fitToHeight: function() { |
| 776 | this.fitToHeightInternal_(true); |
| 777 | }, |
| 778 | |
| 779 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 780 | * 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] | 781 | * |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 782 | * @param {boolean} scrollToTopOfPage Set to true if the viewport should be |
| 783 | * scrolled to the top of the current page. Set to false if the viewport |
| 784 | * should remain at the current scroll position. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 785 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 786 | */ |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 787 | fitToPageInternal_: function(scrollToTopOfPage) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 788 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 789 | this.fittingType_ = FittingType.FIT_TO_PAGE; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 790 | if (!this.documentDimensions_) |
| 791 | return; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 792 | const page = this.getMostVisiblePage(); |
sammc | 07f53aa | 2014-11-06 06:57:46 | [diff] [blame] | 793 | // Fit to the current page's height and the widest page's width. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 794 | const dimensions = { |
sammc | 07f53aa | 2014-11-06 06:57:46 | [diff] [blame] | 795 | width: this.documentDimensions_.width, |
| 796 | height: this.pageDimensions_[page].height, |
| 797 | }; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 798 | this.setZoomInternal_(this.computeFittingZoom_(dimensions, true, true)); |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 799 | if (scrollToTopOfPage) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 800 | this.position = {x: 0, y: this.pageDimensions_[page].y * this.zoom}; |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 801 | } |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 802 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 803 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 804 | }, |
| 805 | |
| 806 | /** |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 807 | * Zoom the viewport so that a page consumes the entire viewport. Also scrolls |
| 808 | * the viewport to the top of the current page. |
| 809 | */ |
| 810 | fitToPage: function() { |
| 811 | this.fitToPageInternal_(true); |
| 812 | }, |
| 813 | |
| 814 | /** |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame] | 815 | * Zoom the viewport to the default zoom policy. |
| 816 | */ |
| 817 | fitToNone: function() { |
| 818 | this.mightZoom_(() => { |
| 819 | this.fittingType_ = FittingType.NONE; |
| 820 | if (!this.documentDimensions_) |
| 821 | return; |
| 822 | this.setZoomInternal_(Math.min( |
| 823 | this.defaultZoom_, |
| 824 | this.computeFittingZoom_(this.documentDimensions_, true, false))); |
| 825 | this.updateViewport_(); |
| 826 | }); |
| 827 | }, |
| 828 | |
| 829 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 830 | * Zoom out to the next predefined zoom level. |
| 831 | */ |
| 832 | zoomOut: function() { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 833 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 834 | this.fittingType_ = FittingType.NONE; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 835 | let nextZoom = Viewport.ZOOM_FACTORS[0]; |
| 836 | for (let i = 0; i < Viewport.ZOOM_FACTORS.length; i++) { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 837 | if (Viewport.ZOOM_FACTORS[i] < this.internalZoom_) |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 838 | nextZoom = Viewport.ZOOM_FACTORS[i]; |
| 839 | } |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 840 | this.setZoomInternal_(nextZoom); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 841 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 842 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 843 | }, |
| 844 | |
| 845 | /** |
| 846 | * Zoom in to the next predefined zoom level. |
| 847 | */ |
| 848 | zoomIn: function() { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 849 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 850 | this.fittingType_ = FittingType.NONE; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 851 | let nextZoom = Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1]; |
| 852 | for (let i = Viewport.ZOOM_FACTORS.length - 1; i >= 0; i--) { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 853 | if (Viewport.ZOOM_FACTORS[i] > this.internalZoom_) |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 854 | nextZoom = Viewport.ZOOM_FACTORS[i]; |
| 855 | } |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 856 | this.setZoomInternal_(nextZoom); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 857 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 858 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 859 | }, |
| 860 | |
| 861 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 862 | * Pinch zoom event handler. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 863 | * |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 864 | * @param {!Object} e The pinch event. |
| 865 | */ |
| 866 | pinchZoom: function(e) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 867 | this.mightZoom_(() => { |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 868 | this.pinchPhase_ = e.direction == 'out' ? |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 869 | Viewport.PinchPhase.PINCH_UPDATE_ZOOM_OUT : |
| 870 | Viewport.PinchPhase.PINCH_UPDATE_ZOOM_IN; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 871 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 872 | const scaleDelta = e.startScaleRatio / this.prevScale_; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 873 | this.pinchPanVector_ = |
| 874 | vectorDelta(e.center, this.firstPinchCenterInFrame_); |
| 875 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 876 | const needsScrollbars = |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 877 | this.documentNeedsScrollbars_(this.zoomManager_.applyBrowserZoom( |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 878 | Viewport.clampZoom(this.internalZoom_ * scaleDelta))); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 879 | |
| 880 | this.pinchCenter_ = e.center; |
| 881 | |
| 882 | // If there's no horizontal scrolling, keep the content centered so the |
| 883 | // user can't zoom in on the non-content area. |
| 884 | // TODO(mcnee) Investigate other ways of scaling when we don't have |
| 885 | // horizontal scrolling. We want to keep the document centered, |
| 886 | // but this causes a potentially awkward transition when we start |
| 887 | // using the gesture center. |
| 888 | if (!needsScrollbars.horizontal) { |
| 889 | this.pinchCenter_ = { |
| 890 | x: this.window_.innerWidth / 2, |
| 891 | y: this.window_.innerHeight / 2 |
| 892 | }; |
| 893 | } else if (this.keepContentCentered_) { |
| 894 | this.oldCenterInContent = |
| 895 | this.frameToContent(frameToPluginCoordinate(e.center)); |
| 896 | this.keepContentCentered_ = false; |
| 897 | } |
| 898 | |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 899 | this.setPinchZoomInternal_(scaleDelta, frameToPluginCoordinate(e.center)); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 900 | this.updateViewport_(); |
| 901 | this.prevScale_ = e.startScaleRatio; |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 902 | }); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 903 | }, |
| 904 | |
| 905 | pinchZoomStart: function(e) { |
| 906 | this.pinchPhase_ = Viewport.PinchPhase.PINCH_START; |
| 907 | this.prevScale_ = 1; |
| 908 | this.oldCenterInContent = |
| 909 | this.frameToContent(frameToPluginCoordinate(e.center)); |
| 910 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 911 | const needsScrollbars = this.documentNeedsScrollbars_(this.zoom); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 912 | this.keepContentCentered_ = !needsScrollbars.horizontal; |
| 913 | // We keep track of begining of the pinch. |
| 914 | // By doing so we will be able to compute the pan distance. |
| 915 | this.firstPinchCenterInFrame_ = e.center; |
| 916 | }, |
| 917 | |
| 918 | pinchZoomEnd: function(e) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 919 | this.mightZoom_(() => { |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 920 | this.pinchPhase_ = Viewport.PinchPhase.PINCH_END; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 921 | const scaleDelta = e.startScaleRatio / this.prevScale_; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 922 | this.pinchCenter_ = e.center; |
| 923 | |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 924 | this.setPinchZoomInternal_(scaleDelta, frameToPluginCoordinate(e.center)); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 925 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 926 | }); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 927 | |
| 928 | this.pinchPhase_ = Viewport.PinchPhase.PINCH_NONE; |
| 929 | this.pinchPanVector_ = null; |
| 930 | this.pinchCenter_ = null; |
| 931 | this.firstPinchCenterInFrame_ = null; |
| 932 | }, |
| 933 | |
| 934 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 935 | * Go to the given page index. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 936 | * |
[email protected] | f90b9a4 | 2014-08-20 05:37:34 | [diff] [blame] | 937 | * @param {number} page the index of the page to go to. zero-based. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 938 | */ |
| 939 | goToPage: function(page) { |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 940 | this.goToPageAndXY(page, 0, 0); |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 941 | }, |
| 942 | |
| 943 | /** |
| 944 | * Go to the given y position in the given page index. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 945 | * |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 946 | * @param {number} page the index of the page to go to. zero-based. |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 947 | * @param {number} x the x position in the page to go to. |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 948 | * @param {number} y the y position in the page to go to. |
| 949 | */ |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 950 | goToPageAndXY: function(page, x, y) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 951 | this.mightZoom_(() => { |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 952 | if (this.pageDimensions_.length === 0) |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 953 | return; |
| 954 | if (page < 0) |
| 955 | page = 0; |
| 956 | if (page >= this.pageDimensions_.length) |
| 957 | page = this.pageDimensions_.length - 1; |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 958 | const dimensions = this.pageDimensions_[page]; |
| 959 | let toolbarOffset = 0; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 960 | // Unless we're in fit to page or fit to height mode, scroll above the |
| 961 | // page by |this.topToolbarHeight_| so that the toolbar isn't covering it |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 962 | // initially. |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 963 | if (!this.isPagedMode()) |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 964 | toolbarOffset = this.topToolbarHeight_; |
| 965 | this.position = { |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 966 | x: (dimensions.x + x) * this.zoom, |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 967 | y: (dimensions.y + y) * this.zoom - toolbarOffset |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 968 | }; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 969 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 970 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 971 | }, |
| 972 | |
| 973 | /** |
| 974 | * Set the dimensions of the document. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 975 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 976 | * @param {Object} documentDimensions the dimensions of the document |
| 977 | */ |
| 978 | setDocumentDimensions: function(documentDimensions) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 979 | this.mightZoom_(() => { |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 980 | const initialDimensions = !this.documentDimensions_; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 981 | this.documentDimensions_ = documentDimensions; |
| 982 | this.pageDimensions_ = this.documentDimensions_.pageDimensions; |
| 983 | if (initialDimensions) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 984 | this.setZoomInternal_(Math.min( |
| 985 | this.defaultZoom_, |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 986 | this.computeFittingZoom_(this.documentDimensions_, true, false))); |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 987 | this.position = {x: 0, y: -this.topToolbarHeight_}; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 988 | } |
| 989 | this.contentSizeChanged_(); |
| 990 | this.resize_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 991 | }); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 992 | }, |
| 993 | |
| 994 | /** |
Douglas Stockwell | 1752f776 | 2018-11-28 23:41:36 | [diff] [blame] | 995 | * @param {number} page |
| 996 | * @return {Rect} The bounds for page `page` minus the shadows. |
| 997 | */ |
| 998 | getPageInsetDimensions: function(page) { |
| 999 | const pageDimensions = this.pageDimensions_[page]; |
| 1000 | const shadow = Viewport.PAGE_SHADOW; |
| 1001 | return { |
| 1002 | x: pageDimensions.x + shadow.left, |
| 1003 | y: pageDimensions.y + shadow.top, |
| 1004 | width: pageDimensions.width - shadow.left - shadow.right, |
| 1005 | height: pageDimensions.height - shadow.top - shadow.bottom, |
| 1006 | }; |
| 1007 | }, |
| 1008 | |
| 1009 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1010 | * Get the coordinates of the page contents (excluding the page shadow) |
| 1011 | * relative to the screen. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 1012 | * |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1013 | * @param {number} page the index of the page to get the rect for. |
| 1014 | * @return {Object} a rect representing the page in screen coordinates. |
| 1015 | */ |
| 1016 | getPageScreenRect: function(page) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 1017 | if (!this.documentDimensions_) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 1018 | return {x: 0, y: 0, width: 0, height: 0}; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 1019 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1020 | if (page >= this.pageDimensions_.length) |
| 1021 | page = this.pageDimensions_.length - 1; |
| 1022 | |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 1023 | const pageDimensions = this.pageDimensions_[page]; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1024 | |
| 1025 | // Compute the page dimensions minus the shadows. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 1026 | const insetDimensions = this.getPageInsetDimensions(page); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1027 | |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 1028 | // Compute the x-coordinate of the page within the document. |
| 1029 | // TODO(raymes): This should really be set when the PDF plugin passes the |
| 1030 | // page coordinates, but it isn't yet. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 1031 | const x = (this.documentDimensions_.width - pageDimensions.width) / 2 + |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 1032 | Viewport.PAGE_SHADOW.left; |
| 1033 | // Compute the space on the left of the document if the document fits |
| 1034 | // completely in the screen. |
dstockwell | afba4e4 | 2018-12-02 22:58:06 | [diff] [blame] | 1035 | let spaceOnLeft = |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 1036 | (this.size.width - this.documentDimensions_.width * this.zoom) / 2; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 1037 | spaceOnLeft = Math.max(spaceOnLeft, 0); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1038 | |
| 1039 | return { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 1040 | x: x * this.zoom + spaceOnLeft - this.window_.pageXOffset, |
| 1041 | y: insetDimensions.y * this.zoom - this.window_.pageYOffset, |
| 1042 | width: insetDimensions.width * this.zoom, |
| 1043 | height: insetDimensions.height * this.zoom |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 1044 | }; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 1045 | }, |
| 1046 | |
| 1047 | /** |
| 1048 | * Check if the current fitting type is a paged mode. |
| 1049 | * |
| 1050 | * In a paged mode, page up and page down scroll to the top of the |
| 1051 | * previous/next page and part of the page is under the toolbar. |
| 1052 | * |
| 1053 | * @return {boolean} Whether the current fitting type is a paged mode. |
| 1054 | */ |
| 1055 | isPagedMode: function(page) { |
| 1056 | return ( |
| 1057 | this.fittingType_ == FittingType.FIT_TO_PAGE || |
| 1058 | this.fittingType_ == FittingType.FIT_TO_HEIGHT); |
Henrique Nakashima | 585c7af0 | 2018-03-27 04:55:21 | [diff] [blame] | 1059 | }, |
| 1060 | |
| 1061 | /** |
| 1062 | * Scroll the viewport to the specified position. |
| 1063 | * |
| 1064 | * @param {!PartialPoint} point The position to which to move the viewport. |
| 1065 | */ |
| 1066 | scrollTo: function(point) { |
| 1067 | let changed = false; |
| 1068 | const newPosition = this.position; |
| 1069 | if (point.x !== undefined && point.x != newPosition.x) { |
| 1070 | newPosition.x = point.x; |
| 1071 | changed = true; |
| 1072 | } |
| 1073 | if (point.y !== undefined && point.y != newPosition.y) { |
| 1074 | newPosition.y = point.y; |
| 1075 | changed = true; |
| 1076 | } |
| 1077 | |
| 1078 | if (changed) |
| 1079 | this.position = newPosition; |
| 1080 | }, |
| 1081 | |
| 1082 | /** |
| 1083 | * Scroll the viewport by the specified delta. |
| 1084 | * |
| 1085 | * @param {!Point} delta The delta by which to move the viewport. |
| 1086 | */ |
| 1087 | scrollBy: function(delta) { |
| 1088 | const newPosition = this.position; |
| 1089 | newPosition.x += delta.x; |
| 1090 | newPosition.y += delta.y; |
| 1091 | this.scrollTo(newPosition); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 1092 | } |
| 1093 | }; |