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,6 +210,7 @@ local function updatePageRender(state, pages)
end
local function renderDefault(state, rootElement)
return function()
local event = {os.pullEvent()}
local handled = rootElement:handleEvent(event)
if not handled and event[1] == "monitor_resize" then
@ -229,6 +230,9 @@ local function renderDefault(state, rootElement)
rootElement:draw()
end
end
local function NOOP() end
local PAGES = {
MAIN = function(state)
@ -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()