From e97de84668495921cfb686a37f2ca049b03251ef Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 21 Jul 2024 22:18:43 +0530 Subject: [PATCH] Convenience method to get the mouse position for a window Fix #7652 --- kitty/fast_data_types.pyi | 7 +++++++ kitty/state.c | 11 +++++++++++ kitty/window.py | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 23f7c3899..ed0152613 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -1685,3 +1685,10 @@ def monotonic() -> float: ... def timed_debug_print(x: str) -> None: ... def opengl_version_string() -> str: ... def systemd_move_pid_into_new_scope(pid: int, scope_name: str, description: str) -> str: ... + +class MousePosition(TypedDict): + cell_x: int + cell_y: int + in_left_half_of_cell: bool + +def get_mouse_data_for_window(os_window_id: int, tab_id: int, window_id: int) -> Optional[MousePosition]: ... diff --git a/kitty/state.c b/kitty/state.c index 16c1d1306..38bfeffca 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -1376,11 +1376,22 @@ os_window_focus_counters(PyObject *self UNUSED, PyObject *args UNUSED) { return ans; } +static PyObject* +get_mouse_data_for_window(PyObject *self UNUSED, PyObject *args) { + id_type os_window_id, tab_id, window_id; + PA("KKK", &os_window_id, &tab_id, &window_id); + WITH_WINDOW(os_window_id, tab_id, window_id) + return Py_BuildValue("{sI sI sO}", "cell_x", window->mouse_pos.cell_x, "cell_y", window->mouse_pos.cell_y, "in_left_half_of_cell", window->mouse_pos.in_left_half_of_cell); + END_WITH_WINDOW + Py_RETURN_NONE; +} + #define M(name, arg_type) {#name, (PyCFunction)name, arg_type, NULL} #define MW(name, arg_type) {#name, (PyCFunction)py##name, arg_type, NULL} static PyMethodDef module_methods[] = { M(os_window_focus_counters, METH_NOARGS), + M(get_mouse_data_for_window, METH_VARARGS), MW(update_pointer_shape, METH_VARARGS), MW(current_os_window, METH_NOARGS), MW(next_window_id, METH_NOARGS), diff --git a/kitty/window.py b/kitty/window.py index d0ea75be7..440183398 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -68,6 +68,7 @@ from .fast_data_types import ( encode_key_for_tty, get_boss, get_click_interval, + get_mouse_data_for_window, get_options, is_css_pointer_name_valid, last_focused_os_window_id, @@ -125,6 +126,7 @@ MatchPatternType = Union[Pattern[str], Tuple[Pattern[str], Optional[Pattern[str] if TYPE_CHECKING: from kittens.tui.handler import OpenUrlHandler + from .fast_data_types import MousePosition from .file_transmission import FileTransmission @@ -1710,6 +1712,10 @@ class Window: else: self.screen.erase_in_display(3 if scrollback else 2, False) + def current_mouse_position(self) -> Optional['MousePosition']: + ' Return the last position at which a mouse event was received by this window ' + return get_mouse_data_for_window(self.os_window_id, self.tab_id, self.id) + # actions {{{ @ac('cp', 'Show scrollback in a pager like less')