diff options
| author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-01-10 13:32:54 -0800 |
|---|---|---|
| committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-01-10 13:32:54 -0800 |
| commit | e370163b4a8f7e02901ec4f004a965ee5e7e4942 (patch) | |
| tree | 049d2a1726c3d0f42307f890616a28d5d4df7958 /state.lua | |
| parent | 56e08c6897eb3eaa4d70dbe9d3bff7afc1772d05 (diff) | |
| download | mpv-iptv-menu-e370163b4a8f7e02901ec4f004a965ee5e7e4942.tar.gz mpv-iptv-menu-e370163b4a8f7e02901ec4f004a965ee5e7e4942.tar.xz | |
implement margin when scrolling
Diffstat (limited to 'state.lua')
| -rw-r--r-- | state.lua | 18 |
1 files changed, 10 insertions, 8 deletions
@@ -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:: |
