Skip to content

Commit

Permalink
add new state syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 27, 2024
1 parent eddaf09 commit 1b6375a
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions lua/dyn_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,33 @@ local function update_profiles_menu(menu)
observe_property(menu, 'profile-list', 'native', profile_list_cb)
end

-- handle #@prop:check
function update_check_status(menu, prop, reverse)
-- update menu status with the result of expr expansion
local function update_menu_state(menu, expr)
local function update_state(str)
local state = {}
for s in str:gmatch('[^,]+') do table.insert(state, s) end
menu.item.state = state
menu_items_dirty = true
end
local function prop_cb()
local text = mp.command_native({ 'expand-text', expr })
if text then update_state(text) end
end

local has_prop = false
for match in string.gmatch(expr, '${[?!]*(.-)}') do
local prop = match:match('^([%w-/]+)[:=].-$') or match
if prop then
has_prop = true
observe_property(menu, prop, 'native', prop_cb)
end
end

if not has_prop then update_state(expr) end
end

-- handle #@prop:check (deprecated)
local function update_check_status(menu, prop, reverse)
local item = menu.item

local function check(v)
Expand Down Expand Up @@ -415,12 +440,17 @@ local function dyn_menu_update(item, keyword)
}
dyn_menus[keyword] = menu

local prop, e = keyword:match('^([%w-]+):check(!?)$')
if prop then
update_check_status(menu, prop, e == '!')
local expr = keyword:match('^state=([%S]+)$')
if expr then
update_menu_state(menu, expr)
else
local provider = dyn_providers[keyword]
if provider then provider(menu) end
local prop, e = keyword:match('^([%w-]+):check(!?)$')
if prop then
update_check_status(menu, prop, e == '!')
else
local provider = dyn_providers[keyword]
if provider then provider(menu) end
end
end
end

Expand Down

0 comments on commit 1b6375a

Please sign in to comment.