Skip to content

Commit 8bf5548

Browse files
guidocellakasper93
authored andcommitted
console.lua: allow clicking selectable items
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.
1 parent 6871304 commit 8bf5548

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

DOCS/man/mpv.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,10 @@ Command + f (macOS only)
274274
Toggle fullscreen (see also ``--fs``).
275275

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

282282
g-p
283283
Select a playlist entry.

player/lua/console.lua

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,11 @@ local function help_command(param)
791791
log_add(output:sub(1, -2))
792792
end
793793

794+
local function unbind_mouse()
795+
mp.remove_key_binding('_console_mouse_move')
796+
mp.remove_key_binding('_console_mbtn_left')
797+
end
798+
794799
-- Run the current command and clear the line (Enter)
795800
local function handle_enter()
796801
if searching_history then
@@ -800,6 +805,7 @@ local function handle_enter()
800805
cursor = #line + 1
801806
log_buffers[id] = {}
802807
update()
808+
unbind_mouse()
803809
return
804810
end
805811

@@ -833,6 +839,46 @@ local function handle_enter()
833839
clear()
834840
end
835841

842+
local function highlight_hovered_line()
843+
local height = mp.get_property_native('osd-height')
844+
if height == 0 then
845+
return
846+
end
847+
848+
local y = mp.get_property_native('mouse-pos').y - global_margins.t * height
849+
-- Calculate how many lines could be printed without decreasing them for
850+
-- the input line and OSC.
851+
local max_lines = height / mp.get_property_native('display-hidpi-scale')
852+
/ opts.font_size
853+
local clicked_line = math.floor(y / height * max_lines + .5)
854+
855+
-- Subtract 1 line for "n hidden items" when necessary.
856+
local offset = first_match_to_print == 1 and 0 or first_match_to_print - 2
857+
max_lines = calculate_max_log_lines()
858+
859+
if #matches < max_lines then
860+
clicked_line = clicked_line - (max_lines - #matches)
861+
max_lines = #matches
862+
elseif offset + max_lines < #matches then
863+
-- Subtract 1 line for "n hidden items".
864+
max_lines = max_lines - 1
865+
end
866+
867+
if selected_match ~= offset + clicked_line
868+
and clicked_line > 0 and clicked_line <= max_lines then
869+
selected_match = offset + clicked_line
870+
update()
871+
end
872+
end
873+
874+
local function bind_mouse()
875+
mp.add_forced_key_binding('MOUSE_MOVE', '_console_mouse_move', highlight_hovered_line)
876+
mp.add_forced_key_binding('MBTN_LEFT', '_console_mbtn_left', function()
877+
highlight_hovered_line()
878+
handle_enter()
879+
end)
880+
end
881+
836882
-- Go to the specified position in the command history
837883
local function go_history(new_pos)
838884
local old_pos = history_pos
@@ -932,6 +978,7 @@ local function search_history()
932978
end
933979

934980
update()
981+
bind_mouse()
935982
end
936983

937984
local function page_up_or_prev_char()
@@ -1082,7 +1129,12 @@ end
10821129

10831130
-- Paste text from the window-system's clipboard. 'clip' determines whether the
10841131
-- clipboard or the primary selection buffer is used (on X11 and Wayland only.)
1085-
local function paste(clip)
1132+
local function paste(clip, is_wheel)
1133+
if is_wheel and selectable_items then
1134+
handle_enter()
1135+
return
1136+
end
1137+
10861138
local text = get_clipboard(clip)
10871139
local before_cur = line:sub(1, cursor - 1)
10881140
local after_cur = line:sub(cursor)
@@ -1567,7 +1619,7 @@ local function get_bindings()
15671619
{ 'shift+del', handle_del },
15681620
{ 'ins', handle_ins },
15691621
{ 'shift+ins', function() paste(false) end },
1570-
{ 'mbtn_mid', function() paste(false) end },
1622+
{ 'mbtn_mid', function() paste(false, true) end },
15711623
{ 'left', function() prev_char() end },
15721624
{ 'ctrl+b', function() page_up_or_prev_char() end },
15731625
{ 'right', function() next_char() end },
@@ -1665,6 +1717,7 @@ set_active = function (active)
16651717
cursor = 1
16661718
selectable_items = nil
16671719
log_buffers[id] = {}
1720+
unbind_mouse()
16681721
else
16691722
repl_active = false
16701723
suggestion_buffer = {}
@@ -1679,6 +1732,7 @@ set_active = function (active)
16791732
cursor = 1
16801733
selectable_items = nil
16811734
dont_bind_up_down = false
1735+
unbind_mouse()
16821736
end
16831737
collectgarbage()
16841738
end
@@ -1754,6 +1808,7 @@ mp.register_script_message('get-input', function (script_name, args)
17541808
for i, item in ipairs(selectable_items) do
17551809
matches[i] = { index = i, text = item }
17561810
end
1811+
bind_mouse()
17571812
end
17581813

17591814
set_active(true)

0 commit comments

Comments
 (0)