Skip to content

Commit ed93b10

Browse files
committed
fix(input): use click number of last click for mouse drag
1 parent 7a70e95 commit ed93b10

File tree

2 files changed

+113
-31
lines changed

2 files changed

+113
-31
lines changed

src/nvim/os/input.c

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -306,37 +306,35 @@ static uint8_t check_multiclick(int code, int grid, int row, int col)
306306
static int orig_mouse_row = 0;
307307
static uint64_t orig_mouse_time = 0; // time of previous mouse click
308308

309-
if (code == KE_LEFTRELEASE
310-
|| code == KE_RIGHTRELEASE
311-
|| code == KE_MIDDLERELEASE
312-
|| code == KE_MOUSEDOWN
313-
|| code == KE_MOUSEUP
314-
|| code == KE_MOUSELEFT
315-
|| code == KE_MOUSERIGHT
316-
|| code == KE_MOUSEMOVE) {
309+
if ((code >= KE_MOUSEDOWN && code <= KE_MOUSERIGHT) || code == KE_MOUSEMOVE) {
317310
return 0;
318311
}
319-
uint64_t mouse_time = os_hrtime(); // time of current mouse click (ns)
320-
321-
// compute the time elapsed since the previous mouse click and
322-
// convert p_mouse from ms to ns
323-
uint64_t timediff = mouse_time - orig_mouse_time;
324-
uint64_t mouset = (uint64_t)p_mouset * 1000000;
325-
if (code == orig_mouse_code
326-
&& timediff < mouset
327-
&& orig_num_clicks != 4
328-
&& orig_mouse_grid == grid
329-
&& orig_mouse_col == col
330-
&& orig_mouse_row == row) {
331-
orig_num_clicks++;
332-
} else {
333-
orig_num_clicks = 1;
312+
313+
// For click events the number of clicks is updated.
314+
if (code == KE_LEFTMOUSE || code == KE_RIGHTMOUSE || code == KE_MIDDLEMOUSE) {
315+
uint64_t mouse_time = os_hrtime(); // time of current mouse click (ns)
316+
// compute the time elapsed since the previous mouse click and
317+
// convert p_mouse from ms to ns
318+
uint64_t timediff = mouse_time - orig_mouse_time;
319+
uint64_t mouset = (uint64_t)p_mouset * 1000000;
320+
if (code == orig_mouse_code
321+
&& timediff < mouset
322+
&& orig_num_clicks != 4
323+
&& orig_mouse_grid == grid
324+
&& orig_mouse_col == col
325+
&& orig_mouse_row == row) {
326+
orig_num_clicks++;
327+
} else {
328+
orig_num_clicks = 1;
329+
}
330+
orig_mouse_code = code;
331+
orig_mouse_time = mouse_time;
334332
}
335-
orig_mouse_code = code;
333+
// For drag and release events the number of clicks is kept.
334+
336335
orig_mouse_grid = grid;
337336
orig_mouse_col = col;
338337
orig_mouse_row = row;
339-
orig_mouse_time = mouse_time;
340338

341339
uint8_t modifiers = 0;
342340
if (orig_num_clicks == 2) {

test/functional/ui/mouse_spec.lua

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,37 +648,121 @@ describe('ui/mouse/input', function()
648648
]]}
649649
end)
650650

