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 command palette support #36

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 59 additions & 17 deletions recentmenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ options.read_options(o, _, function() end)
local path = mp.command_native({ "expand-path", o.path })

local uosc_available = false
local command_palette_available = false

local menu = {
type = 'recent_menu',
Expand Down Expand Up @@ -270,27 +271,50 @@ function append_item(path, filename, title)
write_json()
end

function open_menu_uosc()
local json = utils.format_json(menu)
mp.commandv('script-message-to', 'uosc', 'open-menu', json)
end

function open_menu_command_palette()
local json = utils.format_json(menu)
mp.commandv('script-message-to',
'command_palette',
'show-command-palette-json', json)
end

function open_menu_select()
local item_titles, item_values = {}, {}
for i, v in ipairs(menu.items) do
item_titles[i] = v.title
item_values[i] = v.value
end
mp.commandv('script-message-to', 'console', 'disable')
input.select({
prompt = menu.title .. ':',
items = item_titles,
default_item = 1,
submit = function(id)
mp.commandv(unpack(item_values[id]))
end,
})
end

function open_menu()
read_json()
if uosc_available then
local json = utils.format_json(menu)
mp.commandv('script-message-to', 'uosc', 'open-menu', json)
elseif input_available then
local item_titles, item_values = {}, {}
for i, v in ipairs(menu.items) do
item_titles[i] = v.title
item_values[i] = v.value
end
mp.commandv('script-message-to', 'console', 'disable')
input.select({
prompt = menu.title .. ':',
items = item_titles,
default_item = 1,
submit = function(id)
mp.commandv(unpack(item_values[id]))
end,
})
open_menu_uosc()
return
end
if command_palette_available then
open_menu_command_palette()
return
end
if input_available then
open_menu_select()
return
end
mp.msg.warn("No menu providers available")
end

function get_dyn_menu_title(title, hint, path)
Expand Down Expand Up @@ -373,6 +397,20 @@ mp.add_key_binding(nil, "last", play_last)
mp.register_event("file-loaded", on_load)
mp.register_event("end-file", on_end)

mp.register_script_message('open-recent-menu', function(provider)
if provider == nil then
open_menu()
elseif provider == "uosc" then
open_menu_uosc()
elseif provider == "command-palette" then
open_menu_command_palette()
elseif provider == "select" then
open_menu_select()
else
mp.msg.warn(provider + "not available")
end
end)

mp.register_script_message('uosc-version', function()
uosc_available = true
mp.commandv('script-message-to', 'uosc', 'get-locale', mp.get_script_name())
Expand All @@ -382,6 +420,10 @@ mp.register_script_message('uosc-locale', function(json)
menu.title = t(menu.title)
end)

mp.register_script_message('command-palette-version', function()
command_palette_available = true
end)

mp.register_script_message('menu-ready', function(script_name)
dyn_menu.ready = true
dyn_menu.script_name = script_name
Expand Down