[listframes] cache estimatedTotalHeight

This commit is contained in:
Ben 2024-06-02 20:41:58 +02:00
parent 2a8e35ad94
commit 419cb82a2d
Signed by: ben
GPG key ID: 0F54A7ED232D3319

View file

@ -3,6 +3,7 @@ local subFrames = {}
local length = 0 local length = 0
local frame = term.current() local frame = term.current()
local drawEntry = nil local drawEntry = nil
local estimatedTotalHeight = 0
-- pagination start line (not number of entries!) -- pagination start line (not number of entries!)
local paginationPos = 1 local paginationPos = 1
@ -58,7 +59,11 @@ local function redrawEntry(idx)
local tw, th = frame.getSize() local tw, th = frame.getSize()
local xPos, yPos = entryFrame.getPosition() local xPos, yPos = entryFrame.getPosition()
local xSize, ySize = entryFrame.getSize() 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
end end
@ -93,6 +98,7 @@ local function updateItemAt(idx, item)
end end
local function clearFrom(idx) local function clearFrom(idx)
estimatedTotalHeight = (idx-1).."?"
local tw, th = frame.getSize() local tw, th = frame.getSize()
for j = length, idx, -1 do for j = length, idx, -1 do
backingList[j] = nil backingList[j] = nil
@ -144,16 +150,13 @@ local function updatePage(scrollPos)
end end
local function estimatedHeight() local function estimatedHeight()
local firstEntryFrame = subFrames[1]
local lastEntryFrame = subFrames[length] local lastEntryFrame = subFrames[length]
if firstEntryFrame ~= nil and lastEntryFrame ~= nil then if lastEntryFrame ~= nil and lastEntryFrame.isVisible() then
local firstXPos, firstYPos = firstEntryFrame.getPosition()
local xPos, yPos = lastEntryFrame.getPosition() local xPos, yPos = lastEntryFrame.getPosition()
local xSize, ySize = lastEntryFrame.getSize() local xSize, ySize = lastEntryFrame.getSize()
return yPos-firstYPos+ySize estimatedTotalHeight = yPos+ySize-1
else
return length
end end
return estimatedTotalHeight
end end