Skip to content

Commit

Permalink
polish menu update code
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 14, 2024
1 parent 722ff1b commit 8a43db3
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions lua/dyn_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,8 @@ local function update_profiles_menu(menu)
end

-- handle #@prop:check
function update_check_status(menu, keyword)
function update_check_status(menu, prop, reverse)
local item = menu.item
local prop, e = keyword:match('^([%w-]+):check(!?)$')
if not prop then return false end

local function check(v)
local tp = type(v)
Expand All @@ -361,16 +359,28 @@ function update_check_status(menu, keyword)

local function prop_cb(_, value)
local ok = check(value)
if e == '!' then ok = not ok end
if reverse then ok = not ok end
item.state = ok and { 'checked' } or {}
menu_items_dirty = true
end

observe_property(menu, prop, 'native', prop_cb)

return true
end

-- dynamic menu providers
local dyn_providers = {
['tracks'] = update_tracks_menu,
['tracks/video'] = function(menu) update_track_menu(menu, 'video', 'vid') end,
['tracks/audio'] = function(menu) update_track_menu(menu, 'audio', 'aid') end,
['tracks/sub'] = function(menu) update_track_menu(menu, 'sub', 'sid') end,
['tracks/sub-secondary'] = function(menu) update_track_menu(menu, 'sub', 'secondary-sid') end,
['chapters'] = update_chapters_menu,
['editions'] = update_editions_menu,
['audio-devices'] = update_audio_devices_menu,
['playlist'] = update_playlist_menu,
['profiles'] = update_profiles_menu,
}

-- update dynamic menu item and handle update
local function dyn_menu_update(item, keyword)
local menu = {
Expand All @@ -379,28 +389,12 @@ local function dyn_menu_update(item, keyword)
}
dyn_menus[keyword] = menu

if update_check_status(menu, keyword) then return end

if keyword == 'tracks' then
update_tracks_menu(menu)
elseif keyword == 'tracks/video' then
update_track_menu(menu, "video", "vid")
elseif keyword == 'tracks/audio' then
update_track_menu(menu, "audio", "aid")
elseif keyword == 'tracks/sub' then
update_track_menu(menu, "sub", "sid")
elseif keyword == 'tracks/sub-secondary' then
update_track_menu(menu, "sub", "secondary-sid")
elseif keyword == 'chapters' then
update_chapters_menu(menu)
elseif keyword == 'editions' then
update_editions_menu(menu)
elseif keyword == 'audio-devices' then
update_audio_devices_menu(menu)
elseif keyword == 'playlist' then
update_playlist_menu(menu)
elseif keyword == 'profiles' then
update_profiles_menu(menu)
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

Expand Down

0 comments on commit 8a43db3

Please sign in to comment.