Only render when dirty

This commit is contained in:
Gabriel Tofvesson 2024-10-04 00:29:14 +02:00
parent 3d1da9177a
commit 552c19db47
4 changed files with 77 additions and 18 deletions

View File

@ -31,11 +31,15 @@ function Element:new(o)
end
function Element:draw()
local win = self:_getWindow()
-- Calling draw() without a valid window is a logical error
---@diagnostic disable-next-line: need-check-nil
win.setCursorPos(1, 1)
local dirty = self:_isDirty()
if dirty then
local win = self:_getWindow()
-- Calling draw() without a valid window is a logical error
---@diagnostic disable-next-line: need-check-nil
win.setCursorPos(1, 1)
end
self.dirty = false
return dirty
end
function Element:getX()
@ -76,6 +80,7 @@ function Element:setParent(parent)
if self.parent ~= parent then
self:setDirty()
self.parent = parent
self:_reload()
end
end
@ -115,6 +120,10 @@ function Element:setVisible(visible)
self:_getWindow().setVisible(visible)
end
function Element:_isDirty()
return self.dirty
end
function Element:resize(opts)
if (opts.width ~= nil and self.x ~= opts.width) or (opts.height ~= nil and self.y ~= opts.height) then
self:setDirty()
@ -189,10 +198,12 @@ function Element:setOnClick(onClick)
end
function Element:_reload()
local window = window.create(self.parent, self:getX(), self:getY(), self:getWidth(), self:getHeight(), self:isVisible())
window.setBackgroundColor(self:getBgColor())
window.setTextColor(self:getFgColor())
self:_setWindow(window)
if self.parent ~= nil then
local window = window.create(self.parent, self:getX(), self:getY(), self:getWidth(), self:getHeight(), self:isVisible())
window.setBackgroundColor(self:getBgColor())
window.setTextColor(self:getFgColor())
self:_setWindow(window)
end
end
return Element

View File

@ -21,9 +21,22 @@ end
function List:insertChild(child, atIndex)
local index = math.min(math.max(1, atIndex or #self.children), #self.children)
table.insert(self.children, index, child)
-- Update window references
self:_reload()
-- Update render positions
adjustPositions(self.children, self:isVertical(), index)
end
function List:setParent(parent)
Element.setParent(self, parent)
local win = self:_getWindow()
for _,child in ipairs(self.children) do
child:setParent(win)
end
end
function List:removeChild(child)
local index
local searchType = type(child)
@ -51,6 +64,7 @@ function List:removeChild(child)
end
local removed = table.remove(self.children, index)
self:_reload()
if index <= #self.children then
adjustPositions(self.children, self:isVertical(), index)
end
@ -67,11 +81,14 @@ function List:isHorizontal()
end
function List:draw()
Element.draw(self)
self:_getWindow().clear()
for _,v in ipairs(self.children) do
v:draw()
local dirty = Element.draw(self)
if dirty then
self:_getWindow().clear()
for _,child in ipairs(self.children) do
child:draw()
end
end
return dirty
end
function List:getHeight()
@ -105,4 +122,29 @@ function List:findById(id)
return nil
end
function List:_isDirty()
if Element._isDirty(self) then
return true
end
for _,child in ipairs(self.childrent) do
if child:_isDirty() then
return true
end
end
return false
end
function List:_reload()
Element._reload(self)
-- Reload child windows
local win = self:_getWindow()
for _,child in ipairs(self.children) do
if child:_getWindow() ~= win then
child:setParent(win)
end
end
end
return List

View File

@ -28,10 +28,13 @@ function Padding:resize(opts)
end
function Padding:draw()
Element.draw(self)
local win = self:_getWindow()
win.clear()
self.element:draw()
if Element.draw(self) or self.element:_isDirty() then
local win = self:_getWindow()
win.clear()
self.element:draw()
return true
end
return false
end
function Padding:getPaddingLeft()

View File

@ -26,8 +26,11 @@ function Text:getText()
end
function Text:draw()
Element.draw(self)
self:_getWindow().write(self:getText())
local dirty = Element.draw(self)
if dirty then
self:_getWindow().write(self:getText())
end
return dirty
end
function Text:getHeight()