From ee82185776a8d602bd71e6e119dc2ebfe21afa78 Mon Sep 17 00:00:00 2001 From: Noam Stolero Date: Mon, 16 Nov 2026 12:18:19 +0200 Subject: [PATCH] Fix the cursor colour while rendering is paused with pixel scrolling With pixel_scroll the paused rendering snapshot has an extra leading row, so the cell under the cursor was read from the row above it and the cursor took that cell's colours. Read the cursor's line through a helper that offsets for the extra row. The helper is exposed to Python as Screen.paused_cursor_line so that the row choice can be tested. --- kitty/fast_data_types.pyi | 1 + kitty/screen.c | 29 ++++++++++++++++++++++++++++- kitty/screen.h | 1 + kitty/shaders.c | 3 +-- kitty_tests/screen.py | 18 ++++++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 80c77b525..bf4e89447 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -1371,6 +1371,7 @@ class Screen: def scrollbar_geometry(self) -> Tuple[float, float]: ... def has_scrollbar(self, is_hovering: bool) -> bool: ... def update_graphics_layers(self) -> Tuple[ImageLayer, ...]: ... + def paused_cursor_line(self, y: int) -> Line: ... def set_tab_bar_render_data(os_window_id: int, screen: Screen, left: int, top: int, right: int, bottom: int) -> None: pass diff --git a/kitty/screen.c b/kitty/screen.c index 812123f27..b0586dd8e 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -6390,6 +6390,33 @@ line(Screen *self, PyObject *val) { return (PyObject *)self->linebuf->line; } +Line * +screen_paused_line_for_cursor(Screen *self, index_type y) { + if (!self->paused_rendering.expires_at) return NULL; + // With pixel scroll the snapshot has an extra leading row, so screen row y is at y + 1 + y += self->paused_rendering.pixel_scroll; + if (y >= self->paused_rendering.linebuf->ynum) return NULL; + linebuf_init_line(self->paused_rendering.linebuf, y); + return self->paused_rendering.linebuf->line; +} + +static PyObject * +paused_cursor_line(Screen *self, PyObject *val) { + unsigned long y = PyLong_AsUnsignedLong(val); + if (PyErr_Occurred()) return NULL; + if (y >= self->lines) { + PyErr_SetString(PyExc_IndexError, "Out of bounds"); + return NULL; + } + Line *ans = screen_paused_line_for_cursor(self, y); + if (!ans) { + PyErr_SetString(PyExc_RuntimeError, "Rendering is not paused"); + return NULL; + } + Py_INCREF(ans); + return (PyObject *)ans; +} + Line * screen_visual_line(Screen *self, index_type y) { if (y >= self->lines) return NULL; @@ -8223,7 +8250,7 @@ static PyMethodDef methods[] = { MND(current_key_encoding_flags, METH_NOARGS) MND(text_for_selection, METH_VARARGS) MND(text_for_marked_url, METH_VARARGS) MND(is_rectangle_select, METH_NOARGS) MND(scroll, METH_VARARGS) MND(scroll_to_absolute, METH_O) MND(fractional_scroll, METH_O) MND(scroll_to_prompt, METH_VARARGS) MND(set_last_visited_prompt, METH_VARARGS) MND(send_escape_code_to_child, METH_VARARGS) - MND(pause_rendering, METH_VARARGS) MND(hyperlink_at, METH_VARARGS) MND(toggle_alt_screen, METH_NOARGS) MND(reset_callbacks, METH_NOARGS) + MND(pause_rendering, METH_VARARGS) MND(paused_cursor_line, METH_O) MND(hyperlink_at, METH_VARARGS) MND(toggle_alt_screen, METH_NOARGS) MND(reset_callbacks, METH_NOARGS) MND(paste, METH_O) MND(paste_bytes, METH_O) MND(focus_changed, METH_O) MND(has_focus, METH_NOARGS) MND(has_activity_since_last_focus, METH_NOARGS) MND(copy_colors_from, METH_O) MND(set_marker, METH_VARARGS) MND(marked_cells, METH_NOARGS) MND(scroll_to_next_mark, METH_VARARGS) MND(update_only_line_graphics_data, METH_NOARGS) diff --git a/kitty/screen.h b/kitty/screen.h index 07493c420..a604f8ce0 100644 --- a/kitty/screen.h +++ b/kitty/screen.h @@ -416,6 +416,7 @@ bool get_line_edge_colors_at_row(Screen *self, index_type y, color_type *left, c bool parse_sgr(Screen *screen, const uint8_t *buf, unsigned int num, const char *report_name, bool is_deccara); bool screen_pause_rendering(Screen *self, bool pause, int for_in_ms); void screen_check_pause_rendering(Screen *self, monotonic_t now); +Line *screen_paused_line_for_cursor(Screen *self, index_type y); void screen_designate_charset(Screen *self, uint32_t which, uint32_t as); void screen_multi_cursor(Screen *self, int queried_shape, int *params, unsigned num_params); #define DECLARE_CH_SCREEN_HANDLER(name) void screen_##name(Screen *screen); diff --git a/kitty/shaders.c b/kitty/shaders.c index 0476cdec5..d64ee242b 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -1119,8 +1119,7 @@ cell_update_uniform_block( bool reversed = false; if (cursor->x < screen->columns && cursor->y < screen->lines) { if (screen->paused_rendering.expires_at) { - linebuf_init_line(screen->paused_rendering.linebuf, cursor->y); - line_for_cursor = screen->paused_rendering.linebuf->line; + line_for_cursor = screen_paused_line_for_cursor(screen, cursor->y); } else { linebuf_init_line(screen->linebuf, cursor->y); line_for_cursor = screen->linebuf->line; diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index dd7957cd0..6e7f3e906 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -412,6 +412,24 @@ class TestScreen(BaseTest): self.assertTrue(s.pause_rendering(False)) self.ae(s.current_selections(), before) + def test_paused_cursor_line_matches_screen_row(self): + for pixel_scroll in (True, False): + s = self.create_screen(cols=6, lines=4, options={'pixel_scroll': pixel_scroll}) + texts = [f'row{i}' for i in range(s.lines)] + for i, text in enumerate(texts): + s.draw(text) + if i + 1 < s.lines: + parse_bytes(s, b'\r\n') + self.assertTrue(s.pause_rendering(True, 5000)) + parse_bytes(s, b'\x1b[H\x1b[2JXXXX') # live screen changes, the snapshot must not + for r, text in enumerate(texts): + self.ae(str(s.paused_cursor_line(r)), text, f'{pixel_scroll=} {r=}') + with self.assertRaises(IndexError): + s.paused_cursor_line(s.lines) + self.assertTrue(s.pause_rendering(False)) + with self.assertRaises(RuntimeError): + s.paused_cursor_line(0) + def test_emoji_skin_tone_modifiers(self): s = self.create_screen() q = chr(0x1F469) + chr(0x1F3FD)