Clean up code

This commit is contained in:
Gabriel Tofvesson 2024-10-13 17:39:50 +02:00
parent 3943bbcc19
commit 86f378b5f4
2 changed files with 17 additions and 16 deletions

View File

@ -52,43 +52,43 @@ end
function Element:setPos(x, y)
if (x ~= nil and self.x ~= x) or (y ~= nil and self.y ~= y) then
self:setDirty()
self.x = x or self.x
self.y = y or self.y
self:setDirty()
self:_reload()
end
end
function Element:setX(x)
if self.x ~= x then
self:setDirty()
self.x = x
self:setDirty()
self:_reload()
end
end
function Element:setY(y)
if self.y ~= y then
self:setDirty()
self.y = y
self:setDirty()
self:_reload()
end
end
function Element:setParent(parent)
if self.parent ~= parent then
self:setDirty()
self.parent = parent
self:setDirty()
self:_reload()
end
end
function Element:setFgColor(color)
if color ~= self.fgColor then
self:setDirty()
self.fgColor = color
self:setDirty()
local win = self:_getWindow()
if win ~= nil then
win.setTextColor(color)
@ -98,9 +98,9 @@ end
function Element:setBgColor(color)
if color ~= self.bgColor then
self:setDirty()
self.bgColor = color
self:setDirty()
local win = self:_getWindow()
if win ~= nil then
win.setBackgroundColor(color)
@ -121,15 +121,15 @@ function Element:_getWindow()
end
function Element:_setWindow(window)
self:setDirty()
self.window = window
end
function Element:setVisible(visible)
local win = self:_getWindow()
self.visible = visible
self:setDirty()
local win = self:_getWindow()
if win ~= nil then
self:setDirty()
win.setVisible(visible)
end
end
@ -140,25 +140,25 @@ 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()
self.width = opts.width or self.width
self.height = opts.height or self.height
self:setDirty()
self:_reload()
end
end
function Element:setWidth(width)
if width ~= self.width then
self:setDirty()
self.width = width
self:setDirty()
self:_reload()
end
end
function Element:setHeight(height)
if height ~= self.height then
self:setDirty()
self.height = height
self:setDirty()
self:_reload()
end
end
@ -214,6 +214,7 @@ function Element:_reload()
win.setTextColor(self:getFgColor())
win.setVisible(self.visible)
self:_setWindow(win)
self:setDirty(true)
end
end

View File

@ -10,7 +10,6 @@ local Element = require("gfx.element")
local Progress = require("gfx.progress")
local Prop = require("gfx.prop")
local Children = require("gfx.prop.children")
--local Visibility = require("gfx.prop.visibility")
local Orientation = require("gfx.prop.orientation")
@ -219,6 +218,7 @@ local function updatePageRender(state, pages)
end
local function renderDefault(state, rootElement)
rootElement:setDirty(true)
rootElement:setParent(state.monitor)
rootElement:_reload()
return function()
@ -229,17 +229,17 @@ local function renderDefault(state, rootElement)
if state.width ~= width or state.height ~= height then
state.width = width
state.height = height
-- Trigger re-render
rootElement:setDirty(true)
rootElement:_reload()
end
end
if not handled and not Event.isClickEvent(event) then
os.queueEvent(table.unpack(event))
end
rootElement:draw()
end
end