651-
it('two clicks will select the word and enter VISUAL', function()
652-
feed('<LeftMouse><2,2><LeftMouse><2,2>')
651+
it('two clicks will enter VISUAL and dragging selects words', function()
652+
feed('<LeftMouse><2,2>')
653+
feed('<LeftRelease><2,2>')
654+
feed('<LeftMouse><2,2>')
653655
screen:expect([[
654656
testing |
655657
mouse |
656658
{1:suppor}^t and selection |
657659
{0:~ }|
658660
{2:-- VISUAL --} |
659661
]])
662+
feed('<LeftDrag><0,1>')
663+
screen:expect([[
664+
testing |
665+
^m{1:ouse} |
666+
{1:support} and selection |
667+
{0:~ }|
668+
{2:-- VISUAL --} |
669+
]])
670+
feed('<LeftDrag><4,0>')
671+
screen:expect([[
672+
^t{1:esting} |
673+
{1:mouse} |
674+
{1:support} and selection |
675+
{0:~ }|
676+
{2:-- VISUAL --} |
677+
]])
678+
feed('<LeftDrag><14,2>')
679+
screen:expect([[
680+
testing |
681+
mouse |
682+
{1:support and selectio}^n |
683+
{0:~ }|
684+
{2:-- VISUAL --} |
685+
]])
660686
end)
661687

662-
it('three clicks will select the line and enter VISUAL LINE', function()
663-
feed('<LeftMouse><2,2><LeftMouse><2,2><LeftMouse><2,2>')
688+
it('three clicks will enter VISUAL LINE and dragging selects lines', function()
689+
feed('<LeftMouse><2,2>')
690+
feed('<LeftRelease><2,2>')
691+
feed('<LeftMouse><2,2>')
692+
feed('<LeftRelease><2,2>')
693+
feed('<LeftMouse><2,2>')
664694
screen:expect([[
665695
testing |
666696
mouse |
667697
{1:su}^p{1:port and selection} |
668698
{0:~ }|
669699
{2:-- VISUAL LINE --} |
670700
]])
701+
feed('<LeftDrag><0,1>')
702+
screen:expect([[
703+
testing |
704+
^m{1:ouse} |
705+
{1:support and selection} |
706+
{0:~ }|
707+
{2:-- VISUAL LINE --} |
708+
]])
709+
feed('<LeftDrag><4,0>')
710+
screen:expect([[
711+
{1:test}^i{1:ng} |
712+
{1:mouse} |
713+
{1:support and selection} |
714+
{0:~ }|
715+
{2:-- VISUAL LINE --} |
716+
]])
717+
feed('<LeftDrag><14,2>')
718+
screen:expect([[
719+
testing |
720+
mouse |
721+
{1:support and se}^l{1:ection} |
722+
{0:~ }|
723+
{2:-- VISUAL LINE --} |
724+
]])
671725
end)
672726

673-
it('four clicks will enter VISUAL BLOCK', function()
674-
feed('<LeftMouse><2,2><LeftMouse><2,2><LeftMouse><2,2><LeftMouse><2,2>')
727+
it('four clicks will enter VISUAL BLOCK and dragging selects blockwise', function()
728+
feed('<LeftMouse><2,2>')
729+
feed('<LeftRelease><2,2>')
730+
feed('<LeftMouse><2,2>')
731+
feed('<LeftRelease><2,2>')
732+
feed('<LeftMouse><2,2>')
733+
feed('<LeftRelease><2,2>')
734+
feed('<LeftMouse><2,2>')
675735
screen:expect([[
676736
testing |
677737
mouse |
678738
su^pport and selection |
679739
{0:~ }|
680740
{2:-- VISUAL BLOCK --} |
681741
]])
742+
feed('<LeftDrag><0,1>')
743+
screen:expect([[
744+
testing |
745+
^m{1:ou}se |
746+
{1:sup}port and selection |
747+
{0:~ }|
748+
{2:-- VISUAL BLOCK --} |
749+
]])
750+
feed('<LeftDrag><4,0>')
751+
screen:expect([[
752+
te{1:st}^ing |
753+
mo{1:use} |
754+
su{1:ppo}rt and selection |
755+
{0:~ }|
756+
{2:-- VISUAL BLOCK --} |
757+
]])
758+
feed('<LeftDrag><14,2>')
759+
screen:expect([[
760+
testing |
761+
mouse |
762+
su{1:pport and se}^lection |
763+
{0:~ }|
764+
{2:-- VISUAL BLOCK --} |
765+
]])
682766
end)
683767

684768
it('right click extends visual selection to the clicked location', function()

0 commit comments

Comments
 (0)