Implement basic page-switching

This commit is contained in:
Gabriel Tofvesson 2024-10-09 04:10:10 +02:00
parent ffde1b2978
commit df93ccf801

View File

@ -210,26 +210,30 @@ local function updatePageRender(state, pages)
end
local function renderDefault(state, rootElement)
local event = {os.pullEvent()}
local handled = rootElement:handleEvent(event)
if not handled and event[1] == "monitor_resize" then
local width, height = state.monitor.getSize()
if state.width ~= width or state.height ~= height then
state.width = width
state.height = height
-- Trigger re-render
rootElement:setDirty(true)
return function()
local event = {os.pullEvent()}
local handled = rootElement:handleEvent(event)
if not handled and event[1] == "monitor_resize" then
local width, height = state.monitor.getSize()
if state.width ~= width or state.height ~= height then
state.width = width
state.height = height
-- Trigger re-render
rootElement:setDirty(true)
end
end
if not handled and not Event.isClickEvent(event) then
os.queueEvent(table.unpack(event))
end
rootElement:draw()
end
if not handled and not Event.isClickEvent(event) then
os.queueEvent(table.unpack(event))
end
rootElement:draw()
end
local function NOOP() end
local PAGES = {
MAIN = function(state)
local pageState = state:currentPageState({})
@ -248,11 +252,15 @@ local PAGES = {
state.height,
pageState.listState,
function(element, x, y, source, group)
print("Clicked: "..group:getSimpleName())
state:setPage("GROUP_DETAIL", group)
return true
end,
function(page)
state.nextPage = page or state.currentPage
if page == nil then
state:reloadPage()
else
state:setPage(page)
end
end,
function()
print("Importing...")
@ -267,19 +275,30 @@ local PAGES = {
end)
-- Force reload stacks
state:currentPageState({}).stacks = nil
state.nextPage = state.currentPage
state:reloadPage()
end
)
pageState.listState = listState
listResult:setParent(state.monitor)
return function()
renderDefault(state, listResult)
end
return renderDefault(state, listResult)
end,
GROUP_DETAIL = function(state, newPage)
local group = state:getExtra()
if group == nil then
state:setPage("MAIN")
return NOOP
end
local hello = Text:new{
text = "Detail: "..group:getSimpleName(),
parent = state.monitor,
onClick = function(e, x, y, s)
state:setPage("MAIN")
end
}
return renderDefault(state, hello)
end,
REQUEST = function(state, newPage)
@ -317,6 +336,19 @@ function CONTROLLER_STATE:currentPageState(default)
return self.pageState[self.currentPage]
end
function CONTROLLER_STATE:setPage(page, extra)
self.nextPage = page
self.extra = extra
end
function CONTROLLER_STATE:getExtra()
return self.extra
end
function CONTROLLER_STATE:reloadPage()
self.nextPage = self.currentPage
end
while not CONTROLLER_STATE.exit do
updatePageRender(CONTROLLER_STATE, PAGES)
CONTROLLER_STATE._pageRender()