fix(terminal): handle horizontal scrolling in another window (#24828)

This commit is contained in:
zeertzjq 2023-08-21 23:19:12 +08:00 committed by GitHub
parent d0717a7c4e
commit d401b33314
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 23 deletions

View File

@ -1003,13 +1003,13 @@ void ins_mouse(int c)
/// K_MOUSERIGHT - MSCR_RIGHT
/// "curwin" may have been changed to the window that should be scrolled and
/// differ from the window that actually has focus.
static void do_mousescroll(cmdarg_T *cap)
void do_mousescroll(cmdarg_T *cap)
{
bool shift_or_ctrl = mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL);
if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN) {
// Vertical scrolling
if (!(State & MODE_INSERT) && shift_or_ctrl) {
if ((State & MODE_NORMAL) && shift_or_ctrl) {
// whole page up or down
(void)onepage(cap->arg ? FORWARD : BACKWARD, 1);
} else {

View File

@ -79,6 +79,7 @@
#include "nvim/move.h"
#include "nvim/msgpack_rpc/channel_defs.h"
#include "nvim/normal.h"
#include "nvim/ops.h"
#include "nvim/option.h"
#include "nvim/optionstr.h"
#include "nvim/pos.h"
@ -1456,18 +1457,38 @@ static bool send_mouse_event(Terminal *term, int c)
return false;
}
if (c == K_MOUSEDOWN || c == K_MOUSEUP) {
if (c == K_MOUSEUP || c == K_MOUSEDOWN || c == K_MOUSELEFT || c == K_MOUSERIGHT) {
win_T *save_curwin = curwin;
// switch window/buffer to perform the scroll
curwin = mouse_win;
curbuf = curwin->w_buffer;
int direction = c == K_MOUSEDOWN ? MSCR_DOWN : MSCR_UP;
if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) {
scroll_redraw(direction, curwin->w_botline - curwin->w_topline);
} else if (p_mousescroll_vert > 0) {
scroll_redraw(direction, (linenr_T)p_mousescroll_vert);
cmdarg_T cap;
oparg_T oa;
CLEAR_FIELD(cap);
clear_oparg(&oa);
cap.oap = &oa;
switch (cap.cmdchar = c) {
case K_MOUSEUP:
cap.arg = MSCR_UP;
break;
case K_MOUSEDOWN:
cap.arg = MSCR_DOWN;
break;
case K_MOUSELEFT:
cap.arg = MSCR_LEFT;
break;
case K_MOUSERIGHT:
cap.arg = MSCR_RIGHT;
break;
default:
abort();
}
// Call the common mouse scroll function shared with other modes.
do_mousescroll(&cap);
curwin->w_redr_status = true;
curwin = save_curwin;
curbuf = curwin->w_buffer;

View File

@ -287,7 +287,7 @@ describe(':terminal mouse', function()
]])
end)
it("won't lose focus if another window is scrolled", function()
it("scrolling another window keeps focus and respects 'mousescroll'", function()
feed('<ScrollWheelUp><4,0><ScrollWheelUp><4,0>')
screen:expect([[
{7: 21 }line line30 |
@ -308,20 +308,6 @@ describe(':terminal mouse', function()
========== ========== |
{3:-- TERMINAL --} |
]])
end)
it("scrolling another window respects 'mousescroll'", function()
command('set mousescroll=ver:1')
feed('<ScrollWheelUp><4,0>')
screen:expect([[
{7: 26 }line line30 |
{7: 27 }line rows: 5, cols: 25 |
{7: 28 }line rows: 5, cols: 24 |
{7: 29 }line mouse enabled |
{7: 30 }line {1: } |
========== ========== |
{3:-- TERMINAL --} |
]])
command('set mousescroll=ver:10')
feed('<ScrollWheelUp><4,0>')
screen:expect([[
@ -336,6 +322,39 @@ describe(':terminal mouse', function()
command('set mousescroll=ver:0')
feed('<ScrollWheelUp><4,0>')
screen:expect_unchanged()
feed([[<C-\><C-N><C-W>w]])
command('setlocal nowrap')
feed('0<C-V>gg3ly$4p<C-W>wi')
screen:expect([[
{7: 1 }linelinelinelineline line30 |
{7: 2 }linelinelinelineline rows: 5, cols: 25 |
{7: 3 }linelinelinelineline rows: 5, cols: 24 |
{7: 4 }linelinelinelineline mouse enabled |
{7: 5 }linelinelinelineline {1: } |
========== ========== |
{3:-- TERMINAL --} |
]])
feed('<ScrollWheelRight><4,0>')
screen:expect([[
{7: 1 }nelinelineline line30 |
{7: 2 }nelinelineline rows: 5, cols: 25 |
{7: 3 }nelinelineline rows: 5, cols: 24 |
{7: 4 }nelinelineline mouse enabled |
{7: 5 }nelinelineline {1: } |
========== ========== |
{3:-- TERMINAL --} |
]])
command('set mousescroll=hor:4')
feed('<ScrollWheelLeft><4,0>')
screen:expect([[
{7: 1 }nelinelinelineline line30 |
{7: 2 }nelinelinelineline rows: 5, cols: 25 |
{7: 3 }nelinelinelineline rows: 5, cols: 24 |
{7: 4 }nelinelinelineline mouse enabled |
{7: 5 }nelinelinelineline {1: } |
========== ========== |
{3:-- TERMINAL --} |
]])
end)
it('will lose focus if another window is clicked', function()