From d5b6d72a8a9375a32378033c4883a933355b63f5 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 13 May 2025 01:50:29 -0700 Subject: centre menu options when using go-to shortcut --- main.lua | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index 681f7bb..086dd4b 100644 --- a/main.lua +++ b/main.lua @@ -318,13 +318,15 @@ local function update_osd() osd_bg:update() end -local function set_cursor(pos, move_view) +local function set_cursor(pos, opts) local menu = menus[depth] local lines = osd_menu_lines() local pos = math.max(1, math.min(pos, #menu.options)) local top = menu.view_top - if move_view then + if opts and opts.centre then + top = pos - math.floor((osd_menu_lines() - 1) / 2) + elseif opts and opts.keep_offset then top = top + pos - menu.cursor end @@ -359,11 +361,20 @@ local function cursor_end() end local function cursor_page_up() - set_cursor(menus[depth].cursor - osd_menu_lines(), true) + set_cursor(menus[depth].cursor - osd_menu_lines(), {keep_offset=true}) end local function cursor_page_down() - set_cursor(menus[depth].cursor + osd_menu_lines(), true) + set_cursor(menus[depth].cursor + osd_menu_lines(), {keep_offset=true}) +end + +local function cursor_to_object(id) + for i, v in ipairs(menus[depth].options) do + if v.id == id then + set_cursor(i, {centre=true}) + return + end + end end local function prev_menu() @@ -475,6 +486,8 @@ local function push_group_menu(group) push_menu({ options=group_menu_options(group), title=group.name, + type='group', + group_id=group.id, }) update_osd() end @@ -530,21 +543,22 @@ end local function goto_option() local menu = menus[depth] local opt = menu.options[menu.cursor] - if not opt or not opt.path then + if not opt then return end - for i = 1, #opt.path do - push_group_menu(opt.path[i]) + if menu.type == 'search' or menu.group_id == 'favourites' then + depth = depth - 1 + end - local target = i == #opt.path and opt or opt.path[i+1] - for i, v in ipairs(menus[depth].options) do - if v.id == target.id then - set_cursor(i, true) - break - end + if opt.path then + for i = 1, #opt.path do + cursor_to_object(opt.path[i].id) + push_group_menu(opt.path[i]) end end + cursor_to_object(opt.id) + update_osd() end local function search_menu_options_build(options, t, path) -- cgit v1.2.3-70-g09d2