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