feat: menu pages dispatching

This commit is contained in:
MihailRis 2025-01-16 07:40:34 +03:00
parent 43dc88c6b5
commit 1a246e2ae0
3 changed files with 23 additions and 2 deletions

View File

@ -37,4 +37,6 @@ end
function on_open()
refresh()
input.add_callback("key:f5", refresh, document.root)
end

View File

@ -1,4 +1,6 @@
local gui_util = {}
local gui_util = {
local_dispatchers = {}
}
--- Parse `pagename?arg1=value1&arg2=value2` queries
--- @param query page query string
@ -25,6 +27,11 @@ end
--- @return document_id
function gui_util.load_page(query)
local name, args = gui_util.parse_query(query)
for i = #gui_util.local_dispatchers, 1, -1 do
local newname, newargs = gui_util.local_dispatchers[i](name, args)
name = newname or name
args = newargs or args
end
local filename = file.find(string.format("layouts/pages/%s.xml", name))
if filename then
name = file.prefix(filename)..":pages/"..name
@ -33,4 +40,15 @@ function gui_util.load_page(query)
end
end
function gui_util.add_page_dispatcher(dispatcher)
if type(dispatcher) ~= "function" then
error("function expected")
end
table.insert(gui_util.local_dispatchers, dispatcher)
end
function gui_util.reset_local()
gui_util.local_dispatchers = {}
end
return gui_util

View File

@ -201,7 +201,7 @@ _GUI_ROOT = Document.new("core:root")
_MENU = _GUI_ROOT.menu
menu = _MENU
local gui_util = require "core:gui_util"
gui_util = require "core:gui_util"
__vc_page_loader = gui_util.load_page
--- Console library extension ---
@ -408,6 +408,7 @@ end
function __vc_on_world_quit()
_rules.clear()
gui_util:reset_local()
end
local __vc_coroutines = {}