From 69a70c5a3c617746442adeaa43151b3a6c1d5db8 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Fri, 12 Jan 2024 22:19:44 +0800 Subject: [PATCH] add support for check type item --- README.md | 18 ++++++++++++------ lua/dyn_menu.lua | 48 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index ec99e1a..40e17b2 100644 --- a/README.md +++ b/README.md @@ -34,18 +34,24 @@ The menu syntax is similar to [mpv.net](https://github.com/mpvnet-player/mpv.net - `#@audio-devices`: audio device list - `#@playlist`: playlist - `#@profiles`: profile list + - use `#@prop:check` to check menu item if property type and value is: + - `boolean`: `true` + - `string`: not empty + - `number`: not zero + - `table`: not empty + - none of above: not `nil` - use `_` if no keybinding - use `ignore` if no command ``` Ctrl+a show-text foobar #menu: Foo > Bar _ ignore #menu: - - -_ ignore #menu: Tracks #@tracks -_ ignore #menu: Chapters #@chapters -_ ignore #menu: Editions #@editions -_ ignore #menu: - -_ ignore #menu: Audio Devices #@audio-devices +_ ignore #menu: Tracks #@tracks +_ ignore #menu: Chapters #@chapters +_ ignore #menu: Editions #@editions +_ ignore #menu: - +_ cycle mute #menu: Audio > Mute #@mute:check +_ ignore #menu: Audio > Devices #@audio-devices ``` Add a keybinding to trigger the menu (required): diff --git a/lua/dyn_menu.lua b/lua/dyn_menu.lua index 87c4e7c..6e1a32b 100644 --- a/lua/dyn_menu.lua +++ b/lua/dyn_menu.lua @@ -4,16 +4,19 @@ -- #@keyword support for dynamic menu -- -- supported keywords: --- #@tracks: video/audio/sub tracks --- #@tracks/video: video track list --- #@tracks/audio: audio track list --- #@tracks/sub: subtitle list --- #@tracks/sub-secondary: subtitle list (secondary) --- #@chapters: chapter list --- #@editions: edition list --- #@audio-devices: audio device list --- #@playlist: playlist --- #@profiles: profile list +-- +-- #@tracks video/audio/sub tracks +-- #@tracks/video video track list +-- #@tracks/audio audio track list +-- #@tracks/sub subtitle list +-- #@tracks/sub-secondary subtitle list (secondary) +-- #@chapters chapter list +-- #@editions edition list +-- #@audio-devices audio device list +-- #@playlist playlist +-- #@profiles profile list +-- +-- #@prop:check check menu item based on property value local opts = require('mp.options') local utils = require('mp.utils') @@ -309,8 +312,31 @@ local function update_profiles_menu(submenu) end) end --- update dynamic menu item and handle submenu update +-- handle #@prop:check +function update_check_status(item, keyword) + local prop = keyword:match('^([%w-]+):check$') + if not prop then return false end + + local function check(v) + local tp = type(v) + if tp == 'boolean' then return v end + if tp == 'string' then return v ~= '' end + if tp == 'number' then return v ~= 0 end + if tp == 'table' then return #v > 0 end + return v ~= nil + end + mp.observe_property(prop, 'native', function(name, value) + item.state = check(value) and { 'checked' } or {} + menu_items_dirty = true + end) + + return true +end + +-- update dynamic menu item and handle update local function dyn_menu_update(item, keyword) + if update_check_status(item, keyword) then return end + item.type = 'submenu' item.submenu = {} item.cmd = nil