A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 121 lines 3.3 kB view raw
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 = submenu_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 = submenu_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 = submenu_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 local lv = 2 36 local mt = {"Item_1", "Item_2", "Item_3"} 37 local ft = {} 38 39 local function insert_item(i, name, func) --closure 40 submenu_insert(mt, i, name) 41 submenu_insert(ft, i, func) 42 end 43 44 for i = 1, #mt, 1 do 45 ft[i] = submenu_create(lv, ITEM_MENU()) 46 end 47 48 local function add_new(i, menu_t, func_t) 49 local parent, lv = submenu_get_parent(lv - 1) 50 local last = #mt 51 local name = "Item_" .. tostring(last) 52 local func = submenu_create(lv + 1, ITEM_MENU()) 53 54 local lv_out, item_out, removed = submenu_collapse(parent, lv + 1)-- collapse others 55 submenu_collapse(parent, lv) -- collapse the parent 56 57 insert_item(last, name, func) 58 59 func_t[parent](parent, menu_t, func_t) -- reopen parent 60 menu_ctx.start = i - removed 61 return true 62 end 63 64 local next = #mt + 1 65 insert_item(next, "Add New", add_new) 66 return mt, ft 67end 68 69local function MAIN_MENU() 70 71 local function go_back(i, m, f) 72 local parent = submenu_get_parent() or 0 73 if parent > 0 then 74 f[parent](parent, m, f) 75 else 76 menu_ctx.quit = true 77 end 78 menu_ctx.start = parent - 1 79 return true 80 end 81 82 local mt = { 83 [1] = "lua Menu Demo", 84 [2] = "Items", 85 [3] = "Back", 86 } 87 88 local ft = { 89 [0] = go_back, --if user cancels do this function 90 [1] = false, -- shouldn't happen title occupies this slot 91 [2] = submenu_create(1, USERITEMS()), 92 [3] = go_back, 93 } 94 return mt, ft, get_ctx_menu 95end 96 97function ShowMain() 98 set_menu(MAIN_MENU()) 99end 100 101--ShowMainMenu() 102ShowMain() 103rb.lcd_clear_display() 104rb.lcd_update() 105local lu = collectgarbage("collect") 106local used, allocd, free = rb.mem_stats() 107local lu = collectgarbage("count") 108local fmt = function(t, v) return string.format("%s: %d Kb\n", t, v /1024) end 109 110-- this is how lua recommends to concat strings rather than .. 111local s_t = {} 112s_t[1] = "rockbox:\n" 113s_t[2] = fmt("Used ", used) 114s_t[3] = fmt("Allocd ", allocd) 115s_t[4] = fmt("Free ", free) 116s_t[5] = "\nlua:\n" 117s_t[6] = fmt("Used", lu * 1024) 118s_t[7] = "\n\nNote that the rockbox used count is a high watermark" 119rb.splash_scroller(5 * rb.HZ, table.concat(s_t)) 120--require("print_lua_func") 121os.exit(1, "Goodbye")