console.lua: improve hovered line calculation

- Don't do anything if clicking on the top counter, the input line or
  the new line between the items and the input line

- Don't scroll upwards when hovering the counter

- Don't redraw before clicking to avoid quickly updating the overlay
  before closing it with touch input, where the item you touch can be
  different from the one that was highlighted
This commit is contained in:
Guido Cella 2024-10-14 20:01:37 +02:00 committed by Kacper Michajłow
parent b0f1830a8e
commit 0f74b1bc2d
1 changed files with 18 additions and 8 deletions

View File

@ -781,7 +781,7 @@ local function handle_enter()
clear()
end
local function highlight_hovered_line()
local function determine_hovered_item()
local height = mp.get_property_native('osd-height')
if height == 0 then
return
@ -795,10 +795,12 @@ local function highlight_hovered_line()
local clicked_line = math.floor(y / height * max_lines + .5)
local offset = first_match_to_print - 1
local min_line = 1
max_lines = calculate_max_log_lines()
-- Subtract 1 line for the position counter.
if first_match_to_print > 1 or offset + max_lines < #matches then
min_line = 2
offset = offset - 1
end
@ -807,18 +809,26 @@ local function highlight_hovered_line()
max_lines = #matches
end
if selected_match ~= offset + clicked_line
and clicked_line > 0 and clicked_line <= max_lines then
selected_match = offset + clicked_line
update()
if clicked_line >= min_line and clicked_line <= max_lines then
return offset + clicked_line
end
end
local function bind_mouse()
mp.add_forced_key_binding('MOUSE_MOVE', '_console_mouse_move', highlight_hovered_line)
mp.add_forced_key_binding('MOUSE_MOVE', '_console_mouse_move', function()
local item = determine_hovered_item()
if item and item ~= selected_match then
selected_match = item
update()
end
end)
mp.add_forced_key_binding('MBTN_LEFT', '_console_mbtn_left', function()
highlight_hovered_line()
local item = determine_hovered_item()
if item then
selected_match = item
handle_enter()
end
end)
end