diff options
Diffstat (limited to 'apps/plugins/lua_scripts/submenu_demo.lua')
-rw-r--r-- | apps/plugins/lua_scripts/submenu_demo.lua | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/apps/plugins/lua_scripts/submenu_demo.lua b/apps/plugins/lua_scripts/submenu_demo.lua new file mode 100644 index 0000000000..75fec11979 --- /dev/null +++ b/apps/plugins/lua_scripts/submenu_demo.lua | |||
@@ -0,0 +1,93 @@ | |||
1 | --Bilgus 4/2021 Menu with subitems and context demo | ||
2 | require("printsubmenu") | ||
3 | |||
4 | local scrpath = rb.current_path() | ||
5 | |||
6 | local function get_ctx_menu(parent, sel, menu_t, func_t) | ||
7 | local mt = {"Context menu " .. (menu_t[parent] or "ROOT") .. | ||
8 | " : " .. menu_t[sel], "Quit", "Action 1", "Action 2"} | ||
9 | local ft = {false, function() menu_ctx.quit = true return true end} | ||
10 | return mt, ft | ||
11 | end | ||
12 | |||
13 | local function ITEM_MENU() | ||
14 | |||
15 | local function flung(i, menu_t, func_t) | ||
16 | local parent = get_parent() or 0 | ||
17 | rb.splash(100, "flung " .. (menu_t[parent] or "?")) | ||
18 | end | ||
19 | |||
20 | local function foo(i, menu_t, func_t) | ||
21 | local parent = get_parent() or 0 | ||
22 | rb.splash(100, "FOO " .. menu_t[parent]) | ||
23 | end | ||
24 | |||
25 | local function far(i, menu_t, func_t) | ||
26 | local parent = get_parent() or 0 | ||
27 | rb.splash(100, "far" .. menu_t[parent]) | ||
28 | end | ||
29 | |||
30 | return {"Flung", "FOO", "Far"}, | ||
31 | {flung, foo, far} | ||
32 | end | ||
33 | |||
34 | local function USERITEMS() | ||
35 | |||
36 | return {"Item_1", "Item_2", "Item_3"}, | ||
37 | {create_sub_menu(2, ITEM_MENU()), create_sub_menu(2, ITEM_MENU()), | ||
38 | create_sub_menu(2, ITEM_MENU()), function() end} | ||
39 | end | ||
40 | |||
41 | local function MAIN_MENU() | ||
42 | |||
43 | local function go_back(i, m, f) | ||
44 | local parent = get_parent() or 0 | ||
45 | if parent > 0 then | ||
46 | f[parent](parent, m, f) | ||
47 | else | ||
48 | menu_ctx.quit = true | ||
49 | end | ||
50 | menu_ctx.start = parent - 1 | ||
51 | return true | ||
52 | end | ||
53 | |||
54 | local mt = { | ||
55 | [1] = "lua Menu Demo", | ||
56 | [2] = "Items", | ||
57 | [3] = "Back", | ||
58 | } | ||
59 | |||
60 | local ft = { | ||
61 | [0] = go_back, --if user cancels do this function | ||
62 | [1] = false, -- shouldn't happen title occupies this slot | ||
63 | [2] = create_sub_menu(1, USERITEMS()), | ||
64 | [3] = go_back, | ||
65 | } | ||
66 | return mt, ft, get_ctx_menu | ||
67 | end | ||
68 | |||
69 | function ShowMain() | ||
70 | set_menu(MAIN_MENU()) | ||
71 | end | ||
72 | |||
73 | --ShowMainMenu() | ||
74 | ShowMain() | ||
75 | rb.lcd_clear_display() | ||
76 | rb.lcd_update() | ||
77 | local lu = collectgarbage("collect") | ||
78 | local used, allocd, free = rb.mem_stats() | ||
79 | local lu = collectgarbage("count") | ||
80 | local fmt = function(t, v) return string.format("%s: %d Kb\n", t, v /1024) end | ||
81 | |||
82 | -- this is how lua recommends to concat strings rather than .. | ||
83 | local s_t = {} | ||
84 | s_t[1] = "rockbox:\n" | ||
85 | s_t[2] = fmt("Used ", used) | ||
86 | s_t[3] = fmt("Allocd ", allocd) | ||
87 | s_t[4] = fmt("Free ", free) | ||
88 | s_t[5] = "\nlua:\n" | ||
89 | s_t[6] = fmt("Used", lu * 1024) | ||
90 | s_t[7] = "\n\nNote that the rockbox used count is a high watermark" | ||
91 | rb.splash_scroller(5 * rb.HZ, table.concat(s_t)) | ||
92 | --require("print_lua_func") | ||
93 | os.exit(1, "Goodbye") | ||