From 419cb82a2de5a20c0a89c05016da47bd91ef51ef Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Sun, 2 Jun 2024 20:41:58 +0200 Subject: [PATCH] [listframes] cache estimatedTotalHeight --- listframes.lua | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/listframes.lua b/listframes.lua index a72f4b0..6f1ef62 100644 --- a/listframes.lua +++ b/listframes.lua @@ -3,6 +3,7 @@ local subFrames = {} local length = 0 local frame = term.current() local drawEntry = nil +local estimatedTotalHeight = 0 -- pagination start line (not number of entries!) local paginationPos = 1 @@ -58,7 +59,11 @@ local function redrawEntry(idx) local tw, th = frame.getSize() local xPos, yPos = entryFrame.getPosition() local xSize, ySize = entryFrame.getSize() - entryFrame.setVisible((yPos + ySize) > 1 and yPos <= th) + local yEnd = yPos+ySize-1 + entryFrame.setVisible(yEnd > 0 and yPos <= th) + if yEnd > estimatedTotalHeight then + estimatedTotalHeight = yEnd + end end end @@ -93,6 +98,7 @@ local function updateItemAt(idx, item) end local function clearFrom(idx) + estimatedTotalHeight = (idx-1).."?" local tw, th = frame.getSize() for j = length, idx, -1 do backingList[j] = nil @@ -144,16 +150,13 @@ local function updatePage(scrollPos) end local function estimatedHeight() - local firstEntryFrame = subFrames[1] local lastEntryFrame = subFrames[length] - if firstEntryFrame ~= nil and lastEntryFrame ~= nil then - local firstXPos, firstYPos = firstEntryFrame.getPosition() + if lastEntryFrame ~= nil and lastEntryFrame.isVisible() then local xPos, yPos = lastEntryFrame.getPosition() local xSize, ySize = lastEntryFrame.getSize() - return yPos-firstYPos+ySize - else - return length + estimatedTotalHeight = yPos+ySize-1 end + return estimatedTotalHeight end