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 message handler support #30

Closed
wants to merge 1 commit into from
Closed

add message handler support #30

wants to merge 1 commit into from

Conversation

dyphire
Copy link
Contributor

@dyphire dyphire commented Jan 13, 2024

Other scripts can send a message to render a submenu serialized as JSON

There are two issues to be solved

  • Custom menus sometimes fail to render
  • JSON content update unable to synchronize to menu

@dyphire
Copy link
Contributor Author

dyphire commented Jan 13, 2024

The following is an example patch applied to recentmenu.lua script

---
 scripts/recentmenu.lua | 23 ++++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/scripts/recentmenu.lua b/scripts/recentmenu.lua
index 40d44dc..f17f26b 100644
--- a/scripts/recentmenu.lua
+++ b/scripts/recentmenu.lua
@@ -288,6 +288,21 @@ function open_menu()
     mp.commandv('script-message-to', 'uosc', 'open-menu', json)
 end
 
+function dyn_menu()
+    read_json()
+    local submenu = {}
+    local menu_items = menu.items
+    for _, item in ipairs(menu_items) do
+        submenu[#submenu + 1] = {
+            title = string.format('%s\t%s', item.title, item.hint),
+            cmd = string.format("%s '%s'", item.value[1], item.value[2]),
+        }
+    end
+    local json = utils.format_json(submenu)
+    mp.commandv('script-message-to', 'dyn_menu', 'menu-items', json, 'recent')
+end
+
 function play_last()
     read_json()
     if menu.items[1] then
@@ -310,14 +325,23 @@ function on_load()
     end
     current_item = { path, filename, title }
     append_item(path, filename, title)
+    dyn_menu()
 end
 
 function on_end(e)
     if e and e.reason and e.reason == "quit" then
         append_item(current_item[1], current_item[2], current_item[3])
     end
+    dyn_menu()
 end
 
+mp.register_script_message('dyn_menu-version', function(version)
+    local min_version = '2.0.0'
+    if version >= min_version then
+        dyn_menu()
+    end
+end)
+
 mp.add_key_binding(nil, "open", open_menu)
 mp.add_key_binding(nil, "last", play_last)
 mp.register_event("file-loaded", on_load)
-- 

Other scripts can send a message to render a submenu serialized as JSON
@tsl0922
Copy link
Owner

tsl0922 commented Jan 14, 2024

Why? you can trigger the uosc menu created by recentmenu.lua from this plugin.

I have plan to add script message support, by the syntax is different:

script-message to dyn_menu get <menu_path>
script-message to dyn_menu update <menu_path> <menu_json>
  • menu_path is the submenu title chain, for example: Open > Recent
  • menu_json is the menu item content json, for example: {"title":"Recent","type":"submenu", "submenu":[...]}

@dyphire
Copy link
Contributor Author

dyphire commented Jan 14, 2024

Why? you can trigger the uosc menu created by recentmenu.lua from this plugin.

That's just an example. uosc rendering is a separate menu, while patches implement a right-click submenu.

I have plan to add script message support, by the syntax is different:

script-message to dyn_menu get <menu_path>
script-message to dyn_menu update <menu_path> <menu_json>
  • menu_path is the submenu title chain, for example: Open > Recent

I'm glad to hear that you have a similar idea, but I don't quite agree with the usage of this script message. You plan to have other scripts send the complete chain of submenu titles, but I believe it should be configurable in input.conf instead of being separated from each other.

  • menu_json is the menu item content json, for example: {"title":"Recent","type":"submenu", "submenu":[...]}

This is similar to what I am currently doing, except that I have reused some existing functions.

@tsl0922
Copy link
Owner

tsl0922 commented Jan 14, 2024

Custom menus sometimes fail to render

That's because your script may call script-message-to dyn_menu before dyn_menu.lua init itself.

@dyphire
Copy link
Contributor Author

dyphire commented Jan 14, 2024

Custom menus sometimes fail to render

That's because your script may call script-message-to dyn_menu before dyn_menu.lua init itself.

So, We can communicate this init state to other scripts through set user-data/dyn_menu/init properties. What should I do?

@tsl0922 tsl0922 mentioned this pull request Jan 14, 2024
@tsl0922
Copy link
Owner

tsl0922 commented Jan 14, 2024

What should I do?

#31

@tsl0922 tsl0922 closed this Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants