From e370163b4a8f7e02901ec4f004a965ee5e7e4942 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sat, 10 Jan 2026 13:32:54 -0800 Subject: implement margin when scrolling --- config.lua | 2 ++ main.lua | 8 ++++---- state.lua | 18 ++++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/config.lua b/config.lua index 9dc9bff..bd0819c 100644 --- a/config.lua +++ b/config.lua @@ -7,6 +7,8 @@ local config = {} -- font size is in units of osd height, which is scaled to 720 config.font_size = 20 +config.scroll_margin = 4 + config.bg_alpha = '44' config.colours = { title = '999999', diff --git a/main.lua b/main.lua index 12c7f4d..4014d16 100644 --- a/main.lua +++ b/main.lua @@ -101,11 +101,11 @@ local function set_cursor(pos, opts) end local function cursor_up() - set_cursor(state:menu().cursor - 1) + set_cursor(state:menu().cursor - 1, {margin = config.scroll_margin}) end local function cursor_down() - set_cursor(state:menu().cursor + 1) + set_cursor(state:menu().cursor + 1, {margin = config.scroll_margin}) end local function cursor_start() @@ -163,11 +163,11 @@ local function move_option(pos, opts) end local function move_option_up() - move_option(state:menu().cursor - 1) + move_option(state:menu().cursor - 1, {margin = config.scroll_margin}) end local function move_option_down() - move_option(state:menu().cursor + 1) + move_option(state:menu().cursor + 1, {margin = config.scroll_margin}) end local function move_option_start() diff --git a/state.lua b/state.lua index 8bf4ddd..41c317c 100644 --- a/state.lua +++ b/state.lua @@ -85,8 +85,9 @@ end function menu_mt:set_cursor(pos, lines, opts) local pos = math.max(1, math.min(pos, #self.options)) local top = self.view_top + local margin = 0 - if not lines then + if not lines or lines < 2 then top = pos goto update end @@ -95,15 +96,16 @@ function menu_mt:set_cursor(pos, lines, opts) top = pos - math.floor((lines - 1) / 2) elseif opts and opts.keep_offset then top = top + pos - self.cursor + elseif opts and opts.margin then + margin = math.max(0, math.min( + opts.margin, + math.floor((lines - 1) / 2), + self.cursor - self.view_top, + self.view_top + lines - 1 - self.cursor)) end - -- move view to keep selected option visible - if pos < top then - top = pos - elseif pos > top + lines - 1 then - top = pos - lines + 1 - end - + -- keep selected option visible and view in bounds + top = math.max(pos - lines + 1 + margin, math.min(top, pos - margin)) top = math.max(1, math.min(top, #self.options - lines + 1)) ::update:: -- cgit v1.2.3-70-g09d2