From 3010ebfef5520a7e148235426c252f77a7141459 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 12 Feb 2020 18:57:23 +0530 Subject: [PATCH] More mouse interaction tests --- kitty/mouse.c | 11 ++++++++-- kitty_tests/__init__.py | 2 +- kitty_tests/mouse.py | 46 +++++++++++++++++++++++++++++++++++------ 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/kitty/mouse.c b/kitty/mouse.c index 84f5a130e..cfc281c0c 100644 --- a/kitty/mouse.c +++ b/kitty/mouse.c @@ -349,6 +349,11 @@ distance(double x1, double y1, double x2, double y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } +static inline void +clear_click_queue(Window *w) { + w->click_queue.length = 0; +} + HANDLER(add_click) { ClickQueue *q = &w->click_queue; if (q->length == CLICK_QUEUE_SZ) { memmove(q->clicks, q->clicks + 1, sizeof(Click) * (CLICK_QUEUE_SZ - 1)); q->length--; } @@ -683,12 +688,14 @@ test_encode_mouse(PyObject *self UNUSED, PyObject *args) { static PyObject* send_mock_mouse_event_to_window(PyObject *self UNUSED, PyObject *args) { PyObject *capsule; - int button, modifiers, is_release; + int button, modifiers, is_release, clear_clicks; unsigned int x, y; - if (!PyArg_ParseTuple(args, "O!iipII", &PyCapsule_Type, &capsule, &button, &modifiers, &is_release, &x, &y)) return NULL; + if (!PyArg_ParseTuple(args, "O!iipIIp", &PyCapsule_Type, &capsule, &button, &modifiers, &is_release, &x, &y, &clear_clicks)) return NULL; Window *w = PyCapsule_GetPointer(capsule, "Window"); if (!w) return NULL; + if (clear_clicks) clear_click_queue(w); bool mouse_cell_changed = x != w->mouse_pos.cell_x || y != w->mouse_pos.cell_y; + w->mouse_pos.x = 10 * x; w->mouse_pos.y = 20 * y; w->mouse_pos.cell_x = x; w->mouse_pos.cell_y = y; if (button < 0) { if (button == -2) do_drag_scroll(w, true); diff --git a/kitty_tests/__init__.py b/kitty_tests/__init__.py index 9ff9a4b2d..45809adfb 100644 --- a/kitty_tests/__init__.py +++ b/kitty_tests/__init__.py @@ -76,7 +76,7 @@ class BaseTest(TestCase): def create_screen(self, cols=5, lines=5, scrollback=5, cell_width=10, cell_height=20, options=None): if options is None: - options = {'scrollback_pager_history_size': 1024} + options = {'scrollback_pager_history_size': 1024, 'click_interval': 0.5} options = Options(merge_configs(defaults._asdict(), options)) set_options(options) c = Callbacks() diff --git a/kitty_tests/mouse.py b/kitty_tests/mouse.py index 8692e6eba..2b009944a 100644 --- a/kitty_tests/mouse.py +++ b/kitty_tests/mouse.py @@ -11,8 +11,8 @@ from kitty.fast_data_types import ( from . import BaseTest -def send_mouse_event(window, button=-1, modifiers=0, is_release=False, x=0, y=0): - send_mock_mouse_event_to_window(window, button, modifiers, is_release, x, y) +def send_mouse_event(window, button=-1, modifiers=0, is_release=False, x=0, y=0, clear_click_queue=False): + send_mock_mouse_event_to_window(window, button, modifiers, is_release, x, y, clear_click_queue) class TestMouse(BaseTest): @@ -23,7 +23,7 @@ class TestMouse(BaseTest): ev = partial(send_mouse_event, w) def sel(): - return s.text_for_selection()[0] + return ''.join(s.text_for_selection()) def init(): s.reset() @@ -33,9 +33,43 @@ class TestMouse(BaseTest): s.draw('fghij') s.draw('klmno') + def press(x=0, y=0): + ev(GLFW_MOUSE_BUTTON_LEFT, x=x, y=y) + + def release(x=0, y=0): + ev(GLFW_MOUSE_BUTTON_LEFT, x=x, y=y, is_release=True, clear_click_queue=True) + + def move(x=0, y=0): + ev(x=x, y=y) + + def multi_click(x=0, y=0, count=2): + while count > 0: + count -= 1 + ev(GLFW_MOUSE_BUTTON_LEFT, x=x, y=y) + + # Simple, click, move, release test init() - ev(GLFW_MOUSE_BUTTON_LEFT) - ev(x=3) + press() + move(x=3) self.ae(sel(), '1234') - ev(GLFW_MOUSE_BUTTON_LEFT, x=3, is_release=True) + release(x=3) self.ae(sel(), '1234') + + # Multi-line click release + init() + press(1, 1), release(3, 2) + self.ae(sel(), '7890abcd') + press(3, 4), release(2, 2) + self.ae(sel(), 'cdefghijklmn') + + # Word select with drag + s.reset() + s.draw('ab cd') + s.draw(' f gh') + s.draw(' stuv') + multi_click(x=1) + self.ae(sel(), 'ab') + move(3) + self.ae(sel(), 'ab cd') + release(3, 1) + self.ae(sel(), 'ab cd f gh')