Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'list' message and add ability to modify certain menu item #51

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
57 changes: 44 additions & 13 deletions src/lua/dyn_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,10 @@ local function dyn_menu_load(item, keyword)
dirty = false,
}
dyn_menus[#dyn_menus + 1] = menu
keyword_to_menu[keyword] = menu
local list = keyword_to_menu[keyword] or {}
list[#list+1] = menu
keyword_to_menu[keyword] = list
list = nil

local expr = keyword:match('^state=(.-)%s*$')
if expr then
Expand Down Expand Up @@ -504,23 +507,39 @@ local function load_dyn_menus()
mp.commandv('script-message', 'menu-ready', mp.get_script_name())
end

-- script message: list <src>
mp.register_script_message('list', function(src)
local list = {}
for name, _ in pairs(keyword_to_menu) do
list[#list + 1] = name
end

mp.commandv('script-message-to', src, 'menu-list-reply', utils.format_json(list))
end)

-- script message: get <keyword> <src>
mp.register_script_message('get', function(keyword, src)
if not src or src == '' then
msg.debug('get: ignored message with empty src')
return
end

local menu = keyword_to_menu[keyword]
local list = keyword_to_menu[keyword] or nil
local reply = { keyword = keyword }
if menu then reply.item = menu.item else reply.error = 'keyword not found' end
mp.commandv('script-message-to', src, 'menu-get-reply', utils.format_json(reply))

if not list then reply.error = 'keyword not found' return end

for id, menu in ipairs(list) do
reply.item = menu.item
reply.id = id
mp.commandv('script-message-to', src, 'menu-get-reply', utils.format_json(reply))
end
end)

-- script message: update <keyword> <json>
mp.register_script_message('update', function(keyword, json)
local menu = keyword_to_menu[keyword]
if not menu then
-- script message: update <keyword> <json> <opt:id>
mp.register_script_message('update', function(keyword, json, id)
local list = keyword_to_menu[keyword] or nil
if not list then
msg.debug('update: ignored message with invalid keyword:', keyword)
return
end
Expand All @@ -532,12 +551,24 @@ mp.register_script_message('update', function(keyword, json)
return
end

local item = menu.item
if not data.title or data.title == '' then data.title = item.title end
if not data.type or data.type == '' then data.type = item.type end
local function update(menu)
local item = menu.item
if not data.title or data.title == '' then data.title = item.title end
if not data.type or data.type == '' then data.type = item.type end

for k, _ in pairs(item) do item[k] = nil end
for k, v in pairs(data) do item[k] = v end
for k, _ in pairs(item) do item[k] = nil end
for k, v in pairs(data) do item[k] = v end
end
if not id then
for _, menu in ipairs(list) do
update(menu)
end
else
local pos = tonumber(id)
if not pos then msg.error('update: invalid id:', id) return end
local menu = list[pos]
update(menu)
end

menu_items_dirty = true
end)
Expand Down