fix: broken active item indexes in self updating menus

This commit is contained in:
tomasklaen
2022-09-13 13:24:48 +02:00
parent bf012b2485
commit fc8c554f85

View File

@@ -1799,7 +1799,7 @@ end
---@param menu? MenuStack ---@param menu? MenuStack
function Menu:activate_index(index, menu) function Menu:activate_index(index, menu)
menu = menu or self.current menu = menu or self.current
if index and index >= 1 and index <= #menu.items then menu.items[index] = true end if index and index >= 1 and index <= #menu.items then menu.items[index].active = true end
request_render() request_render()
end end
@@ -3734,7 +3734,7 @@ function toggle_menu_with_items(submenu_id)
else open_command_menu({type = 'menu', items = config.menu_items}, submenu_id) end else open_command_menu({type = 'menu', items = config.menu_items}, submenu_id) end
end end
---@param options {type: string; title: string; list_prop: string; list_serializer: fun(name: string, value: any): MenuDataItem[]; active_prop?: string; active_index_serializer: fun(name: string, value: any): integer; on_select: fun(value: any)} ---@param options {type: string; title: string; list_prop: string; list_serializer: fun(name: string, value: any): MenuDataItem[]; active_prop?: string; on_active_prop: fun(name: string, value: any, menu: Menu): integer; on_select: fun(value: any)}
function create_self_updating_menu_opener(options) function create_self_updating_menu_opener(options)
return function() return function()
if Menu:is_open(options.type) then Menu:close() return end if Menu:is_open(options.type) then Menu:close() return end
@@ -3749,8 +3749,8 @@ function create_self_updating_menu_opener(options)
local ignore_initial_active = true local ignore_initial_active = true
local function handle_active_prop_change(name, value) local function handle_active_prop_change(name, value)
if ignore_initial_active then ignore_initial = false if ignore_initial_active then ignore_initial_active = false
else menu:activate_unique_index(options.active_index_serializer(name, value)) end else options.on_active_prop(name, value, menu) end
end end
local initial_items, selected_index = options.list_serializer( local initial_items, selected_index = options.list_serializer(
@@ -4174,13 +4174,12 @@ mp.add_key_binding(nil, 'playlist', create_self_updating_menu_opener({
items[index] = { items[index] = {
title = item_title or (is_url and item.filename or serialize_path(item.filename).basename), title = item_title or (is_url and item.filename or serialize_path(item.filename).basename),
hint = tostring(index), hint = tostring(index),
active = item.current,
value = index, value = index,
} }
end end
return items return items
end, end,
active_prop = 'playlist-pos-1',
active_index_serializer = function(_, playlist_pos) return playlist_pos end,
on_select = function(index) mp.commandv('set', 'playlist-pos-1', tostring(index)) end, on_select = function(index) mp.commandv('set', 'playlist-pos-1', tostring(index)) end,
})) }))
mp.add_key_binding(nil, 'chapters', create_self_updating_menu_opener({ mp.add_key_binding(nil, 'chapters', create_self_updating_menu_opener({
@@ -4190,25 +4189,40 @@ mp.add_key_binding(nil, 'chapters', create_self_updating_menu_opener({
list_serializer = function(_, _) list_serializer = function(_, _)
local items = {} local items = {}
local chapters = get_normalized_chapters() local chapters = get_normalized_chapters()
local active_found = false
for index, chapter in ipairs(chapters) do for index, chapter in ipairs(chapters) do
items[#items + 1] = { local item = {
title = chapter.title or '', title = chapter.title or '',
hint = mp.format_time(chapter.time), hint = mp.format_time(chapter.time),
value = chapter.time, value = chapter.time,
} }
items[#items + 1] = item
if active_found == false then
local is_active = chapter.time >= state.time
if is_active then
item.active = true
active_found = true
end
end
end end
return items return items
end, end,
active_prop = 'playback-time', active_prop = 'playback-time',
active_index_serializer = function(_, playback_time) on_active_prop = function(_, playback_time, menu)
-- Select first chapter from the end with time lower -- Select first chapter from the end with time lower
-- than current playing position. -- than current playing position.
local position = playback_time local position = playback_time
if not position then return nil end if not position then
local items = Menu.current.items menu:deactivate_items()
return
end
local items = menu.current.items
for index = #items, 1, -1 do for index = #items, 1, -1 do
if position >= items[index].value then return index end if position >= items[index].value then
menu:activate_unique_index(index)
return
end
end end
end, end,
on_select = function(time) mp.commandv('seek', tostring(time), 'absolute') end, on_select = function(time) mp.commandv('seek', tostring(time), 'absolute') end,