Skip to content

Commit

Permalink
add support for check type item
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 12, 2024
1 parent 2521a33 commit 69a70c5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
48 changes: 37 additions & 11 deletions lua/dyn_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 69a70c5

Please sign in to comment.