OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Returns the area of the intersection of two rectangles. | 6 * Returns the area of the intersection of two rectangles. |
7 * @param {Object} rect1 the first rect | 7 * @param {Object} rect1 the first rect |
8 * @param {Object} rect2 the second rect | 8 * @param {Object} rect2 the second rect |
9 * @return {number} the area of the intersection of the rects | 9 * @return {number} the area of the intersection of the rects |
10 */ | 10 */ |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 f(); | 199 f(); |
200 this.allowedToChangeZoom_ = false; | 200 this.allowedToChangeZoom_ = false; |
201 this.afterZoomCallback_(); | 201 this.afterZoomCallback_(); |
202 }, | 202 }, |
203 | 203 |
204 /** | 204 /** |
205 * @private | 205 * @private |
206 * Sets the zoom of the viewport. | 206 * Sets the zoom of the viewport. |
207 * @param {number} newZoom the zoom level to zoom to. | 207 * @param {number} newZoom the zoom level to zoom to. |
208 */ | 208 */ |
209 setZoom_: function(newZoom) { | 209 setZoomInternal_: function(newZoom) { |
210 if (!this.allowedToChangeZoom_) | 210 if (!this.allowedToChangeZoom_) { |
211 throw 'Called Viewport.setZoom_ without calling Viewport.mightZoom_.'; | 211 throw 'Called Viewport.setZoomInternal_ without calling ' + |
| 212 'Viewport.mightZoom_.'; |
| 213 } |
212 var oldZoom = this.zoom_; | 214 var oldZoom = this.zoom_; |
213 this.zoom_ = newZoom; | 215 this.zoom_ = newZoom; |
214 // Record the scroll position (relative to the middle of the window). | 216 // Record the scroll position (relative to the middle of the window). |
215 var currentScrollPos = [ | 217 var currentScrollPos = [ |
216 (this.window_.pageXOffset + this.window_.innerWidth / 2) / oldZoom, | 218 (this.window_.pageXOffset + this.window_.innerWidth / 2) / oldZoom, |
217 (this.window_.pageYOffset + this.window_.innerHeight / 2) / oldZoom | 219 (this.window_.pageYOffset + this.window_.innerHeight / 2) / oldZoom |
218 ]; | 220 ]; |
219 this.contentSizeChanged_(); | 221 this.contentSizeChanged_(); |
220 // Scroll to the scaled scroll position. | 222 // Scroll to the scaled scroll position. |
221 this.window_.scrollTo( | 223 this.window_.scrollTo( |
222 currentScrollPos[0] * newZoom - this.window_.innerWidth / 2, | 224 currentScrollPos[0] * newZoom - this.window_.innerWidth / 2, |
223 currentScrollPos[1] * newZoom - this.window_.innerHeight / 2); | 225 currentScrollPos[1] * newZoom - this.window_.innerHeight / 2); |
224 }, | 226 }, |
225 | 227 |
226 /** | 228 /** |
227 * @private | 229 * Sets the zoom to the given zoom level. |
228 * Sets the zoom for testing purposes. | 230 * @param {number} newZoom the zoom level to zoom to. |
229 */ | 231 */ |
230 setZoomForTest_: function(newZoom) { | 232 setZoom: function(newZoom) { |
231 this.mightZoom_(function() { | 233 this.mightZoom_(function() { |
232 this.setZoom_(newZoom); | 234 this.setZoomInternal_(newZoom); |
| 235 this.updateViewport_(); |
233 }.bind(this)); | 236 }.bind(this)); |
234 }, | 237 }, |
235 | 238 |
236 /** | 239 /** |
237 * @type {number} the width of scrollbars in the viewport in pixels. | 240 * @type {number} the width of scrollbars in the viewport in pixels. |
238 */ | 241 */ |
239 get scrollbarWidth() { | 242 get scrollbarWidth() { |
240 return this.scrollbarWidth_; | 243 return this.scrollbarWidth_; |
241 }, | 244 }, |
242 | 245 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 */ | 377 */ |
375 fitToWidth: function() { | 378 fitToWidth: function() { |
376 this.mightZoom_(function() { | 379 this.mightZoom_(function() { |
377 this.fittingType_ = Viewport.FittingType.FIT_TO_WIDTH; | 380 this.fittingType_ = Viewport.FittingType.FIT_TO_WIDTH; |
378 if (!this.documentDimensions_) | 381 if (!this.documentDimensions_) |
379 return; | 382 return; |
380 // Track the last y-position to stay at the same position after zooming. | 383 // Track the last y-position to stay at the same position after zooming. |
381 var oldY = this.window_.pageYOffset / this.zoom_; | 384 var oldY = this.window_.pageYOffset / this.zoom_; |
382 // When computing fit-to-width, the maximum width of a page in the | 385 // When computing fit-to-width, the maximum width of a page in the |
383 // document is used, which is equal to the size of the document width. | 386 // document is used, which is equal to the size of the document width. |
384 this.setZoom_(this.computeFittingZoom_(this.documentDimensions_, true)); | 387 this.setZoomInternal_(this.computeFittingZoom_(this.documentDimensions_, |
| 388 true)); |
385 var page = this.getMostVisiblePage(); | 389 var page = this.getMostVisiblePage(); |
386 this.window_.scrollTo(0, oldY * this.zoom_); | 390 this.window_.scrollTo(0, oldY * this.zoom_); |
387 this.updateViewport_(); | 391 this.updateViewport_(); |
388 }.bind(this)); | 392 }.bind(this)); |
389 }, | 393 }, |
390 | 394 |
391 /** | 395 /** |
392 * Zoom the viewport so that a page consumes the entire viewport. Also scrolls | 396 * Zoom the viewport so that a page consumes the entire viewport. Also scrolls |
393 * to the top of the most visible page. | 397 * to the top of the most visible page. |
394 */ | 398 */ |
395 fitToPage: function() { | 399 fitToPage: function() { |
396 this.mightZoom_(function() { | 400 this.mightZoom_(function() { |
397 this.fittingType_ = Viewport.FittingType.FIT_TO_PAGE; | 401 this.fittingType_ = Viewport.FittingType.FIT_TO_PAGE; |
398 if (!this.documentDimensions_) | 402 if (!this.documentDimensions_) |
399 return; | 403 return; |
400 var page = this.getMostVisiblePage(); | 404 var page = this.getMostVisiblePage(); |
401 this.setZoom_(this.computeFittingZoom_( | 405 this.setZoomInternal_(this.computeFittingZoom_( |
402 this.pageDimensions_[page], false)); | 406 this.pageDimensions_[page], false)); |
403 // Center the document in the page by scrolling by the amount of empty | 407 // Center the document in the page by scrolling by the amount of empty |
404 // space to the left of the document. | 408 // space to the left of the document. |
405 var xOffset = | 409 var xOffset = |
406 (this.documentDimensions_.width - this.pageDimensions_[page].width) * | 410 (this.documentDimensions_.width - this.pageDimensions_[page].width) * |
407 this.zoom_ / 2; | 411 this.zoom_ / 2; |
408 this.window_.scrollTo(xOffset, | 412 this.window_.scrollTo(xOffset, |
409 this.pageDimensions_[page].y * this.zoom_); | 413 this.pageDimensions_[page].y * this.zoom_); |
410 this.updateViewport_(); | 414 this.updateViewport_(); |
411 }.bind(this)); | 415 }.bind(this)); |
412 }, | 416 }, |
413 | 417 |
414 /** | 418 /** |
415 * Zoom out to the next predefined zoom level. | 419 * Zoom out to the next predefined zoom level. |
416 */ | 420 */ |
417 zoomOut: function() { | 421 zoomOut: function() { |
418 this.mightZoom_(function() { | 422 this.mightZoom_(function() { |
419 this.fittingType_ = Viewport.FittingType.NONE; | 423 this.fittingType_ = Viewport.FittingType.NONE; |
420 var nextZoom = Viewport.ZOOM_FACTORS[0]; | 424 var nextZoom = Viewport.ZOOM_FACTORS[0]; |
421 for (var i = 0; i < Viewport.ZOOM_FACTORS.length; i++) { | 425 for (var i = 0; i < Viewport.ZOOM_FACTORS.length; i++) { |
422 if (Viewport.ZOOM_FACTORS[i] < this.zoom_) | 426 if (Viewport.ZOOM_FACTORS[i] < this.zoom_) |
423 nextZoom = Viewport.ZOOM_FACTORS[i]; | 427 nextZoom = Viewport.ZOOM_FACTORS[i]; |
424 } | 428 } |
425 this.setZoom_(nextZoom); | 429 this.setZoomInternal_(nextZoom); |
426 this.updateViewport_(); | 430 this.updateViewport_(); |
427 }.bind(this)); | 431 }.bind(this)); |
428 }, | 432 }, |
429 | 433 |
430 /** | 434 /** |
431 * Zoom in to the next predefined zoom level. | 435 * Zoom in to the next predefined zoom level. |
432 */ | 436 */ |
433 zoomIn: function() { | 437 zoomIn: function() { |
434 this.mightZoom_(function() { | 438 this.mightZoom_(function() { |
435 this.fittingType_ = Viewport.FittingType.NONE; | 439 this.fittingType_ = Viewport.FittingType.NONE; |
436 var nextZoom = Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1]; | 440 var nextZoom = Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1]; |
437 for (var i = Viewport.ZOOM_FACTORS.length - 1; i >= 0; i--) { | 441 for (var i = Viewport.ZOOM_FACTORS.length - 1; i >= 0; i--) { |
438 if (Viewport.ZOOM_FACTORS[i] > this.zoom_) | 442 if (Viewport.ZOOM_FACTORS[i] > this.zoom_) |
439 nextZoom = Viewport.ZOOM_FACTORS[i]; | 443 nextZoom = Viewport.ZOOM_FACTORS[i]; |
440 } | 444 } |
441 this.setZoom_(nextZoom); | 445 this.setZoomInternal_(nextZoom); |
442 this.updateViewport_(); | 446 this.updateViewport_(); |
443 }.bind(this)); | 447 }.bind(this)); |
444 }, | 448 }, |
445 | 449 |
446 /** | 450 /** |
447 * Go to the given page index. | 451 * Go to the given page index. |
448 * @param {number} page the index of the page to go to. | 452 * @param {number} page the index of the page to go to. |
449 */ | 453 */ |
450 goToPage: function(page) { | 454 goToPage: function(page) { |
451 this.mightZoom_(function() { | 455 this.mightZoom_(function() { |
(...skipping 13 matching lines...) Expand all Loading... |
465 /** | 469 /** |
466 * Set the dimensions of the document. | 470 * Set the dimensions of the document. |
467 * @param {Object} documentDimensions the dimensions of the document | 471 * @param {Object} documentDimensions the dimensions of the document |
468 */ | 472 */ |
469 setDocumentDimensions: function(documentDimensions) { | 473 setDocumentDimensions: function(documentDimensions) { |
470 this.mightZoom_(function() { | 474 this.mightZoom_(function() { |
471 var initialDimensions = !this.documentDimensions_; | 475 var initialDimensions = !this.documentDimensions_; |
472 this.documentDimensions_ = documentDimensions; | 476 this.documentDimensions_ = documentDimensions; |
473 this.pageDimensions_ = this.documentDimensions_.pageDimensions; | 477 this.pageDimensions_ = this.documentDimensions_.pageDimensions; |
474 if (initialDimensions) { | 478 if (initialDimensions) { |
475 this.setZoom_(this.computeFittingZoom_(this.documentDimensions_, true)); | 479 this.setZoomInternal_(this.computeFittingZoom_(this.documentDimensions_, |
| 480 true)); |
476 if (this.zoom_ > 1) | 481 if (this.zoom_ > 1) |
477 this.setZoom_(1); | 482 this.setZoomInternal_(1); |
478 this.window_.scrollTo(0, 0); | 483 this.window_.scrollTo(0, 0); |
479 } | 484 } |
480 this.contentSizeChanged_(); | 485 this.contentSizeChanged_(); |
481 this.resize_(); | 486 this.resize_(); |
482 }.bind(this)); | 487 }.bind(this)); |
483 }, | 488 }, |
484 | 489 |
485 /** | 490 /** |
486 * Get the coordinates of the page contents (excluding the page shadow) | 491 * Get the coordinates of the page contents (excluding the page shadow) |
487 * relative to the screen. | 492 * relative to the screen. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 spaceOnLeft = Math.max(spaceOnLeft, 0); | 529 spaceOnLeft = Math.max(spaceOnLeft, 0); |
525 | 530 |
526 return { | 531 return { |
527 x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset, | 532 x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset, |
528 y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset, | 533 y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset, |
529 width: insetDimensions.width * this.zoom_, | 534 width: insetDimensions.width * this.zoom_, |
530 height: insetDimensions.height * this.zoom_ | 535 height: insetDimensions.height * this.zoom_ |
531 }; | 536 }; |
532 } | 537 } |
533 }; | 538 }; |
OLD | NEW |