History

console.lua: refine the hovered item calculation

Calculate the hovered item from the bottom of the screen where the console is anchored. This is simpler and accurate because it accounts for the fact that the empty line between the log and the input line isn't as tall as a full line, for the exact height of an always visible OSC bottombar, and for the empty top pixels present because the height isn't an integer multiple of the font size.

Guido Cella committed 2 years ago

Commit 00e223c1 parent e1680161 file · +12 −17Browse files

Showing the changes to player/lua/console.lua (1 of 1 file). Show all

Unified · Split

player/lua/console.lua+12−17
@@ -822,29 +822,24 @@ end · 821 unmodified lines
822822 local function determine_hovered_item()
823823 local height = select(2, get_scaled_osd_dimensions())
824824 local y = mp.get_property_native('mouse-pos').y / scale_factor()
825− - global_margins.t * height
826− -- Calculate how many lines could be printed without decreasing them for
827− -- the input line and OSC.
828− local max_lines = height / opts.font_size
829− local clicked_line = math.ceil(y / height * max_lines)
825+ local log_bottom_pos = height * (1 - global_margins.b)
826+ - bottom_left_margin - 1.5 * opts.font_size
830827
831− local offset = first_match_to_print - 1
832− local min_line = 1
833− max_lines = calculate_max_log_lines()
828+ if y > log_bottom_pos then
829+ return
830+ end
834831
832+ local max_lines = calculate_max_log_lines()
835833 -- Subtract 1 line for the position counter.
836− if first_match_to_print > 1 or offset + max_lines < #matches then
837− min_line = 2
838− offset = offset - 1
834+ if #matches > max_lines then
835+ max_lines = max_lines - 1
839836 end
837+ local last = math.min(first_match_to_print - 1 + max_lines, #matches)
840838
841− if #matches < max_lines then
842− clicked_line = clicked_line - (max_lines - #matches)
843− max_lines = #matches
844− end
839+ local hovered_item = last - math.floor((log_bottom_pos - y) / opts.font_size)
845840
846− if clicked_line >= min_line and clicked_line <= max_lines then
847− return offset + clicked_line
841+ if hovered_item >= first_match_to_print then
842+ return hovered_item
848843 end
849844 end
850845

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t