Skip to content

Commit

Permalink
add more menu messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Feb 5, 2024
1 parent 13fc505 commit 1b07a84
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/lua/dialog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local o = {
}
opts.read_options(o)

local menu_native = 'menu'
local open_action = ''
local save_action = ''
local save_arg1 = nil
Expand Down Expand Up @@ -139,6 +140,9 @@ mp.register_script_message('dialog-open-folder-reply', open_folder_cb)
mp.register_script_message('dialog-save-reply', save_cb)
mp.register_script_message('clipboard-get-reply', clipboard_cb)

-- detect dll client name
mp.register_script_message('menu-init', function(name) menu_native = name end)

-- open dialog
mp.register_script_message('open', function(action)
local function append_raw(filters, name, spec)
Expand Down Expand Up @@ -174,12 +178,12 @@ mp.register_script_message('open', function(action)
end

mp.set_property_native('user-data/menu/dialog/filters', filters)
mp.commandv('script-message-to', 'menu', 'dialog/open-multi', mp.get_script_name())
mp.commandv('script-message-to', menu_native, 'dialog/open-multi', mp.get_script_name())
end)

-- open folder dialog
mp.register_script_message('open-folder', function()
mp.commandv('script-message-to', 'menu', 'dialog/open-folder', mp.get_script_name())
mp.commandv('script-message-to', menu_native, 'dialog/open-folder', mp.get_script_name())
end)

-- save dialog
Expand Down Expand Up @@ -213,18 +217,18 @@ mp.register_script_message('save', function(action, arg1)
mp.osd_message('unknown save action: ' .. save_action)
return
end
mp.commandv('script-message-to', 'menu', 'dialog/save', mp.get_script_name())
mp.commandv('script-message-to', menu_native, 'dialog/save', mp.get_script_name())
end)

-- open clipboard
mp.register_script_message('open-clipboard', function(action)
open_action = action
mp.commandv('script-message-to', 'menu', 'clipboard/get', mp.get_script_name())
mp.commandv('script-message-to', menu_native, 'clipboard/get', mp.get_script_name())
end)

-- set clipboard
mp.register_script_message('set-clipboard', function(text)
if not text then return end
local value = text:gsub('\xFD.-\xFE', '')
mp.commandv('script-message-to', 'menu', 'clipboard/set', value)
mp.commandv('script-message-to', menu_native, 'clipboard/set', value)
end)
13 changes: 11 additions & 2 deletions src/lua/dyn_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local o = {
}
opts.read_options(o)

local menu_native = 'menu' -- dll client name
local menu_prop = 'user-data/menu/items' -- menu data property
local menu_items = {} -- raw menu data
local menu_items_dirty = false -- menu data dirty flag
Expand Down Expand Up @@ -499,7 +500,7 @@ local function load_dyn_menus()
dyn_menu_check(menu_items)

-- broadcast menu ready message
mp.commandv('script-message', 'menu-ready')
mp.commandv('script-message', 'menu-ready', mp.get_script_name())
end

-- script message: get <keyword> <src>
Expand Down Expand Up @@ -540,6 +541,14 @@ mp.register_script_message('update', function(keyword, json)
menu_items_dirty = true
end)

-- script binding: show
mp.add_key_binding('MBTN_RIGHT', 'show', function()
mp.commandv('script-message-to', menu_native, 'show')
end)

-- detect dll client name
mp.register_script_message('menu-init', function(name) menu_native = name end)

-- detect uosc installation
mp.register_script_message('uosc-version', function() has_uosc = true end)

Expand Down Expand Up @@ -590,7 +599,7 @@ local function parse_input_conf(conf)
if not cmd or cmd == '' then return '' end
local title = cmd:match('#menu:%s*(.*)%s*')
if not title and o.uosc_syntax then title = cmd:match('#!%s*(.*)%s*') end
if title then title = title:match('(.-)%s+#.*$') or title end
if title then title = title:match('(.-)%s*#.*$') or title end
return title or ''
end

Expand Down
4 changes: 4 additions & 0 deletions src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ void show_menu(plugin_ctx *ctx, POINT *pt) {
if (!PtInRect(&rc, *pt)) return;

ClientToScreen(ctx->hwnd, pt);
mpv_command(ctx->mpv,
(const char *[]){"script-message", "menu-open", NULL});
TrackPopupMenuEx(ctx->hmenu, TPM_LEFTALIGN | TPM_LEFTBUTTON, pt->x, pt->y,
ctx->hwnd, NULL);
mpv_command(ctx->mpv,
(const char *[]){"script-message", "menu-close", NULL});
}

// run mpv command stored in menu item data
Expand Down
3 changes: 3 additions & 0 deletions src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ MPV_EXPORT int mpv_open_cplugin(mpv_handle *handle) {
mpv_observe_property(handle, 0, "window-id", MPV_FORMAT_INT64);
mpv_observe_property(handle, 0, MENU_DATA_PROP, MPV_FORMAT_NODE);

mpv_command(handle, (const char *[]){"script-message", "menu-init",
mpv_client_name(handle), NULL});

while (handle) {
mpv_event *event = mpv_wait_event(handle, -1);
if (event->event_id == MPV_EVENT_SHUTDOWN) break;
Expand Down

0 comments on commit 1b07a84

Please sign in to comment.