Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console.lua: allow clicking selectable items #15016

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions DOCS/man/mpv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ Command + f (macOS only)
Toggle fullscreen (see also ``--fs``).

(The following keybindings open a selector in the console that lets you choose
from a list of items by typing part of the desired item and/or by navigating
them with keybindings: ``Down`` and ``Ctrl+n`` go down, ``Up`` and ``Ctrl+p`` go
up, ``Page down`` and ``Ctrl+f`` scroll down one page, and ``Page up`` and
``Ctrl+b`` scroll up one page.)
from a list of items by typing part of the desired item, by clicking the desired
item, or by navigating them with keybindings: ``Down`` and ``Ctrl+n`` go down,
``Up`` and ``Ctrl+p`` go up, ``Page down`` and ``Ctrl+f`` scroll down one page,
and ``Page up`` and ``Ctrl+b`` scroll up one page.)

g-p
Select a playlist entry.
Expand Down
59 changes: 57 additions & 2 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,46 @@ local function handle_enter()
clear()
end

local function highlight_hovered_line()
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

local function bind_mouse()
mp.add_forced_key_binding('MOUSE_MOVE', '_console_mouse_move', highlight_hovered_line)
mp.add_forced_key_binding('MBTN_LEFT', '_console_mbtn_left', function()
highlight_hovered_line()
handle_enter()
end)
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 +978,7 @@ local function search_history()
end

update()
bind_mouse()
end

local function page_up_or_prev_char()
Expand Down Expand Up @@ -1082,7 +1129,12 @@ end

-- Paste text from the window-system's clipboard. 'clip' determines whether the
-- clipboard or the primary selection buffer is used (on X11 and Wayland only.)
local function paste(clip)
local function paste(clip, is_wheel)
if is_wheel and selectable_items then
handle_enter()
return
end

local text = get_clipboard(clip)
local before_cur = line:sub(1, cursor - 1)
local after_cur = line:sub(cursor)
Expand Down Expand Up @@ -1567,7 +1619,7 @@ local function get_bindings()
{ 'shift+del', handle_del },
{ 'ins', handle_ins },
{ 'shift+ins', function() paste(false) end },
{ 'mbtn_mid', function() paste(false) end },
{ 'mbtn_mid', function() paste(false, true) end },
{ 'left', function() prev_char() end },
{ 'ctrl+b', function() page_up_or_prev_char() end },
{ 'right', function() next_char() end },
Expand Down Expand Up @@ -1665,6 +1717,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 +1732,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 +1808,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