diff options
| author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-01-20 01:19:36 -0800 |
|---|---|---|
| committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-01-20 01:26:08 -0800 |
| commit | 50021f8d850b81e7ec27b0e048dafb693fb49a6c (patch) | |
| tree | aba9c1ac75903e86640633efbf2e378b5cccef1c | |
| parent | 0793df89e4a018c6abe662181a564ec137b2487a (diff) | |
| download | mpv-iptv-menu-50021f8d850b81e7ec27b0e048dafb693fb49a6c.tar.gz mpv-iptv-menu-50021f8d850b81e7ec27b0e048dafb693fb49a6c.tar.xz | |
support clicking in scrollbar to move cursor/view
| -rw-r--r-- | main.lua | 62 | ||||
| -rw-r--r-- | osd.lua | 23 |
2 files changed, 73 insertions, 12 deletions
@@ -1044,18 +1044,24 @@ local function process_mouse_click(ev) return end - local line = osd:get_mouse_line(mouse_state.pos) - if line then - line = line - (osd.lines - osd:menu_lines(state)) + local area + local val = osd:mouse_scrollbar_ratio(mouse_state.pos) + if val then + area = 'scrollbar' + else + local line = osd:mouse_menu_line(mouse_state.pos) + if line then + area = 'menu' + val = line - (osd.lines - osd:menu_lines(state)) + end end - local key, dbl = handle_mouse_click(ev, line) - return key, dbl, line + local key, dbl = handle_mouse_click(ev, area == 'menu' and val or nil) + return key, dbl, area, val end -local function mouse_click_left(ev) - local key, dbl, line = process_mouse_click(ev) - if not key or not line or line == 0 then +local function mouse_click_left_menu(dbl, line) + if line == 0 then return end @@ -1091,9 +1097,32 @@ local function mouse_click_left(ev) end end -local function mouse_click_right(ev) - local key, dbl, line = process_mouse_click(ev) - if not key or not dbl or not line or line < 1 then +local function mouse_click_left_scrollbar(dbl, ratio) + if not dbl then + return + end + + -- set_cursor handles out-of-bounds moves (when ratio == 1) + set_cursor( + math.floor(ratio * #state:menu().options) + 1, + {centre = true}) +end + +local function mouse_click_left(ev) + local key, dbl, area, val = process_mouse_click(ev) + if not key then + return + end + + if area == 'menu' then + return mouse_click_left_menu(dbl, val) + elseif area == 'scrollbar' then + return mouse_click_left_scrollbar(dbl, val) + end +end + +local function mouse_click_right_menu(dbl, line) + if not dbl or line < 1 then return end @@ -1106,6 +1135,17 @@ local function mouse_click_right(ev) open_option_info(menu.options[pos]) end +local function mouse_click_right(ev) + local key, dbl, area, val = process_mouse_click(ev) + if not key then + return + end + + if area == 'menu' then + return mouse_click_right_menu(dbl, val) + end +end + local function reload_data() if state.depth > 1 then osd:flash_error('Can only reload data from root menu') @@ -419,6 +419,7 @@ function mt:draw_scrollbar(state) local opts = #menu.options local lines = self:menu_lines(state) if opts <= lines then + self.scrollbar_coords = nil return end @@ -427,6 +428,12 @@ function mt:draw_scrollbar(state) local w = self.padding - 4 local hh = math.max(config.font_size / 2, h * lines / opts) local pos = (h - hh) * (menu.view_top - 1) / (opts - lines) + self.scrollbar_coords = { + x1 = 2, + y1 = top, + x2 = 2 + w, + y2 = top + h, + } return ( -- bg @@ -603,7 +610,7 @@ function mt:is_hidden() return self.fg.hidden end -function mt:get_mouse_line(mpos) +function mt:mouse_menu_line(mpos) local x = mpos.x / self.scale local w = self.width / self.scale if x < self.padding or x > w - self.padding then @@ -618,6 +625,20 @@ function mt:get_mouse_line(mpos) return line end +function mt:mouse_scrollbar_ratio(mpos) + local x = mpos.x / self.scale + local y = mpos.y / self.scale + local coords = self.scrollbar_coords + + if not coords or x < coords.x1 or x > coords.x2 or + y < coords.y1 or y > coords.y2 then + return + end + + local ratio = (y - coords.y1) / (coords.y2 - coords.y1) + return math.max(0, math.min(1, ratio)) +end + function mt:mouse_over_button_area(mpos) if not mpos.hover then return false |
