Skip to content

Commit

Permalink
console.lua: allow clicking selectable items
Browse files Browse the repository at this point in the history
This adds click support for the select menu. Scrolling with the wheel
already worked.

If a custom OSC binds a button to a select.lua script-binding, this lets
users keep using the mouse to select an item.

While the OSC and the select menu are open at the same time, you can no
longer click the OSC's buttons. By using mp.add_key_binding instead of
add_forced_key_binding you could click both, but the console's binding
would be shadowed by MBTN_LEFT bindings in input.conf.
  • Loading branch information
guidocella committed Oct 8, 2024
1 parent fb9ac9b commit e6cdc97
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,11 @@ local function help_command(param)
log_add(output:sub(1, -2))
end

local function unbind_mouse()
mp.remove_key_binding('_console_mouse_move')
mp.remove_key_binding('_console_mbtn_left')
end

-- Run the current command and clear the line (Enter)
local function handle_enter()
if searching_history then
Expand All @@ -800,6 +805,7 @@ local function handle_enter()
cursor = #line + 1
log_buffers[id] = {}
update()
unbind_mouse()
return
end

Expand Down Expand Up @@ -833,6 +839,42 @@ local function handle_enter()
clear()
end

local function bind_mouse()
mp.add_forced_key_binding('MOUSE_MOVE', '_console_mouse_move', function()
local height = mp.get_property_native('osd-height')
if height == 0 then
return
end

local y = mp.get_property_native('mouse-pos').y - global_margins.t * height
-- Calculate how many lines could be printed without decreasing them for
-- the input line and OSC.
local max_lines = height / mp.get_property_native('display-hidpi-scale')
/ opts.font_size
local clicked_line = math.floor(y / height * max_lines + .5)

-- Subtract 1 line for "n hidden items" when necessary.
local offset = first_match_to_print == 1 and 0 or first_match_to_print - 2
max_lines = calculate_max_log_lines()

if #matches < max_lines then
clicked_line = clicked_line - (max_lines - #matches)
max_lines = #matches
elseif offset + max_lines < #matches then
-- Subtract 1 line for "n hidden items".
max_lines = max_lines - 1
end

if selected_match ~= offset + clicked_line
and clicked_line > 0 and clicked_line <= max_lines then
selected_match = offset + clicked_line
update()
end
end)

mp.add_forced_key_binding('MBTN_LEFT', '_console_mbtn_left', handle_enter)
end

-- Go to the specified position in the command history
local function go_history(new_pos)
local old_pos = history_pos
Expand Down Expand Up @@ -932,6 +974,7 @@ local function search_history()
end

update()
bind_mouse()
end

local function page_up_or_prev_char()
Expand Down Expand Up @@ -1665,6 +1708,7 @@ set_active = function (active)
cursor = 1
selectable_items = nil
log_buffers[id] = {}
unbind_mouse()
else
repl_active = false
suggestion_buffer = {}
Expand All @@ -1679,6 +1723,7 @@ set_active = function (active)
cursor = 1
selectable_items = nil
dont_bind_up_down = false
unbind_mouse()
end
collectgarbage()
end
Expand Down Expand Up @@ -1754,6 +1799,7 @@ mp.register_script_message('get-input', function (script_name, args)
for i, item in ipairs(selectable_items) do
matches[i] = { index = i, text = item }
end
bind_mouse()
end

set_active(true)
Expand Down

0 comments on commit e6cdc97

Please sign in to comment.