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