stand/lua: Extract menu handlers out into menu.handlers table

This is a bit cleaner than our former method of an if ... else chain of
handlers. Store handlers in the menu.handlers table so that they may be
added to or removed dynamically.

All handlers take the current menu and selected entry as parameters, and
their return value indicates whether the menu processor should continue or
not. An omitted return value or 'true' will indicate that we should
continue, while returning 'false' will indicate that we should exit the
current menu.

The omitted return value behavior is due to continuing the loop being the
more common situation.
This commit is contained in:
Kyle Evans 2018-02-20 03:40:16 +00:00
parent 81d68271d7
commit 8d415029e1
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=329619

View File

@ -42,6 +42,40 @@ local run;
local autoboot;
local carousel_choices = {};
menu.handlers = {
-- Menu handlers take the current menu and selected entry as parameters,
-- and should return a boolean indicating whether execution should
-- continue or not. The return value may be omitted if this entry should
-- have no bearing on whether we continue or not, indicating that we
-- should just continue after execution.
[core.MENU_ENTRY] = function(current_menu, entry)
-- run function
entry.func();
end,
[core.MENU_CAROUSEL_ENTRY] = function(current_menu, entry)
-- carousel (rotating) functionality
local carid = entry.carousel_id;
local caridx = menu.getCarouselIndex(carid);
local choices = entry.items();
if (#choices > 0) then
caridx = (caridx % #choices) + 1;
menu.setCarouselIndex(carid, caridx);
entry.func(caridx, choices[caridx], choices);
end
end,
[core.MENU_SUBMENU] = function(current_menu, entry)
-- recurse
return menu.run(entry.submenu());
end,
[core.MENU_RETURN] = function(current_menu, entry)
-- allow entry to have a function/side effect
if (entry.func ~= nil) then
entry.func();
end
return false;
end,
};
-- loader menu tree is rooted at menu.welcome
menu.boot_options = {
@ -338,31 +372,16 @@ function menu.run(m)
-- if we have an alias do the assigned action:
if (sel_entry ~= nil) then
if (sel_entry.entry_type == core.MENU_ENTRY) then
-- run function
sel_entry.func();
elseif (sel_entry.entry_type == core.MENU_CAROUSEL_ENTRY) then
-- carousel (rotating) functionality
local carid = sel_entry.carousel_id;
local caridx = menu.getCarouselIndex(carid);
local choices = sel_entry.items();
if (#choices > 0) then
caridx = (caridx % #choices) + 1;
menu.setCarouselIndex(carid, caridx);
sel_entry.func(caridx, choices[caridx],
choices);
-- Get menu handler
local handler = menu.handlers[sel_entry.entry_type];
if (handler ~= nil) then
-- The handler's return value indicates whether
-- we need to exit this menu. An omitted return
-- value means "continue" by default.
cont = handler(m, sel_entry);
if (cont == nil) then
cont = true;
end
elseif (sel_entry.entry_type == core.MENU_SUBMENU) then
-- recurse
cont = menu.run(sel_entry.submenu());
elseif (sel_entry.entry_type == core.MENU_RETURN) then
-- allow entry to have a function/side effect
if (sel_entry.func ~= nil) then
sel_entry.func();
end
-- break recurse
cont = false;
end
-- if we got an alias key the screen is out of date:
screen.clear();