From ae2c9e19d72ecc701d9ce64ae44cbe1f1e77413b Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 15 Dec 2018 00:36:18 -0400 Subject: Optimize getScrollPositionOfElementAtEndOfView This actually drastically improves the performance of mtui when opening very, very large playlists. --- ui/form/ListScrollForm.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'ui') diff --git a/ui/form/ListScrollForm.js b/ui/form/ListScrollForm.js index 77bebcd..d22cd35 100644 --- a/ui/form/ListScrollForm.js +++ b/ui/form/ListScrollForm.js @@ -158,14 +158,21 @@ module.exports = class ListScrollForm extends Form { getScrollPositionOfElementAtEndOfView(element) { // We can decide how many items to scroll past by moving forward until // the item's far edge is visible. + const pos = this.getItemPos(element); let edge = this.formEdge - let i = 0 - for (; i < this.inputs.length; i++) { - if (this.getItemPos(element) <= edge) break + for (let i = 0; i < this.inputs.length; i++) { + if (pos <= edge) { + return i; + } - edge += this.inputs[i][this.sizeProp] + if (this.sizeProp === 'w') { + edge += this.inputs[i].w; + } else { + edge += this.inputs[i].h; + } } - return i + // No result? Well, it's at the end. + return this.inputs.length; } scrollElementIntoEndOfView(element) { @@ -199,8 +206,16 @@ module.exports = class ListScrollForm extends Form { getItemPos(item) { // Gets the position of the item in an unscrolled view. - return this.inputs.slice(0, this.inputs.indexOf(item) + 1) - .reduce((a, b) => a + b[this.sizeProp], 0) + const index = this.inputs.indexOf(item); + let pos = 0; + for (let i = 0; i <= index; i++) { + if (this.sizeProp === 'w') { + pos += this.inputs[i].w; + } else { + pos += this.inputs[i].h; + } + } + return pos; } getSizeProp() { -- cgit 1.3.0-6-gf8a5