feat(api): add forward and back mouse buttons

This commit is contained in:
Amanda Graven 2023-11-28 21:05:33 +01:00
parent 3a4aa3fc58
commit 428edcde70
No known key found for this signature in database
GPG Key ID: F747582C5608F4CB
6 changed files with 31 additions and 4 deletions

View File

@ -1145,7 +1145,7 @@ nvim_input_mouse({button}, {action}, {modifier}, {grid}, {row}, {col})
Parameters: ~ Parameters: ~
• {button} Mouse button: one of "left", "right", "middle", "wheel", • {button} Mouse button: one of "left", "right", "middle", "wheel",
"move". "move", "x1", "x2".
• {action} For ordinary buttons, one of "press", "drag", "release". • {action} For ordinary buttons, one of "press", "drag", "release".
For the wheel, one of "up", "down", "left", "right". For the wheel, one of "up", "down", "left", "right".
Ignored for "move". Ignored for "move".

View File

@ -268,6 +268,8 @@ The following new APIs and features were added.
• 'completeopt' option supports "popup" flags to show extra information in • 'completeopt' option supports "popup" flags to show extra information in
in floating window. in floating window.
• Added `x1` and `x2` mouse buttons as possible arguments to |nvim_input_mouse()|
============================================================================== ==============================================================================
CHANGED FEATURES *news-changed* CHANGED FEATURES *news-changed*

View File

@ -1375,7 +1375,7 @@ function vim.api.nvim_input(keys) end
--- processed soon by the event loop. --- processed soon by the event loop.
--- ---
--- @param button string Mouse button: one of "left", "right", "middle", "wheel", --- @param button string Mouse button: one of "left", "right", "middle", "wheel",
--- "move". --- "move", "x1", "x2".
--- @param action string For ordinary buttons, one of "press", "drag", "release". --- @param action string For ordinary buttons, one of "press", "drag", "release".
--- For the wheel, one of "up", "down", "left", "right". --- For the wheel, one of "up", "down", "left", "right".
--- Ignored for "move". --- Ignored for "move".

View File

@ -345,7 +345,8 @@ Integer nvim_input(String keys)
/// mouse input in a GUI. The deprecated pseudokey form /// mouse input in a GUI. The deprecated pseudokey form
/// ("<LeftMouse><col,row>") of |nvim_input()| has the same limitation. /// ("<LeftMouse><col,row>") of |nvim_input()| has the same limitation.
/// ///
/// @param button Mouse button: one of "left", "right", "middle", "wheel", "move". /// @param button Mouse button: one of "left", "right", "middle", "wheel", "move",
/// "x1", "x2".
/// @param action For ordinary buttons, one of "press", "drag", "release". /// @param action For ordinary buttons, one of "press", "drag", "release".
/// For the wheel, one of "up", "down", "left", "right". Ignored for "move". /// For the wheel, one of "up", "down", "left", "right". Ignored for "move".
/// @param modifier String of modifiers each represented by a single char. /// @param modifier String of modifiers each represented by a single char.
@ -376,6 +377,10 @@ void nvim_input_mouse(String button, String action, String modifier, Integer gri
code = KE_RIGHTMOUSE; code = KE_RIGHTMOUSE;
} else if (strequal(button.data, "wheel")) { } else if (strequal(button.data, "wheel")) {
code = KE_MOUSEDOWN; code = KE_MOUSEDOWN;
} else if (strequal(button.data, "x1")) {
code = KE_X1MOUSE;
} else if (strequal(button.data, "x2")) {
code = KE_X2MOUSE;
} else if (strequal(button.data, "move")) { } else if (strequal(button.data, "move")) {
code = KE_MOUSEMOVE; code = KE_MOUSEMOVE;
} else { } else {

View File

@ -311,7 +311,8 @@ static uint8_t check_multiclick(int code, int grid, int row, int col)
} }
// For click events the number of clicks is updated. // For click events the number of clicks is updated.
if (code == KE_LEFTMOUSE || code == KE_RIGHTMOUSE || code == KE_MIDDLEMOUSE) { if (code == KE_LEFTMOUSE || code == KE_RIGHTMOUSE || code == KE_MIDDLEMOUSE
|| code == KE_X1MOUSE || code == KE_X2MOUSE) {
uint64_t mouse_time = os_hrtime(); // time of current mouse click (ns) uint64_t mouse_time = os_hrtime(); // time of current mouse click (ns)
// compute the time elapsed since the previous mouse click and // compute the time elapsed since the previous mouse click and
// convert p_mouse from ms to ns // convert p_mouse from ms to ns

View File

@ -758,6 +758,25 @@ describe('ui/mouse/input', function()
feed('<cr>') feed('<cr>')
end) end)
it('x1 and x2 can be triggered by api', function()
meths.set_var('x1_pressed', 0)
meths.set_var('x1_released', 0)
meths.set_var('x2_pressed', 0)
meths.set_var('x2_released', 0)
command('nnoremap <X1Mouse> <Cmd>let g:x1_pressed += 1<CR>')
command('nnoremap <X1Release> <Cmd>let g:x1_released += 1<CR>')
command('nnoremap <X2Mouse> <Cmd>let g:x2_pressed += 1<CR>')
command('nnoremap <X2Release> <Cmd>let g:x2_released += 1<CR>')
meths.input_mouse('x1', 'press', '', 0, 0, 0)
meths.input_mouse('x1', 'release', '', 0, 0, 0)
meths.input_mouse('x2', 'press', '', 0, 0, 0)
meths.input_mouse('x2', 'release', '', 0, 0, 0)
eq(1, meths.get_var('x1_pressed'), 'x1 pressed once')
eq(1, meths.get_var('x1_released'), 'x1 released once')
eq(1, meths.get_var('x2_pressed'), 'x2 pressed once')
eq(1, meths.get_var('x2_released'), 'x2 released once')
end)
it('dragging vertical separator', function() it('dragging vertical separator', function()
screen:try_resize(45, 5) screen:try_resize(45, 5)
command('setlocal nowrap') command('setlocal nowrap')