Index: third_party/WebKit/Source/devtools/front_end/ui/SuggestBox.js |
diff --git a/third_party/WebKit/Source/devtools/front_end/ui/SuggestBox.js b/third_party/WebKit/Source/devtools/front_end/ui/SuggestBox.js |
index 1b20de2e0162c3f1caf51a5295798dbd816e4dad..64ac296b17b78db645648872ad76f439fd493b70 100644 |
--- a/third_party/WebKit/Source/devtools/front_end/ui/SuggestBox.js |
+++ b/third_party/WebKit/Source/devtools/front_end/ui/SuggestBox.js |
@@ -46,7 +46,6 @@ UI.SuggestBoxDelegate.prototype = { |
}; |
/** |
- * @implements {UI.ViewportControl.Provider} |
* @unrestricted |
*/ |
UI.SuggestBox = class { |
@@ -57,13 +56,13 @@ UI.SuggestBox = class { |
*/ |
constructor(suggestBoxDelegate, maxItemsHeight, captureEnter) { |
this._suggestBoxDelegate = suggestBoxDelegate; |
- this._length = 0; |
this._selectedIndex = -1; |
this._selectedElement = null; |
this._maxItemsHeight = maxItemsHeight; |
this._maybeHideBound = this._maybeHide.bind(this); |
this._container = createElementWithClass('div', 'suggest-box-container'); |
- this._viewport = new UI.ViewportControl(this); |
+ /** @type {!UI.ViewportControl<!UI.SuggestBox.Suggestion>} */ |
+ this._viewport = new UI.ViewportControl(this._createItemElement.bind(this)); |
this._element = this._viewport.element; |
this._element.classList.add('suggest-box'); |
this._container.appendChild(this._element); |
@@ -75,14 +74,9 @@ UI.SuggestBox = class { |
this._asyncDetailsPromises = new Map(); |
this._userInteracted = false; |
this._captureEnter = captureEnter; |
- /** @type {!Array<!Element>} */ |
- this._elementList = []; |
- this._rowHeight = 17; |
this._viewportWidth = '100vw'; |
this._hasVerticalScroll = false; |
this._userEnteredText = ''; |
- /** @type {!UI.SuggestBox.Suggestions} */ |
- this._items = []; |
} |
/** |
@@ -96,17 +90,18 @@ UI.SuggestBox = class { |
* @param {!AnchorBox} anchorBox |
*/ |
setPosition(anchorBox) { |
- this._updateBoxPosition(anchorBox); |
+ this._updateBoxPosition(anchorBox, this._viewport.length()); |
} |
/** |
* @param {!AnchorBox} anchorBox |
+ * @param {number} length |
*/ |
- _updateBoxPosition(anchorBox) { |
+ _updateBoxPosition(anchorBox, length) { |
console.assert(this._overlay); |
- if (this._lastAnchorBox && this._lastAnchorBox.equals(anchorBox) && this._lastItemCount === this.itemCount()) |
+ if (this._lastAnchorBox && this._lastAnchorBox.equals(anchorBox) && this._lastItemCount === length) |
return; |
- this._lastItemCount = this.itemCount(); |
+ this._lastItemCount = length; |
this._lastAnchorBox = anchorBox; |
// Position relative to main DevTools element. |
@@ -128,7 +123,7 @@ UI.SuggestBox = class { |
var maxHeight = Math.min( |
Math.max(underHeight, aboveHeight) - spacer, |
this._maxItemsHeight ? this._maxItemsHeight * this._rowHeight : Infinity); |
- var height = this._rowHeight * this._items.length; |
+ var height = this._rowHeight * length; |
this._hasVerticalScroll = height > maxHeight; |
this._element.style.height = Math.min(maxHeight, height) + 'px'; |
} |
@@ -138,14 +133,20 @@ UI.SuggestBox = class { |
this._element.style.width = '100vw'; |
return; |
} |
+ if (!this._viewport.length()) |
+ return; |
// If there are no scrollbars, set the width to the width of the largest row. |
+ var maxItem = this._viewport.itemAtIndex(0); |
var maxIndex = 0; |
- for (var i = 0; i < this._items.length; i++) { |
- if (this._items[i].title.length > this._items[maxIndex].title.length) |
+ for (var i = 1; i < this._viewport.length(); i++) { |
+ var item = this._viewport.itemAtIndex(i); |
+ if (item.title.length > maxItem.title.length) { |
+ maxItem = item; |
maxIndex = i; |
+ } |
} |
- var element = /** @type {!Element} */ (this.itemElement(maxIndex)); |
- this._element.style.width = UI.measurePreferredSize(element, this._element).width + 'px'; |
+ this._element.style.width = |
+ UI.measurePreferredSize(this._viewport.elementAtIndex(maxIndex), this._element).width + 'px'; |
} |
/** |
@@ -175,10 +176,8 @@ UI.SuggestBox = class { |
this._bodyElement.addEventListener('mousedown', this._maybeHideBound, true); |
this._overlay = new UI.SuggestBox.Overlay(); |
this._overlay.setContentElement(this._container); |
- var measuringElement = this._createItemElement('1', '12'); |
- this._viewport.element.appendChild(measuringElement); |
- this._rowHeight = measuringElement.getBoundingClientRect().height; |
- measuringElement.remove(); |
+ this._rowHeight = |
+ UI.measurePreferredSize(this._createItemElement({title: '1', subtitle: '12'}), this._element).height; |
} |
hide() { |
@@ -241,7 +240,8 @@ UI.SuggestBox = class { |
* @return {boolean} is changed |
*/ |
_selectClosest(shift, isCircular) { |
- if (!this._length) |
+ var length = this._viewport.length(); |
+ if (!length) |
return false; |
this._userInteracted = true; |
@@ -252,9 +252,9 @@ UI.SuggestBox = class { |
var index = this._selectedIndex + shift; |
if (isCircular) |
- index = (this._length + index) % this._length; |
+ index = (length + index) % length; |
else |
- index = Number.constrain(index, 0, this._length - 1); |
+ index = Number.constrain(index, 0, length - 1); |
this._selectItem(index); |
return true; |
@@ -270,23 +270,20 @@ UI.SuggestBox = class { |
} |
/** |
- * @param {string} query |
- * @param {string} title |
- * @param {string=} subtitle |
- * @param {string=} iconType |
- * @param {boolean=} isSecondary |
+ * @param {!UI.SuggestBox.Suggestion} item |
* @return {!Element} |
*/ |
- _createItemElement(query, title, subtitle, iconType, isSecondary) { |
+ _createItemElement(item) { |
+ var query = this._userEnteredText; |
var element = createElementWithClass('div', 'suggest-box-content-item source-code'); |
- if (iconType) { |
- var icon = UI.Icon.create(iconType, 'suggestion-icon'); |
+ if (item.iconType) { |
+ var icon = UI.Icon.create(item.iconType, 'suggestion-icon'); |
element.appendChild(icon); |
} |
- if (isSecondary) |
+ if (item.isSecondary) |
element.classList.add('secondary'); |
element.tabIndex = -1; |
- var displayText = title.trimEnd(50 + query.length); |
+ var displayText = item.title.trimEnd(50 + query.length); |
var titleElement = element.createChild('span', 'suggestion-title'); |
var index = displayText.toLowerCase().indexOf(query.toLowerCase()); |
@@ -296,32 +293,16 @@ UI.SuggestBox = class { |
titleElement.createChild('span', 'query').textContent = displayText.substring(index, index + query.length); |
titleElement.createChild('span').textContent = displayText.substring(index > -1 ? index + query.length : 0); |
titleElement.createChild('span', 'spacer'); |
- if (subtitle) { |
+ if (item.subtitle) { |
var subtitleElement = element.createChild('span', 'suggestion-subtitle'); |
- subtitleElement.textContent = subtitle.trimEnd(15); |
+ subtitleElement.textContent = item.subtitle.trimEnd(15); |
} |
- element.__fullValue = title; |
+ element.__fullValue = item.title; |
element.addEventListener('mousedown', this._onItemMouseDown.bind(this), false); |
return element; |
} |
/** |
- * @param {!UI.SuggestBox.Suggestions} items |
- * @param {string} userEnteredText |
- * @param {function(number): !Promise<{detail:string, description:string}>=} asyncDetails |
- */ |
- _updateItems(items, userEnteredText, asyncDetails) { |
- this._length = items.length; |
- this._asyncDetailsPromises.clear(); |
- this._asyncDetailsCallback = asyncDetails; |
- this._elementList = []; |
- delete this._selectedElement; |
- |
- this._userEnteredText = userEnteredText; |
- this._items = items; |
- } |
- |
- /** |
* @param {number} index |
* @return {!Promise<?{detail: string, description: string}>} |
*/ |
@@ -358,14 +339,14 @@ UI.SuggestBox = class { |
if (index < 0) |
return; |
- this._selectedElement = this.itemElement(index); |
+ this._selectedElement = this._viewport.elementAtIndex(index); |
this._selectedElement.classList.add('selected'); |
this._selectedElement.classList.add('force-white-icons'); |
this._detailsPopup.classList.add('hidden'); |
var elem = this._selectedElement; |
this._asyncDetails(index).then(showDetails.bind(this), function() {}); |
- this._viewport.scrollItemIntoView(index); |
+ this._viewport.scrollItemAtIndexIntoView(index); |
this._applySuggestion(true); |
/** |
@@ -398,15 +379,6 @@ UI.SuggestBox = class { |
return canShowForSingleItem && completions[0].title !== userEnteredText; |
} |
- _ensureRowCountPerViewport() { |
- if (this._rowCountPerViewport) |
- return; |
- if (!this._items.length) |
- return; |
- |
- this._rowCountPerViewport = Math.floor(this._element.getBoundingClientRect().height / this._rowHeight); |
- } |
- |
/** |
* @param {!AnchorBox} anchorBox |
* @param {!UI.SuggestBox.Suggestions} completions |
@@ -424,11 +396,15 @@ UI.SuggestBox = class { |
asyncDetails) { |
delete this._onlyCompletion; |
if (this._canShowBox(completions, canShowForSingleItem, userEnteredText)) { |
- this._updateItems(completions, userEnteredText, asyncDetails); |
+ this._asyncDetailsPromises.clear(); |
+ this._asyncDetailsCallback = asyncDetails; |
+ delete this._selectedElement; |
+ this._userEnteredText = userEnteredText; |
this._show(); |
- this._updateBoxPosition(anchorBox); |
+ this._updateBoxPosition(anchorBox, completions.length); |
+ this._viewport.setFixedHeight(this._rowHeight); |
+ this._viewport.replaceAllItems(completions); |
this._updateWidth(); |
- this._viewport.refresh(); |
var highestPriorityItem = -1; |
if (selectHighestPriority) { |
var highestPriority = -Infinity; |
@@ -441,7 +417,6 @@ UI.SuggestBox = class { |
} |
} |
this._selectItem(highestPriorityItem); |
- delete this._rowCountPerViewport; |
} else { |
if (completions.length === 1) { |
this._onlyCompletion = completions[0].title; |
@@ -489,16 +464,16 @@ UI.SuggestBox = class { |
* @return {boolean} |
*/ |
pageUpKeyPressed() { |
- this._ensureRowCountPerViewport(); |
- return this._selectClosest(-this._rowCountPerViewport, false); |
+ var rowCount = Math.floor(this._viewport.element.offsetHeight / this._rowHeight); |
+ return this._selectClosest(-rowCount, false); |
} |
/** |
* @return {boolean} |
*/ |
pageDownKeyPressed() { |
- this._ensureRowCountPerViewport(); |
- return this._selectClosest(this._rowCountPerViewport, false); |
+ var rowCount = Math.floor(this._viewport.element.offsetHeight / this._rowHeight); |
+ return this._selectClosest(rowCount, false); |
} |
/** |
@@ -515,41 +490,15 @@ UI.SuggestBox = class { |
// to commit the input or handle it otherwise. |
return hasSelectedItem; |
} |
- |
- /** |
- * @override |
- * @param {number} index |
- * @return {number} |
- */ |
- fastItemHeight(index) { |
- return this._rowHeight; |
- } |
- |
- /** |
- * @override |
- * @return {number} |
- */ |
- itemCount() { |
- return this._items.length; |
- } |
- |
- /** |
- * @override |
- * @param {number} index |
- * @return {?Element} |
- */ |
- itemElement(index) { |
- if (!this._elementList[index]) { |
- this._elementList[index] = this._createItemElement( |
- this._userEnteredText, this._items[index].title, this._items[index].subtitle, this._items[index].iconType, |
- this._items[index].isSecondary); |
- } |
- return this._elementList[index]; |
- } |
}; |
/** |
- * @typedef {!Array.<{title: string, subtitle: (string|undefined), iconType: (string|undefined), priority: (number|undefined), isSecondary: (boolean|undefined)}>} |
+ * @typedef {!{title: string, subtitle: (string|undefined), iconType: (string|undefined), priority: (number|undefined), isSecondary: (boolean|undefined)}} |
+ */ |
+UI.SuggestBox.Suggestion; |
+ |
+/** |
+ * @typedef {!Array<!UI.SuggestBox.Suggestion>} |
*/ |
UI.SuggestBox.Suggestions; |