Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 this._provider = provider; | 52 this._provider = provider; |
| 53 this.element.addEventListener("scroll", this._onScroll.bind(this), false); | 53 this.element.addEventListener("scroll", this._onScroll.bind(this), false); |
| 54 this.element.addEventListener("copy", this._onCopy.bind(this), false); | 54 this.element.addEventListener("copy", this._onCopy.bind(this), false); |
| 55 this.element.addEventListener("dragstart", this._onDragStart.bind(this), fal se); | 55 this.element.addEventListener("dragstart", this._onDragStart.bind(this), fal se); |
| 56 | 56 |
| 57 this._firstVisibleIndex = 0; | 57 this._firstVisibleIndex = 0; |
| 58 this._lastVisibleIndex = -1; | 58 this._lastVisibleIndex = -1; |
| 59 this._renderedItems = []; | 59 this._renderedItems = []; |
| 60 this._anchorSelection = null; | 60 this._anchorSelection = null; |
| 61 this._headSelection = null; | 61 this._headSelection = null; |
| 62 this._stickToBottom = false; | |
| 63 this._scrolledToBottom = true; | |
| 64 this._itemCount = 0; | 62 this._itemCount = 0; |
| 63 | |
| 64 // Listen for any changes to descendants and trigger a refresh. This ensures | |
| 65 // that items updated asynchronously will not break stick-to-bottom behavior | |
| 66 // if they change the scroll height. | |
| 67 this._observer = new MutationObserver(this.refresh.bind(this)); | |
|
dgozman
2016/08/03 21:19:01
Did you check the performance of this? E.g. do con
luoe
2016/08/04 23:13:01
Scrolling with a console.table() was slow! Not be
| |
| 68 this._observerConfig = { childList: true, subtree: true }; | |
| 65 } | 69 } |
| 66 | 70 |
| 67 /** | 71 /** |
| 68 * @interface | 72 * @interface |
| 69 */ | 73 */ |
| 70 WebInspector.ViewportControl.Provider = function() | 74 WebInspector.ViewportControl.Provider = function() |
| 71 { | 75 { |
| 72 } | 76 } |
| 73 | 77 |
| 74 WebInspector.ViewportControl.Provider.prototype = { | 78 WebInspector.ViewportControl.Provider.prototype = { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 element: function() | 142 element: function() |
| 139 { | 143 { |
| 140 return this._element; | 144 return this._element; |
| 141 }, | 145 }, |
| 142 } | 146 } |
| 143 | 147 |
| 144 WebInspector.ViewportControl.prototype = { | 148 WebInspector.ViewportControl.prototype = { |
| 145 /** | 149 /** |
| 146 * @return {boolean} | 150 * @return {boolean} |
| 147 */ | 151 */ |
| 148 scrolledToBottom: function() | 152 stickToBottom: function() |
| 149 { | 153 { |
| 150 return this._scrolledToBottom; | 154 return this._stickToBottom; |
| 151 }, | 155 }, |
| 152 | 156 |
| 153 /** | 157 /** |
| 154 * @param {boolean} value | 158 * @param {boolean} value |
| 155 */ | 159 */ |
| 156 setStickToBottom: function(value) | 160 setStickToBottom: function(value) |
| 157 { | 161 { |
| 158 this._stickToBottom = value; | 162 this._stickToBottom = value; |
| 163 if (this._stickToBottom) | |
| 164 this._observer.observe(this._contentElement, this._observerConfig); | |
| 165 else | |
| 166 this._observer.disconnect(); | |
| 159 }, | 167 }, |
| 160 | 168 |
| 161 /** | 169 /** |
| 162 * @param {!Event} event | 170 * @param {!Event} event |
| 163 */ | 171 */ |
| 164 _onCopy: function(event) | 172 _onCopy: function(event) |
| 165 { | 173 { |
| 166 var text = this._selectedText(); | 174 var text = this._selectedText(); |
| 167 if (!text) | 175 if (!text) |
| 168 return; | 176 return; |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 369 else if (this._headSelection.item > this._lastVisibleIndex) | 377 else if (this._headSelection.item > this._lastVisibleIndex) |
| 370 headElement = this._bottomGapElement; | 378 headElement = this._bottomGapElement; |
| 371 headOffset = this._selectionIsBackward ? 0 : 1; | 379 headOffset = this._selectionIsBackward ? 0 : 1; |
| 372 } | 380 } |
| 373 | 381 |
| 374 selection.setBaseAndExtent(anchorElement, anchorOffset, headElement, hea dOffset); | 382 selection.setBaseAndExtent(anchorElement, anchorOffset, headElement, hea dOffset); |
| 375 }, | 383 }, |
| 376 | 384 |
| 377 refresh: function() | 385 refresh: function() |
| 378 { | 386 { |
| 387 this._observer.disconnect(); | |
| 388 this._innerRefresh(); | |
| 389 if (this._stickToBottom) | |
| 390 this._observer.observe(this._contentElement, this._observerConfig); | |
| 391 }, | |
| 392 | |
| 393 _innerRefresh: function() | |
| 394 { | |
| 379 if (!this._visibleHeight()) | 395 if (!this._visibleHeight()) |
| 380 return; // Do nothing for invisible controls. | 396 return; // Do nothing for invisible controls. |
| 381 | 397 |
| 382 if (!this._itemCount) { | 398 if (!this._itemCount) { |
| 383 for (var i = 0; i < this._renderedItems.length; ++i) | 399 for (var i = 0; i < this._renderedItems.length; ++i) |
| 384 this._renderedItems[i].willHide(); | 400 this._renderedItems[i].willHide(); |
| 385 this._renderedItems = []; | 401 this._renderedItems = []; |
| 386 this._contentElement.removeChildren(); | 402 this._contentElement.removeChildren(); |
| 387 this._topGapElement.style.height = "0px"; | 403 this._topGapElement.style.height = "0px"; |
| 388 this._bottomGapElement.style.height = "0px"; | 404 this._bottomGapElement.style.height = "0px"; |
| 389 this._firstVisibleIndex = -1; | 405 this._firstVisibleIndex = -1; |
| 390 this._lastVisibleIndex = -1; | 406 this._lastVisibleIndex = -1; |
| 391 return; | 407 return; |
| 392 } | 408 } |
| 393 | 409 |
| 394 var selection = this.element.getComponentSelection(); | 410 var selection = this.element.getComponentSelection(); |
| 395 var shouldRestoreSelection = this._updateSelectionModel(selection); | 411 var shouldRestoreSelection = this._updateSelectionModel(selection); |
| 396 | 412 |
| 397 var visibleFrom = this.element.scrollTop; | 413 var visibleFrom = this.element.scrollTop; |
| 398 var visibleHeight = this._visibleHeight(); | 414 var visibleHeight = this._visibleHeight(); |
| 399 this._scrolledToBottom = this.element.isScrolledToBottom(); | |
| 400 var isInvalidating = !this._cumulativeHeights; | 415 var isInvalidating = !this._cumulativeHeights; |
| 401 | 416 |
| 402 for (var i = 0; i < this._renderedItems.length; ++i) { | 417 for (var i = 0; i < this._renderedItems.length; ++i) { |
| 403 // Tolerate 1-pixel error due to double-to-integer rounding errors. | 418 // Tolerate 1-pixel error due to double-to-integer rounding errors. |
| 404 if (this._cumulativeHeights && Math.abs(this._cachedItemHeight(this. _firstVisibleIndex + i) - this._renderedItems[i].element().offsetHeight) > 1) | 419 if (this._cumulativeHeights && Math.abs(this._cachedItemHeight(this. _firstVisibleIndex + i) - this._renderedItems[i].element().offsetHeight) > 1) |
| 405 delete this._cumulativeHeights; | 420 delete this._cumulativeHeights; |
| 406 } | 421 } |
| 407 this._rebuildCumulativeHeightsIfNeeded(); | 422 this._rebuildCumulativeHeightsIfNeeded(); |
| 408 var oldFirstVisibleIndex = this._firstVisibleIndex; | 423 var oldFirstVisibleIndex = this._firstVisibleIndex; |
| 409 var oldLastVisibleIndex = this._lastVisibleIndex; | 424 var oldLastVisibleIndex = this._lastVisibleIndex; |
| 410 | 425 |
| 411 var shouldStickToBottom = this._stickToBottom && this._scrolledToBottom; | 426 // When the viewport is scrolled to the bottom, using the cumulative hei ghts estimate is not |
| 412 | 427 // precise enough to determine next visible indices. This stickToBottom check avoids extra |
| 413 if (shouldStickToBottom) { | 428 // calls to refresh in those cases. |
| 429 if (this._stickToBottom) { | |
| 430 this._firstVisibleIndex = Math.max(this._itemCount - Math.ceil(visib leHeight / this._provider.minimumRowHeight()), 0); | |
| 414 this._lastVisibleIndex = this._itemCount - 1; | 431 this._lastVisibleIndex = this._itemCount - 1; |
| 415 this._firstVisibleIndex = Math.max(this._itemCount - Math.ceil(visib leHeight / this._provider.minimumRowHeight()), 0); | |
| 416 } else { | 432 } else { |
| 417 this._firstVisibleIndex = Math.max(Array.prototype.lowerBound.call(t his._cumulativeHeights, visibleFrom + 1), 0); | 433 this._firstVisibleIndex = Math.max(Array.prototype.lowerBound.call(t his._cumulativeHeights, visibleFrom + 1), 0); |
| 418 // Proactively render more rows in case some of them will be collaps ed without triggering refresh. @see crbug.com/390169 | 434 // Proactively render more rows in case some of them will be collaps ed without triggering refresh. @see crbug.com/390169 |
| 419 this._lastVisibleIndex = this._firstVisibleIndex + Math.ceil(visible Height / this._provider.minimumRowHeight()) - 1; | 435 this._lastVisibleIndex = this._firstVisibleIndex + Math.ceil(visible Height / this._provider.minimumRowHeight()) - 1; |
| 420 this._lastVisibleIndex = Math.min(this._lastVisibleIndex, this._item Count - 1); | 436 this._lastVisibleIndex = Math.min(this._lastVisibleIndex, this._item Count - 1); |
| 421 } | 437 } |
| 438 | |
| 422 var topGapHeight = this._cumulativeHeights[this._firstVisibleIndex - 1] || 0; | 439 var topGapHeight = this._cumulativeHeights[this._firstVisibleIndex - 1] || 0; |
| 423 var bottomGapHeight = this._cumulativeHeights[this._cumulativeHeights.le ngth - 1] - this._cumulativeHeights[this._lastVisibleIndex]; | 440 var bottomGapHeight = this._cumulativeHeights[this._cumulativeHeights.le ngth - 1] - this._cumulativeHeights[this._lastVisibleIndex]; |
| 424 | 441 |
| 425 /** | 442 /** |
| 426 * @this {WebInspector.ViewportControl} | 443 * @this {WebInspector.ViewportControl} |
| 427 */ | 444 */ |
| 428 function prepare() | 445 function prepare() |
| 429 { | 446 { |
| 430 this._topGapElement.style.height = topGapHeight + "px"; | 447 this._topGapElement.style.height = topGapHeight + "px"; |
| 431 this._bottomGapElement.style.height = bottomGapHeight + "px"; | 448 this._bottomGapElement.style.height = bottomGapHeight + "px"; |
| 432 this._topGapElement._active = !!topGapHeight; | 449 this._topGapElement._active = !!topGapHeight; |
| 433 this._bottomGapElement._active = !!bottomGapHeight; | 450 this._bottomGapElement._active = !!bottomGapHeight; |
| 434 this._contentElement.style.setProperty("height", "10000000px"); | 451 this._contentElement.style.setProperty("height", "10000000px"); |
| 435 } | 452 } |
| 436 | 453 |
| 437 if (isInvalidating) | 454 if (isInvalidating) |
| 438 this._fullViewportUpdate(prepare.bind(this)); | 455 this._fullViewportUpdate(prepare.bind(this)); |
| 439 else | 456 else |
| 440 this._partialViewportUpdate(oldFirstVisibleIndex, oldLastVisibleInde x, prepare.bind(this)); | 457 this._partialViewportUpdate(oldFirstVisibleIndex, oldLastVisibleInde x, prepare.bind(this)); |
| 441 this._contentElement.style.removeProperty("height"); | 458 this._contentElement.style.removeProperty("height"); |
| 442 // Should be the last call in the method as it might force layout. | 459 // Should be the last call in the method as it might force layout. |
| 443 if (shouldRestoreSelection) | 460 if (shouldRestoreSelection) |
| 444 this._restoreSelection(selection); | 461 this._restoreSelection(selection); |
| 445 if (shouldStickToBottom) | 462 if (this._stickToBottom) |
| 446 this.element.scrollTop = 10000000; | 463 this.element.scrollTop = 10000000; |
| 447 }, | 464 }, |
| 448 | 465 |
| 449 /** | 466 /** |
| 450 * @param {function()} prepare | 467 * @param {function()} prepare |
| 451 */ | 468 */ |
| 452 _fullViewportUpdate: function(prepare) | 469 _fullViewportUpdate: function(prepare) |
| 453 { | 470 { |
| 454 for (var i = 0; i < this._renderedItems.length; ++i) | 471 for (var i = 0; i < this._renderedItems.length; ++i) |
| 455 this._renderedItems[i].willHide(); | 472 this._renderedItems[i].willHide(); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 605 this.forceScrollItemToBeFirst(index); | 622 this.forceScrollItemToBeFirst(index); |
| 606 else if (index >= this._lastVisibleIndex) | 623 else if (index >= this._lastVisibleIndex) |
| 607 this.forceScrollItemToBeLast(index); | 624 this.forceScrollItemToBeLast(index); |
| 608 }, | 625 }, |
| 609 | 626 |
| 610 /** | 627 /** |
| 611 * @param {number} index | 628 * @param {number} index |
| 612 */ | 629 */ |
| 613 forceScrollItemToBeFirst: function(index) | 630 forceScrollItemToBeFirst: function(index) |
| 614 { | 631 { |
| 632 this.setStickToBottom(false); | |
| 615 this._rebuildCumulativeHeightsIfNeeded(); | 633 this._rebuildCumulativeHeightsIfNeeded(); |
| 616 this.element.scrollTop = index > 0 ? this._cumulativeHeights[index - 1] : 0; | 634 this.element.scrollTop = index > 0 ? this._cumulativeHeights[index - 1] : 0; |
| 635 if (this.element.isScrolledToBottom()) | |
| 636 this.setStickToBottom(true); | |
| 617 this.refresh(); | 637 this.refresh(); |
| 618 }, | 638 }, |
| 619 | 639 |
| 620 /** | 640 /** |
| 621 * @param {number} index | 641 * @param {number} index |
| 622 */ | 642 */ |
| 623 forceScrollItemToBeLast: function(index) | 643 forceScrollItemToBeLast: function(index) |
| 624 { | 644 { |
| 645 this.setStickToBottom(false); | |
| 625 this._rebuildCumulativeHeightsIfNeeded(); | 646 this._rebuildCumulativeHeightsIfNeeded(); |
| 626 this.element.scrollTop = this._cumulativeHeights[index] - this._visibleH eight(); | 647 this.element.scrollTop = this._cumulativeHeights[index] - this._visibleH eight(); |
| 648 if (this.element.isScrolledToBottom()) | |
| 649 this.setStickToBottom(true); | |
| 627 this.refresh(); | 650 this.refresh(); |
| 628 }, | 651 }, |
| 629 | 652 |
| 630 /** | 653 /** |
| 631 * @return {number} | 654 * @return {number} |
| 632 */ | 655 */ |
| 633 _visibleHeight: function() | 656 _visibleHeight: function() |
| 634 { | 657 { |
| 635 // Use offsetHeight instead of clientHeight to avoid being affected by h orizontal scroll. | 658 // Use offsetHeight instead of clientHeight to avoid being affected by h orizontal scroll. |
| 636 return this.element.offsetHeight; | 659 return this.element.offsetHeight; |
| 637 } | 660 } |
| 638 } | 661 } |
| OLD | NEW |