Convenience method to get the mouse position for a window

Fix #7652
This commit is contained in:
Kovid Goyal
2024-07-21 22:18:43 +05:30
parent ac4bfd977a
commit e97de84668
3 changed files with 24 additions and 0 deletions

View File

@@ -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]: ...

View File

@@ -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),

View File

@@ -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')