diff --git a/itemcontroller.lua b/itemcontroller.lua index b982015..c6fc426 100644 --- a/itemcontroller.lua +++ b/itemcontroller.lua @@ -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()