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