Chromium Code Reviews| 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 height of the intersection of two rectangles. | 6 * Returns the height 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 height of the intersection of the rects | 9 * @return {number} the height of the intersection of the rects |
| 10 */ | 10 */ |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 this.viewportChangedCallback_ = viewportChangedCallback; | 38 this.viewportChangedCallback_ = viewportChangedCallback; |
| 39 this.beforeZoomCallback_ = beforeZoomCallback; | 39 this.beforeZoomCallback_ = beforeZoomCallback; |
| 40 this.afterZoomCallback_ = afterZoomCallback; | 40 this.afterZoomCallback_ = afterZoomCallback; |
| 41 this.allowedToChangeZoom_ = false; | 41 this.allowedToChangeZoom_ = false; |
| 42 this.zoom_ = 1; | 42 this.zoom_ = 1; |
| 43 this.documentDimensions_ = null; | 43 this.documentDimensions_ = null; |
| 44 this.pageDimensions_ = []; | 44 this.pageDimensions_ = []; |
| 45 this.scrollbarWidth_ = scrollbarWidth; | 45 this.scrollbarWidth_ = scrollbarWidth; |
| 46 this.fittingType_ = Viewport.FittingType.NONE; | 46 this.fittingType_ = Viewport.FittingType.NONE; |
| 47 this.defaultZoom_ = defaultZoom; | 47 this.defaultZoom_ = defaultZoom; |
| 48 this.topToolbarHeight = 0; | |
|
Sam McNally
2015/08/03 08:06:45
Please make this a constructor param instead of a
raymes
2015/08/04 00:28:46
Done.
| |
| 48 | 49 |
| 49 window.addEventListener('scroll', this.updateViewport_.bind(this)); | 50 window.addEventListener('scroll', this.updateViewport_.bind(this)); |
| 50 window.addEventListener('resize', this.resize_.bind(this)); | 51 window.addEventListener('resize', this.resize_.bind(this)); |
| 51 } | 52 } |
| 52 | 53 |
| 53 /** | 54 /** |
| 54 * Enumeration of page fitting types. | 55 * Enumeration of page fitting types. |
| 55 * @enum {string} | 56 * @enum {string} |
| 56 */ | 57 */ |
| 57 Viewport.FittingType = { | 58 Viewport.FittingType = { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 }, | 133 }, |
| 133 | 134 |
| 134 /** | 135 /** |
| 135 * @private | 136 * @private |
| 136 * Helper function called when the zoomed document size changes. | 137 * Helper function called when the zoomed document size changes. |
| 137 */ | 138 */ |
| 138 contentSizeChanged_: function() { | 139 contentSizeChanged_: function() { |
| 139 if (this.documentDimensions_) { | 140 if (this.documentDimensions_) { |
| 140 this.sizer_.style.width = | 141 this.sizer_.style.width = |
| 141 this.documentDimensions_.width * this.zoom_ + 'px'; | 142 this.documentDimensions_.width * this.zoom_ + 'px'; |
| 142 this.sizer_.style.height = | 143 this.sizer_.style.height = this.documentDimensions_.height * this.zoom_ + |
| 143 this.documentDimensions_.height * this.zoom_ + 'px'; | 144 this.topToolbarHeight + 'px'; |
| 144 } | 145 } |
| 145 }, | 146 }, |
| 146 | 147 |
| 147 /** | 148 /** |
| 148 * @private | 149 * @private |
| 149 * Called when the viewport should be updated. | 150 * Called when the viewport should be updated. |
| 150 */ | 151 */ |
| 151 updateViewport_: function() { | 152 updateViewport_: function() { |
| 152 this.viewportChangedCallback_(); | 153 this.viewportChangedCallback_(); |
| 153 }, | 154 }, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 164 else | 165 else |
| 165 this.updateViewport_(); | 166 this.updateViewport_(); |
| 166 }, | 167 }, |
| 167 | 168 |
| 168 /** | 169 /** |
| 169 * @type {Object} the scroll position of the viewport. | 170 * @type {Object} the scroll position of the viewport. |
| 170 */ | 171 */ |
| 171 get position() { | 172 get position() { |
| 172 return { | 173 return { |
| 173 x: this.window_.pageXOffset, | 174 x: this.window_.pageXOffset, |
| 174 y: this.window_.pageYOffset | 175 y: this.window_.pageYOffset - this.topToolbarHeight |
| 175 }; | 176 }; |
| 176 }, | 177 }, |
| 177 | 178 |
| 178 /** | 179 /** |
| 179 * Scroll the viewport to the specified position. | 180 * Scroll the viewport to the specified position. |
| 180 * @type {Object} position the position to scroll to. | 181 * @type {Object} position the position to scroll to. |
| 181 */ | 182 */ |
| 182 set position(position) { | 183 set position(position) { |
| 183 this.window_.scrollTo(position.x, position.y); | 184 this.window_.scrollTo(position.x, position.y + this.topToolbarHeight); |
| 184 }, | 185 }, |
| 185 | 186 |
| 186 /** | 187 /** |
| 187 * @type {Object} the size of the viewport excluding scrollbars. | 188 * @type {Object} the size of the viewport excluding scrollbars. |
| 188 */ | 189 */ |
| 189 get size() { | 190 get size() { |
| 190 var needsScrollbars = this.documentNeedsScrollbars_(this.zoom_); | 191 var needsScrollbars = this.documentNeedsScrollbars_(this.zoom_); |
| 191 var scrollbarWidth = needsScrollbars.vertical ? this.scrollbarWidth_ : 0; | 192 var scrollbarWidth = needsScrollbars.vertical ? this.scrollbarWidth_ : 0; |
| 192 var scrollbarHeight = needsScrollbars.horizontal ? this.scrollbarWidth_ : 0; | 193 var scrollbarHeight = needsScrollbars.horizontal ? this.scrollbarWidth_ : 0; |
| 193 return { | 194 return { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 222 * @private | 223 * @private |
| 223 * Sets the zoom of the viewport. | 224 * Sets the zoom of the viewport. |
| 224 * @param {number} newZoom the zoom level to zoom to. | 225 * @param {number} newZoom the zoom level to zoom to. |
| 225 */ | 226 */ |
| 226 setZoomInternal_: function(newZoom) { | 227 setZoomInternal_: function(newZoom) { |
| 227 if (!this.allowedToChangeZoom_) { | 228 if (!this.allowedToChangeZoom_) { |
| 228 throw 'Called Viewport.setZoomInternal_ without calling ' + | 229 throw 'Called Viewport.setZoomInternal_ without calling ' + |
| 229 'Viewport.mightZoom_.'; | 230 'Viewport.mightZoom_.'; |
| 230 } | 231 } |
| 231 // Record the scroll position (relative to the top-left of the window). | 232 // Record the scroll position (relative to the top-left of the window). |
| 232 var currentScrollPos = [ | 233 var currentScrollPos = { |
| 233 this.window_.pageXOffset / this.zoom_, | 234 x: this.position.x / this.zoom_, |
| 234 this.window_.pageYOffset / this.zoom_ | 235 y: this.position.y / this.zoom_ |
| 235 ]; | 236 }; |
| 236 this.zoom_ = newZoom; | 237 this.zoom_ = newZoom; |
| 237 this.contentSizeChanged_(); | 238 this.contentSizeChanged_(); |
| 238 // Scroll to the scaled scroll position. | 239 // Scroll to the scaled scroll position. |
| 239 this.window_.scrollTo(currentScrollPos[0] * newZoom, | 240 this.position = { |
| 240 currentScrollPos[1] * newZoom); | 241 x: currentScrollPos.x * newZoom, |
| 242 y: currentScrollPos.y * newZoom | |
| 243 }; | |
| 241 }, | 244 }, |
| 242 | 245 |
| 243 /** | 246 /** |
| 244 * Sets the zoom to the given zoom level. | 247 * Sets the zoom to the given zoom level. |
| 245 * @param {number} newZoom the zoom level to zoom to. | 248 * @param {number} newZoom the zoom level to zoom to. |
| 246 */ | 249 */ |
| 247 setZoom: function(newZoom) { | 250 setZoom: function(newZoom) { |
| 248 this.fittingType_ = Viewport.FittingType.NONE; | 251 this.fittingType_ = Viewport.FittingType.NONE; |
| 249 newZoom = Math.max(Viewport.ZOOM_FACTOR_RANGE.min, | 252 newZoom = Math.max(Viewport.ZOOM_FACTOR_RANGE.min, |
| 250 Math.min(newZoom, Viewport.ZOOM_FACTOR_RANGE.max)); | 253 Math.min(newZoom, Viewport.ZOOM_FACTOR_RANGE.max)); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 419 this.fittingType_ = Viewport.FittingType.FIT_TO_PAGE; | 422 this.fittingType_ = Viewport.FittingType.FIT_TO_PAGE; |
| 420 if (!this.documentDimensions_) | 423 if (!this.documentDimensions_) |
| 421 return; | 424 return; |
| 422 var page = this.getMostVisiblePage(); | 425 var page = this.getMostVisiblePage(); |
| 423 // Fit to the current page's height and the widest page's width. | 426 // Fit to the current page's height and the widest page's width. |
| 424 var dimensions = { | 427 var dimensions = { |
| 425 width: this.documentDimensions_.width, | 428 width: this.documentDimensions_.width, |
| 426 height: this.pageDimensions_[page].height, | 429 height: this.pageDimensions_[page].height, |
| 427 }; | 430 }; |
| 428 this.setZoomInternal_(this.computeFittingZoom_(dimensions, false)); | 431 this.setZoomInternal_(this.computeFittingZoom_(dimensions, false)); |
| 429 if (scrollToTopOfPage) | 432 if (scrollToTopOfPage) { |
| 430 this.window_.scrollTo(0, this.pageDimensions_[page].y * this.zoom_); | 433 this.position = { |
| 434 x: 0, | |
| 435 y: this.pageDimensions_[page].y * this.zoom_ | |
| 436 }; | |
| 437 } | |
| 431 this.updateViewport_(); | 438 this.updateViewport_(); |
| 432 }.bind(this)); | 439 }.bind(this)); |
| 433 }, | 440 }, |
| 434 | 441 |
| 435 /** | 442 /** |
| 436 * Zoom the viewport so that a page consumes the entire viewport. Also scrolls | 443 * Zoom the viewport so that a page consumes the entire viewport. Also scrolls |
| 437 * the viewport to the top of the current page. | 444 * the viewport to the top of the current page. |
| 438 */ | 445 */ |
| 439 fitToPage: function() { | 446 fitToPage: function() { |
| 440 this.fitToPageInternal_(true); | 447 this.fitToPageInternal_(true); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 */ | 485 */ |
| 479 goToPage: function(page) { | 486 goToPage: function(page) { |
| 480 this.mightZoom_(function() { | 487 this.mightZoom_(function() { |
| 481 if (this.pageDimensions_.length === 0) | 488 if (this.pageDimensions_.length === 0) |
| 482 return; | 489 return; |
| 483 if (page < 0) | 490 if (page < 0) |
| 484 page = 0; | 491 page = 0; |
| 485 if (page >= this.pageDimensions_.length) | 492 if (page >= this.pageDimensions_.length) |
| 486 page = this.pageDimensions_.length - 1; | 493 page = this.pageDimensions_.length - 1; |
| 487 var dimensions = this.pageDimensions_[page]; | 494 var dimensions = this.pageDimensions_[page]; |
| 488 this.window_.scrollTo(dimensions.x * this.zoom_, | 495 var toolbarOffset = 0; |
| 489 dimensions.y * this.zoom_); | 496 // Unless we're in fit to page mode, scroll a bit above the page so that |
|
Sam McNally
2015/08/03 08:06:45
Can you be a bit more precise than "a bit above"?
raymes
2015/08/04 00:28:46
Done.
| |
| 497 // the toolbar isn't covering it initially. | |
| 498 if (this.fittingType_ != Viewport.FittingType.FIT_TO_PAGE) | |
| 499 toolbarOffset = this.topToolbarHeight; | |
| 500 this.position = { | |
| 501 x: dimensions.x * this.zoom_, | |
| 502 y: dimensions.y * this.zoom_ - toolbarOffset | |
| 503 }; | |
| 490 this.updateViewport_(); | 504 this.updateViewport_(); |
| 491 }.bind(this)); | 505 }.bind(this)); |
| 492 }, | 506 }, |
| 493 | 507 |
| 494 /** | 508 /** |
| 495 * Set the dimensions of the document. | 509 * Set the dimensions of the document. |
| 496 * @param {Object} documentDimensions the dimensions of the document | 510 * @param {Object} documentDimensions the dimensions of the document |
| 497 */ | 511 */ |
| 498 setDocumentDimensions: function(documentDimensions) { | 512 setDocumentDimensions: function(documentDimensions) { |
| 499 this.mightZoom_(function() { | 513 this.mightZoom_(function() { |
| 500 var initialDimensions = !this.documentDimensions_; | 514 var initialDimensions = !this.documentDimensions_; |
| 501 this.documentDimensions_ = documentDimensions; | 515 this.documentDimensions_ = documentDimensions; |
| 502 this.pageDimensions_ = this.documentDimensions_.pageDimensions; | 516 this.pageDimensions_ = this.documentDimensions_.pageDimensions; |
| 503 if (initialDimensions) { | 517 if (initialDimensions) { |
| 504 this.setZoomInternal_( | 518 this.setZoomInternal_( |
| 505 Math.min(this.defaultZoom_, | 519 Math.min(this.defaultZoom_, |
| 506 this.computeFittingZoom_(this.documentDimensions_, true))); | 520 this.computeFittingZoom_(this.documentDimensions_, true))); |
| 507 this.window_.scrollTo(0, 0); | 521 this.position = { |
| 522 x: 0, | |
| 523 y: -this.topToolbarHeight | |
| 524 }; | |
| 508 } | 525 } |
| 509 this.contentSizeChanged_(); | 526 this.contentSizeChanged_(); |
| 510 this.resize_(); | 527 this.resize_(); |
| 511 }.bind(this)); | 528 }.bind(this)); |
| 512 }, | 529 }, |
| 513 | 530 |
| 514 /** | 531 /** |
| 515 * Get the coordinates of the page contents (excluding the page shadow) | 532 * Get the coordinates of the page contents (excluding the page shadow) |
| 516 * relative to the screen. | 533 * relative to the screen. |
| 517 * @param {number} page the index of the page to get the rect for. | 534 * @param {number} page the index of the page to get the rect for. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 553 spaceOnLeft = Math.max(spaceOnLeft, 0); | 570 spaceOnLeft = Math.max(spaceOnLeft, 0); |
| 554 | 571 |
| 555 return { | 572 return { |
| 556 x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset, | 573 x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset, |
| 557 y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset, | 574 y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset, |
| 558 width: insetDimensions.width * this.zoom_, | 575 width: insetDimensions.width * this.zoom_, |
| 559 height: insetDimensions.height * this.zoom_ | 576 height: insetDimensions.height * this.zoom_ |
| 560 }; | 577 }; |
| 561 } | 578 } |
| 562 }; | 579 }; |
| OLD | NEW |