summaryrefslogtreecommitdiff
path: root/apps/plugins/lua_scripts/submenu_demo.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua_scripts/submenu_demo.lua')
-rw-r--r--apps/plugins/lua_scripts/submenu_demo.lua93
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
2require("printsubmenu")
3
4local scrpath = rb.current_path()
5
6local 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
11end
12
13local 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}
32end
33
34local 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}
39end
40
41local 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
67end
68
69function ShowMain()
70 set_menu(MAIN_MENU())
71end
72
73--ShowMainMenu()
74ShowMain()
75rb.lcd_clear_display()
76rb.lcd_update()
77local lu = collectgarbage("collect")
78local used, allocd, free = rb.mem_stats()
79local lu = collectgarbage("count")
80local 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 ..
83local s_t = {}
84s_t[1] = "rockbox:\n"
85s_t[2] = fmt("Used ", used)
86s_t[3] = fmt("Allocd ", allocd)
87s_t[4] = fmt("Free ", free)
88s_t[5] = "\nlua:\n"
89s_t[6] = fmt("Used", lu * 1024)
90s_t[7] = "\n\nNote that the rockbox used count is a high watermark"
91rb.splash_scroller(5 * rb.HZ, table.concat(s_t))
92--require("print_lua_func")
93os.exit(1, "Goodbye")