From b0f1830a8eb3751a0a94fed39aa40ad0c242ef1b Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Sat, 12 Oct 2024 14:30:56 +0200 Subject: [PATCH] console.lua: make the wheel scroll selectable items instead of the match This makes scrolling with the wheel more usable because it doesn't matter if the focused item is reset by moving the mouse slightly, and scrolling always begins immediately. This makes the MBTN_MID binding to handle_enter() unnecessary so it is removed. --- player/lua/console.lua | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/player/lua/console.lua b/player/lua/console.lua index 55007b5b1b..62bc8d3d6c 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -859,7 +859,31 @@ local function go_history(new_pos) end -- Go to the specified relative position in the command history (Up, Down) -local function move_history(amount) +local function move_history(amount, is_wheel) + if is_wheel and selectable_items then + local max_lines = calculate_max_log_lines() + + -- Update selected_match only if it's the first or last printed item and + -- there are hidden items. + if (amount > 0 and selected_match == first_match_to_print + and first_match_to_print + max_lines - 2 < #matches) + or (amount < 0 and selected_match == first_match_to_print + max_lines - 2 + and first_match_to_print > 1) then + selected_match = selected_match + amount + end + + if amount > 0 and first_match_to_print < #matches - max_lines + 2 + or amount < 0 and first_match_to_print > 1 then + -- math.min and math.max would only be needed with amounts other than + -- 1 and -1. + first_match_to_print = math.min( + math.max(first_match_to_print + amount, 1), #matches - max_lines + 2) + end + + update() + return + end + if selectable_items then selected_match = selected_match + amount if selected_match > #matches then @@ -1067,12 +1091,7 @@ 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, is_wheel) - if is_wheel and selectable_items then - handle_enter() - return - end - +local function paste(clip) local text = get_clipboard(clip) local before_cur = line:sub(1, cursor - 1) local after_cur = line:sub(cursor) @@ -1558,7 +1577,7 @@ local function get_bindings() { 'shift+del', handle_del }, { 'ins', handle_ins }, { 'shift+ins', function() paste(false) end }, - { 'mbtn_mid', function() paste(false, true) end }, + { 'mbtn_mid', function() paste(false) end }, { 'mbtn_right', function() set_active(false) end }, { 'left', function() prev_char() end }, { 'ctrl+b', function() page_up_or_prev_char() end }, @@ -1566,10 +1585,10 @@ local function get_bindings() { 'ctrl+f', function() page_down_or_next_char() end}, { 'up', function() move_history(-1) end }, { 'ctrl+p', function() move_history(-1) end }, - { 'wheel_up', function() move_history(-1) end }, + { 'wheel_up', function() move_history(-1, true) end }, { 'down', function() move_history(1) end }, { 'ctrl+n', function() move_history(1) end }, - { 'wheel_down', function() move_history(1) end }, + { 'wheel_down', function() move_history(1, true) end }, { 'wheel_left', function() end }, { 'wheel_right', function() end }, { 'ctrl+left', prev_word },