diff --git a/docs/changelog.rst b/docs/changelog.rst index 8109525e2..9b022450e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -138,6 +138,9 @@ Detailed list of changes ``clear_terminal last_command`` not working or moving the view when scrolled back (:pull:`10841`) +- Fix images, the cursor and the scrollbar not matching the text during + synchronized output when new output arrives while scrolled back (:pull:`10847`) + - Fix jumping back to the last visited prompt and :ac:`show_last_visited_command_output` failing or using the wrong lines after ``clear_terminal last_command`` or after output scrolls only part of the diff --git a/kitty/child-monitor.c b/kitty/child-monitor.c index 63d05fbc6..3c2a62e26 100644 --- a/kitty/child-monitor.c +++ b/kitty/child-monitor.c @@ -836,7 +836,8 @@ collect_cursor_info(CursorRenderInfo *ans, Window *w, monotonic_t now, OSWindow ans->multicursor_count = 0; ans->cursor_opacity = 1; ans->text_blink_opacity = 1; - if (!rd->screen->scrolled_by) { + // Use the scroll position of the rendered frame so the cursor matches the frozen text while rendering is paused + if (!rendered_scrolled_by(rd->screen)) { ans->multicursor_count = screen_multi_cursor_count(rd->screen); ans->is_visible = screen_is_cursor_visible(rd->screen); } diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index ed0b6f2e6..5996d3ada 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -1123,6 +1123,22 @@ class Cursor: text_blink: bool shape: int +class ImageLayerRect(TypedDict): + left: float + top: float + right: float + bottom: float + +class ImageLayer(TypedDict): + src_rect: ImageLayerRect + dest_rect: ImageLayerRect + group_count: int + z_index: int + image_id: int + ref_id: int + indexed_px_sz: int + is_unscaled: bool + class Screen: color_profile: ColorProfile columns: int @@ -1327,6 +1343,10 @@ class Screen: def change_pointer_shape(self, op: str, name: str) -> None: ... def bell(self) -> None: ... def pause_rendering(self, pause: bool = True, for_how_long_in_ms: int = 100) -> bool: ... + def rendered_scroll_state(self) -> Tuple[int, int, int]: ... + def scrollbar_geometry(self) -> Tuple[float, float]: ... + def has_scrollbar(self, is_hovering: bool) -> bool: ... + def update_graphics_layers(self) -> Tuple[ImageLayer, ...]: ... 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/graphics.c b/kitty/graphics.c index 808601bdc..7388c670a 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -3071,12 +3071,8 @@ W(shm_unlink) { Py_RETURN_NONE; } -W(update_layers) { - unsigned int scrolled_by, sx, sy; - float xstart, ystart, dx, dy, scroll_offset_lines = 0.0f; - CellPixelSize cell; - PA("IffffIIII|f", &scrolled_by, &xstart, &ystart, &dx, &dy, &sx, &sy, &cell.width, &cell.height, &scroll_offset_lines); - grman_update_layers(self, scrolled_by, scroll_offset_lines, xstart, ystart, dx, dy, sx, sy, cell); +PyObject * +grman_render_data_as_python(GraphicsManager *self) { PyObject *ans = PyTuple_New(self->render_data.count); for (size_t i = 0; i < self->render_data.count; i++) { ImageRenderData *r = self->render_data.item + i; @@ -3107,6 +3103,15 @@ W(update_layers) { return ans; } +W(update_layers) { + unsigned int scrolled_by, sx, sy; + float xstart, ystart, dx, dy, scroll_offset_lines = 0.0f; + CellPixelSize cell; + PA("IffffIIII|f", &scrolled_by, &xstart, &ystart, &dx, &dy, &sx, &sy, &cell.width, &cell.height, &scroll_offset_lines); + grman_update_layers(self, scrolled_by, scroll_offset_lines, xstart, ystart, dx, dy, sx, sy, cell); + return grman_render_data_as_python(self); +} + #define M(x, va) {#x, (PyCFunction)py##x, va, ""} static PyMethodDef methods[] = { diff --git a/kitty/graphics.h b/kitty/graphics.h index 0e2475a4c..12872cfc1 100644 --- a/kitty/graphics.h +++ b/kitty/graphics.h @@ -271,6 +271,7 @@ png_from_data(void *png_data, size_t png_data_sz, const char *path_for_error_mes bool image_path_to_bitmap(const char *path, uint8_t **data, unsigned int *width, unsigned int *height, size_t *sz); bool scan_active_animations(GraphicsManager *self, const monotonic_t now, monotonic_t *minimum_gap, bool os_window_context_set); void grman_pause_rendering(GraphicsManager *self, GraphicsManager *dest); +PyObject *grman_render_data_as_python(GraphicsManager *self); void grman_mark_layers_dirty(GraphicsManager *self); void grman_set_window_id(GraphicsManager *self, id_type id); bool grman_has_images(GraphicsManager *self); diff --git a/kitty/screen.c b/kitty/screen.c index 804156b5e..2b5513d5b 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -3971,6 +3971,8 @@ screen_pause_rendering(Screen *self, bool pause, int for_in_ms) { self->paused_rendering.expires_at = monotonic() + ms_to_monotonic_t(for_in_ms); self->paused_rendering.inverted = self->modes.mDECSCNM; self->paused_rendering.scrolled_by = self->scrolled_by; + self->paused_rendering.history_count = self->historybuf->count; + self->paused_rendering.on_main_screen = self->linebuf == self->main_linebuf; self->paused_rendering.cell_data_updated = false; self->paused_rendering.cursor_visible = self->modes.mDECTCEM; // Preserve the pixel scroll geometry so that pausing does not cause the rendered content to jump, see #10794 @@ -4559,12 +4561,53 @@ screen_is_cell_selected(Screen *self, index_type x, index_type y) { return false; } +bool +screen_update_graphics_layers(Screen *self) { + // While rendering is paused, the frozen image placements must be positioned using the + // scroll position captured with the frozen text, not the live one, which new output may have changed + GraphicsManager *grman = self->paused_rendering.expires_at ? self->paused_rendering.grman : self->grman; + return grman_update_layers( + grman, + rendered_scrolled_by(self), + scroll_offset_lines_for_screen(self), + -1.f, + 1.f, + 2.f / self->columns, + 2.f / self->lines, + self->columns, + self->lines, + self->cell_size); +} + +bool +screen_has_scrollbar(const Screen *self, bool is_hovering) { + if (!rendered_on_main_screen(self) || !rendered_history_count(self)) return false; + const bool scrolled = rendered_scrolled_by(self) > 0; + switch (OPT(scrollbar)) { + case SCROLLBAR_NEVER: return false; + case SCROLLBAR_ALWAYS: return true; + case SCROLLBAR_ON_SCROLLED: return scrolled; + case SCROLLBAR_ON_HOVERED: return is_hovering; + case SCROLLBAR_ON_SCROLL_AND_HOVER: return scrolled && is_hovering; + case SCROLLBAR_ON_SCROLL_OR_HOVER: return scrolled || is_hovering; + } + return false; +} + +void +screen_scrollbar_geometry(const Screen *self, float *handle_position, float *visible_fraction) { + // handle_position is 0 at the bottom of the scrollback and 1 at the top + const unsigned history_count = rendered_history_count(self); + *handle_position = (float)((rendered_scrolled_by(self) + (double)scroll_offset_lines_for_screen(self)) / MAX(1u, history_count)); + *visible_fraction = (float)self->lines / (float)(self->lines + history_count); +} + void screen_apply_selection(Screen *self, void *address_, size_t size) { uint8_t *address = address_; memset(address, 0, size); const unsigned offset = pixel_scroll_enabled(self); - const unsigned scrolled_by = self->paused_rendering.expires_at ? self->paused_rendering.scrolled_by : self->scrolled_by; + const unsigned scrolled_by = rendered_scrolled_by(self); Selections *sel = self->paused_rendering.expires_at ? &self->paused_rendering.selections : &self->selections; for (size_t i = 0; i < sel->count; i++) apply_selection(self, address, sel->items + i, 1, offset, scrolled_by); sel->last_rendered_count = sel->count; @@ -7287,10 +7330,35 @@ current_selections(Screen *self, PyObject *a UNUSED) { PyObject *ans = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)render_lines_for_screen(self) * self->columns); if (!ans) return NULL; screen_apply_selection(self, PyBytes_AS_STRING(ans), PyBytes_GET_SIZE(ans)); - self->last_rendered.scrolled_by = self->scrolled_by; // mimic what the renderer does + self->last_rendered.scrolled_by = rendered_scrolled_by(self); // mimic what the renderer does return ans; } +static PyObject * +update_graphics_layers(Screen *self, PyObject *a UNUSED) { + screen_update_graphics_layers(self); + return grman_render_data_as_python(self->paused_rendering.expires_at ? self->paused_rendering.grman : self->grman); +} + +static PyObject * +rendered_scroll_state(Screen *self, PyObject *a UNUSED) { + return Py_BuildValue("III", rendered_scrolled_by(self), rendered_pixel_scroll_offset_y(self), rendered_history_count(self)); +} + +static PyObject * +has_scrollbar(Screen *self, PyObject *is_hovering) { + const int hovering = PyObject_IsTrue(is_hovering); + if (hovering < 0) return NULL; + return Py_NewRef(screen_has_scrollbar(self, hovering) ? Py_True : Py_False); +} + +static PyObject * +scrollbar_geometry(Screen *self, PyObject *a UNUSED) { + float handle_position, visible_fraction; + screen_scrollbar_geometry(self, &handle_position, &visible_fraction); + return Py_BuildValue("ff", handle_position, visible_fraction); +} + static PyObject * is_selection_dirty(Screen *self, PyObject *a UNUSED) { if (screen_is_selection_dirty(self)) Py_RETURN_TRUE; @@ -7538,8 +7606,10 @@ static PyMethodDef methods[] = { 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) - MND(bell, METH_NOARGS) MND(current_selections, METH_NOARGS) MND(is_selection_dirty, METH_NOARGS){ - "select_graphic_rendition", (PyCFunction)_select_graphic_rendition, METH_VARARGS, ""}, + MND(bell, METH_NOARGS) MND(current_selections, METH_NOARGS) MND(update_graphics_layers, METH_NOARGS) + MND(rendered_scroll_state, METH_NOARGS) MND(scrollbar_geometry, METH_NOARGS) MND(has_scrollbar, METH_O) + MND(is_selection_dirty, + METH_NOARGS){"select_graphic_rendition", (PyCFunction)_select_graphic_rendition, METH_VARARGS, ""}, {NULL} /* Sentinel */ }; diff --git a/kitty/screen.h b/kitty/screen.h index a4c3a3e4e..335ca2cd6 100644 --- a/kitty/screen.h +++ b/kitty/screen.h @@ -227,8 +227,8 @@ typedef struct { monotonic_t expires_at; Cursor cursor; ColorProfile color_profile; - bool inverted, cell_data_updated, cursor_visible, pixel_scroll; - unsigned int scrolled_by, pixel_scroll_offset_y; + bool inverted, cell_data_updated, cursor_visible, pixel_scroll, on_main_screen; + unsigned int scrolled_by, pixel_scroll_offset_y, history_count; LineBuf *linebuf; GraphicsManager *grman; Selections selections, url_ranges; @@ -258,8 +258,20 @@ typedef struct { #define pixel_scroll_enabled(screen) ((screen)->paused_rendering.expires_at ? (screen)->paused_rendering.pixel_scroll : live_pixel_scroll_enabled(screen)) #define rendered_pixel_scroll_offset_y(screen) \ ((screen)->paused_rendering.expires_at ? (screen)->paused_rendering.pixel_scroll_offset_y : (screen)->pixel_scroll_offset_y) +// The scroll position and scrollback size of the frame being rendered. While +// rendering is paused these are the values captured at pause time, so that +// everything drawn over the frozen text (images, cursor, scrollbar, etc.) +// stays aligned with it even as new output changes the live values. +#define rendered_scrolled_by(screen) ((screen)->paused_rendering.expires_at ? (screen)->paused_rendering.scrolled_by : (screen)->scrolled_by) +#define rendered_history_count(screen) ((screen)->paused_rendering.expires_at ? (screen)->paused_rendering.history_count : (screen)->historybuf->count) +#define rendered_on_main_screen(screen) \ + ((screen)->paused_rendering.expires_at ? (screen)->paused_rendering.on_main_screen : (screen)->linebuf == (screen)->main_linebuf) #define render_lines_for_screen(screen) (screen->lines + pixel_scroll_enabled(screen)) +// The fraction of a line by which the rendered content is scrolled when pixel scrolling +#define scroll_offset_lines_for_screen(screen) \ + ((pixel_scroll_enabled(screen) && (screen)->cell_size.height) ? (float)(rendered_pixel_scroll_offset_y(screen) / (double)(screen)->cell_size.height) : 0.f) + void screen_align(Screen *); void screen_garbage_collect_text_cache(Screen *screen); void screen_restore_cursor(Screen *); @@ -335,6 +347,9 @@ void select_graphic_rendition(Screen *self, int *params, unsigned int count, boo void report_device_status(Screen *self, unsigned int which, bool UNUSED); void report_mode_status(Screen *self, unsigned int which, bool); void screen_apply_selection(Screen *self, void *address, size_t size); +bool screen_update_graphics_layers(Screen *self); +void screen_scrollbar_geometry(const Screen *self, float *handle_position, float *visible_fraction); +bool screen_has_scrollbar(const Screen *self, bool is_hovering); bool screen_is_selection_dirty(Screen *self); bool screen_has_selection(Screen *); bool screen_is_cell_selected(Screen *, index_type x, index_type y); diff --git a/kitty/shaders.c b/kitty/shaders.c index f56da38f1..df91085c4 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -63,12 +63,6 @@ row_offset_for_screen(const Screen *screen) { return -1.f + (float)(rendered_pixel_scroll_offset_y(screen) / (double)screen->cell_size.height); } -static inline float -scroll_offset_lines_for_screen(const Screen *screen) { - if (!pixel_scroll_enabled(screen) || !screen->cell_size.height) return 0.f; - return (float)(rendered_pixel_scroll_offset_y(screen) / (double)screen->cell_size.height); -} - // Sprites {{{ typedef struct { int xnum, ynum, x, y, z, last_num_of_layers, last_ynum; @@ -1394,31 +1388,17 @@ cell_prepare_to_render(ssize_t vao_idx, Screen *screen, FONTS_DATA_HANDLE fonts_ changed = true; \ } -#define update_graphics_data(grman) \ - grman_update_layers( \ - grman, \ - screen->scrolled_by, \ - scroll_offset_lines_for_screen(screen), \ - -1.f, \ - 1.f, \ - 2.f / screen->columns, \ - 2.f / screen->lines, \ - screen->columns, \ - screen->lines, \ - screen->cell_size) - if (screen->paused_rendering.expires_at) { if (!screen->paused_rendering.cell_data_updated) { update_selection_data; - update_graphics_data(screen->paused_rendering.grman); + screen_update_graphics_layers(screen); } screen->paused_rendering.cell_data_updated = true; - screen->last_rendered.scrolled_by = screen->paused_rendering.scrolled_by; } else { if (screen->reload_all_gpu_data || screen_resized || screen_is_selection_dirty(screen)) update_selection_data; - if (update_graphics_data(screen->grman)) changed = true; - screen->last_rendered.scrolled_by = screen->scrolled_by; + if (screen_update_graphics_layers(screen)) changed = true; } + screen->last_rendered.scrolled_by = rendered_scrolled_by(screen); #undef update_selection_data #undef update_cell_data screen->last_rendered.columns = screen->columns; @@ -1631,16 +1611,7 @@ draw_drag_preview_overlay(const UIRenderData *ui) { static bool has_scrollbar(Window *w, Screen *screen) { - if (screen->linebuf != screen->main_linebuf || !screen->historybuf->count) return false; - switch (OPT(scrollbar)) { - case SCROLLBAR_NEVER: return false; - case SCROLLBAR_ALWAYS: return true; - case SCROLLBAR_ON_SCROLLED: return screen->scrolled_by > 0; - case SCROLLBAR_ON_HOVERED: return w->scrollbar.is_hovering; - case SCROLLBAR_ON_SCROLL_AND_HOVER: return screen->scrolled_by > 0 && w->scrollbar.is_hovering; - case SCROLLBAR_ON_SCROLL_OR_HOVER: return screen->scrolled_by > 0 || w->scrollbar.is_hovering; - } - return false; + return screen_has_scrollbar(screen, w->scrollbar.is_hovering); } static unsigned @@ -1910,9 +1881,8 @@ draw_scrollbar(const UIRenderData *ui) { if (!window || !screen || !has_scrollbar(window, screen)) return; color_type bar_color = scrollbar_color(screen, OPT(scrollbar_handle_color)), track_color = scrollbar_color(screen, OPT(scrollbar_track_color)); - double cell_frac = (double)screen->pixel_scroll_offset_y / screen->cell_size.height; - if (!OPT(pixel_scroll)) cell_frac = 0; - float bar_frac = (float)(screen->scrolled_by + cell_frac) / MAX(1u, (float)screen->historybuf->count); + float bar_frac, visible_fraction; + screen_scrollbar_geometry(screen, &bar_frac, &visible_fraction); float opacity = OPT(scrollbar_handle_opacity); float track_opacity = window->scrollbar.is_hovering ? OPT(scrollbar_track_hover_opacity) : OPT(scrollbar_track_opacity); GLsizei scrollbar_width_px = (GLsizei)(OPT(scrollbar_width) * ui->cell_width); @@ -1931,7 +1901,6 @@ draw_scrollbar(const UIRenderData *ui) { GLsizei scrollbar_height = window_height - 2 * scrollbar_gap_px; // Calculate thumb size and position - float visible_fraction = (float)screen->lines / (float)(screen->lines + screen->historybuf->count); float min_thumb_height_fraction = (OPT(scrollbar_min_handle_height) * ui->cell_height) / (float)window_height; float thumb_height_fraction = MAX(min_thumb_height_fraction, visible_fraction); diff --git a/kitty_tests/graphics.py b/kitty_tests/graphics.py index 5650f99b2..ec78f3b63 100644 --- a/kitty_tests/graphics.py +++ b/kitty_tests/graphics.py @@ -1455,6 +1455,153 @@ class TestGraphics(BaseTest): s.update_only_line_graphics_data() self.ae(ref_rows(scroll_view(6, True)), before) + def test_image_layers_with_paused_rendering(self): + # While rendering is paused, images are drawn from a frozen copy of the + # graphics manager, so they must be positioned using the scroll state + # captured with the frozen text, not the live one, otherwise output + # received during the pause shifts them relative to the text. + cw, ch = 10, 20 + + def screen(pixel_scroll=False, lines=5, before=10, after=20): + s = self.create_screen(10, lines, scrollback=50, cell_width=cw, cell_height=ch, options={'pixel_scroll': pixel_scroll}) + for i in range(before): + parse_bytes(s, f'row{i:03d}\r\n'.encode()) + # The image is placed in the cell just after the text IMG + s.draw('IMG') + send_command(s, 'a=T,f=24,i=1,s=10,v=20,c=1,r=1,C=1,q=2', b'x' * 10 * 20 * 3) + s.carriage_return(), s.linefeed() + for i in range(after): + parse_bytes(s, f'row{i + before:03d}\r\n'.encode()) + return s + + def dy(s): + return 2 / s.lines + + def live_layers(s): + offset = s.pixel_scroll_offset_y / ch if s.pixel_scroll_offset_y else 0 + return s.grman.update_layers(s.scrolled_by, -1, 1, 2 / s.columns, dy(s), s.columns, s.lines, cw, ch, offset) + + def image_rows(s, layers): + offset = s.rendered_scroll_state()[1] / ch + return [round((1 - r['dest_rect']['top']) / dy(s) - offset) for r in layers] + + def text_row(s): + for y in range(s.lines): + if str(s.visual_line(y)).startswith('IMG'): + return y + + def assert_layers_close(a, b): + # Positions computed from different scroll states differ by float rounding + self.ae(len(a), len(b)) + for x, y in zip(a, b): + self.ae({k: v for k, v in x.items() if k != 'dest_rect'}, {k: v for k, v in y.items() if k != 'dest_rect'}) + for side in ('left', 'top', 'right', 'bottom'): + self.assertAlmostEqual(x['dest_rect'][side], y['dest_rect'][side], places=5) + + def assert_aligned(s, layers, msg=''): + row = text_row(s) + self.assertIsNotNone(row, msg) + self.ae(image_rows(s, layers), [row], msg) + + # Not paused: matches positioning with the live scroll state + for scrolled_by in (0, 5, 16, 17, 19, 21, 22): + s = screen() + s.scroll(scrolled_by, True) + self.ae(s.scrolled_by, scrolled_by) + layers = s.update_graphics_layers() + self.ae(layers, live_layers(s)) + if 17 <= scrolled_by <= 21: + assert_aligned(s, layers, f'not paused, scrolled by {scrolled_by}') + else: + self.ae(layers, ()) # image is not visible + + # Output arriving while paused and scrolled back + for num_new in (1, 3, 4, 10): + with self.subTest(num_new=num_new): + s = screen() + s.scroll(19, True) + before = s.update_graphics_layers() + self.ae(len(before), 1) + assert_aligned(s, before, 'before pause') + parse_bytes(s, b'\x1b[?2026h') + for i in range(num_new): + parse_bytes(s, f'new{i}\r\n'.encode()) + self.ae(s.scrolled_by, 19 + num_new) + during = s.update_graphics_layers() + self.ae(during, before) + assert_aligned(s, during, 'during pause') + parse_bytes(s, b'\x1b[?2026l') + after = s.update_graphics_layers() + self.ae(after, live_layers(s)) + assert_aligned(s, after, 'after pause') + assert_layers_close(after, before) # the view is anchored on the same content + + # Several synchronized updates before the end of the pause + s = screen() + s.scroll(19, True) + before = s.update_graphics_layers() + parse_bytes(s, b'\x1b[?2026hnew\r\n\x1b[?2026l\x1b[?2026hmore\r\n') + self.ae(s.scrolled_by, 21) + self.ae(s.update_graphics_layers(), before) + self.assertTrue(s.pause_rendering(False)) + assert_aligned(s, s.update_graphics_layers(), 'after consecutive updates') + + # Pixel scrolling: the fractional line offset is also frozen + for position in (18.5, 19.25): + with self.subTest(position=position): + s = screen(pixel_scroll=True) + s.scroll_to_absolute(position) + self.assertNotEqual(s.pixel_scroll_offset_y, 0) + before = s.update_graphics_layers() + self.ae(before, live_layers(s)) + self.ae(len(before), 1) + parse_bytes(s, b'\x1b[?2026h') + for i in range(3): + parse_bytes(s, f'new{i}\r\n'.encode()) + self.ae(s.update_graphics_layers(), before) + parse_bytes(s, b'\x1b[?2026l') + after = s.update_graphics_layers() + self.ae(after, live_layers(s)) + assert_layers_close(after, before) # the view is anchored on the same content + + # Paused at the bottom: output scrolls the live image up, the frozen one stays + s = screen(before=2, after=1) + self.ae(s.scrolled_by, 0) + before = s.update_graphics_layers() + assert_aligned(s, before, 'at bottom') + parse_bytes(s, b'\x1b[?2026h') + for i in range(2): + parse_bytes(s, f'new{i}\r\n'.encode()) + self.ae(s.scrolled_by, 0) + self.ae(s.update_graphics_layers(), before) + self.assertNotEqual(live_layers(s), before) + parse_bytes(s, b'\x1b[?2026l') + after = s.update_graphics_layers() + self.ae(image_rows(s, after), [r - 2 for r in image_rows(s, before)]) + assert_aligned(s, after, 'at bottom after pause') + + # Filling from the scrollback during the pause moves the live view to the bottom + s = screen(after=4) + s.scroll(1, True) + self.ae(s.scrolled_by, 1) + before = s.update_graphics_layers() + self.ae(len(before), 1) + parse_bytes(s, b'\x1b[?2026h\x1b[+T') + self.ae(s.scrolled_by, 0) + self.ae(s.update_graphics_layers(), before) + self.assertTrue(s.pause_rendering(False)) + self.ae(s.update_graphics_layers(), live_layers(s)) + + # Images deleted during the pause remain in the frozen frame + s = screen() + s.scroll(19, True) + before = s.update_graphics_layers() + parse_bytes(s, b'\x1b[?2026hnew\r\n') + send_command(s, 'a=d,d=I,i=1,q=2') + self.ae(s.update_graphics_layers(), before) + parse_bytes(s, b'\x1b[?2026l') + self.ae(s.update_graphics_layers(), ()) + def test_unicode_placeholders_history_fill(self): # Lines popped back from the history (CSI + T, or enlarging the window with # scrollback_fill_enlarged_window) move the history down, so cell images diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index 8e41bb25d..3d770609a 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -1352,6 +1352,248 @@ class TestScreen(BaseTest): # marked (set_mask = 1 for plain selections). self.assertIn(1, result) + def test_rendered_scroll_state_with_paused_rendering(self): + # While rendering is paused, everything drawn over the frozen text + # (images, cursor, scrollbar) must use the scroll state captured at + # pause time, not the live one, which output can change during the pause. + def screen(pixel_scroll=False): + s = self.create_screen(cols=10, lines=3, scrollback=50, cell_height=20, options={'pixel_scroll': pixel_scroll}) + for i in range(40): + parse_bytes(s, f'row{i:03d}\r\n'.encode()) + return s + + def live(s): + return s.scrolled_by, s.pixel_scroll_offset_y, s.historybuf.count + + # Not paused: the rendered state is the live state + s = screen() + self.ae(s.rendered_scroll_state(), live(s)) + s.scroll(5, True) + self.ae(s.rendered_scroll_state(), live(s)) + + # Output arriving while scrolled back moves the live view to keep it anchored + s = screen() + s.scroll(5, True) + captured = live(s) + parse_bytes(s, b'\x1b[?2026h') + for i in range(4): + parse_bytes(s, f'new{i}\r\n'.encode()) + self.ae(s.scrolled_by, 9) + self.ae(s.rendered_scroll_state(), captured) + parse_bytes(s, b'\x1b[?2026l') + self.ae(s.rendered_scroll_state(), live(s)) + self.ae(s.scrolled_by, 9) + + # Filling from the scrollback can bring the live view to the bottom + # while the frozen frame still shows the scrollback + s = screen() + s.scroll(1, True) + captured = live(s) + parse_bytes(s, b'\x1b[?2026h\x1b[+T') + self.ae((s.scrolled_by, s.historybuf.count), (0, captured[2] - 1)) + self.ae(s.rendered_scroll_state(), captured) + self.assertTrue(s.pause_rendering(False)) + self.ae(s.rendered_scroll_state(), live(s)) + + # Clearing the scrollback while at the bottom does not end the pause + s = screen() + captured = live(s) + self.assertGreater(captured[2], 0) + parse_bytes(s, b'\x1b[?2026h\x1b[3J') + self.ae(s.historybuf.count, 0) + self.ae(s.rendered_scroll_state(), captured) + + # Scrolling the view ends the pause, so the rendered state follows it + s = screen() + parse_bytes(s, b'\x1b[?2026h') + s.scroll(3, True) + self.ae(s.rendered_scroll_state(), live(s)) + self.ae(s.scrolled_by, 3) + + # Pausing at the bottom, with output that scrolls the screen + s = screen() + captured = live(s) + parse_bytes(s, b'\x1b[?2026h') + for i in range(4): + parse_bytes(s, f'new{i}\r\n'.encode()) + self.ae(s.scrolled_by, 0) + self.ae(s.rendered_scroll_state(), captured) + + # The pixel scroll offset is captured with the scroll position + s = screen(pixel_scroll=True) + s.scroll_to_absolute(10.5) + captured = live(s) + self.ae(captured[:2], (10, 10)) + parse_bytes(s, b'\x1b[?2026h') + for i in range(3): + parse_bytes(s, f'new{i}\r\n'.encode()) + self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (13, 10)) + self.ae(s.rendered_scroll_state(), captured) + self.assertTrue(s.pause_rendering(False)) + self.ae(s.rendered_scroll_state(), live(s)) + + # Without pixel scroll the rendered pixel offset is always zero + s = screen() + s.scroll(4, True) + parse_bytes(s, b'\x1b[?2026h') + self.ae(s.rendered_scroll_state(), (4, 0, s.historybuf.count)) + + def test_selection_uses_rendered_scroll_state(self): + # A selection rendered during a pause must match the frozen frame even + # though output has moved the live view + s = self.create_screen(cols=10, lines=3, scrollback=50) + for i in range(40): + parse_bytes(s, f'row{i:03d}\r\n'.encode()) + s.scroll(5, True) + s.start_selection(0, 0) + s.update_selection(2, 1) + before = s.current_selections() + parse_bytes(s, b'\x1b[?2026h') + for i in range(3): + parse_bytes(s, f'new{i}\r\n'.encode()) + self.ae(s.scrolled_by, 8) + self.ae(s.current_selections(), before) + parse_bytes(s, b'\x1b[?2026l') + after = s.current_selections() + self.assertFalse(s.is_selection_dirty()) + self.ae(after, before) + + def test_scrollbar_geometry(self): + def screen(pixel_scroll=False, scrollback=50): + s = self.create_screen(cols=10, lines=3, scrollback=scrollback, cell_height=20, options={'pixel_scroll': pixel_scroll}) + for i in range(40): + parse_bytes(s, f'row{i:03d}\r\n'.encode()) + return s + + def expected(scrolled_by, pixel_offset, count, lines=3, cell_height=20): + return (scrolled_by + pixel_offset / cell_height) / max(1, count), lines / (lines + count) + + def check(s, scrolled_by, pixel_offset, count, msg=''): + actual = s.scrollbar_geometry() + exp = expected(scrolled_by, pixel_offset, count) + for a, e in zip(actual, exp): + self.assertAlmostEqual(a, e, places=5, msg=msg) + + # No scrollback + s = self.create_screen(cols=10, lines=3, scrollback=50) + self.ae(s.scrollbar_geometry(), (0, 1)) + + # Live geometry at the bottom, scrolled back and at the top + s = screen() + count = s.historybuf.count + self.ae(count, 38) + check(s, 0, 0, count, 'bottom') + s.scroll(5, True) + check(s, 5, 0, count, 'scrolled') + s.scroll(count, True) + check(s, count, 0, count, 'top') + self.assertAlmostEqual(s.scrollbar_geometry()[0], 1, places=5) + + # The pixel scroll offset contributes a fraction of a line + s = screen(pixel_scroll=True) + s.scroll_to_absolute(10.5) + check(s, 10, 10, s.historybuf.count, 'pixel scroll') + + # Output arriving during a pause does not move the handle + for pixel_scroll in (False, True): + with self.subTest(pixel_scroll=pixel_scroll): + s = screen(pixel_scroll=pixel_scroll) + s.scroll_to_absolute(5.5 if pixel_scroll else 5) + before = s.scrollbar_geometry() + parse_bytes(s, b'\x1b[?2026h') + for i in range(6): + parse_bytes(s, f'new{i}\r\n'.encode()) + self.assertGreater(s.scrolled_by, 5) + self.ae(s.scrollbar_geometry(), before) + parse_bytes(s, b'\x1b[?2026l') + check(s, s.scrolled_by, s.pixel_scroll_offset_y if pixel_scroll else 0, s.historybuf.count, 'after pause') + self.assertNotEqual(s.scrollbar_geometry(), before) + + # Full scrollback: the view stops being anchored once it reaches the top + s = screen(scrollback=20) + s.scroll(15, True) + before = s.scrollbar_geometry() + parse_bytes(s, b'\x1b[?2026h') + for i in range(10): + parse_bytes(s, f'new{i}\r\n'.encode()) + self.ae(s.scrollbar_geometry(), before) + + # Changes to the scrollback size during a pause + s = screen() + s.scroll(1, True) + before = s.scrollbar_geometry() + parse_bytes(s, b'\x1b[?2026h\x1b[+T') + self.ae(s.scrolled_by, 0) + self.ae(s.scrollbar_geometry(), before) + s = screen() + before = s.scrollbar_geometry() + parse_bytes(s, b'\x1b[?2026h\x1b[3J') + self.ae(s.historybuf.count, 0) + self.ae(s.scrollbar_geometry(), before) + parse_bytes(s, b'\x1b[?2026l') + self.ae(s.scrollbar_geometry(), (0, 1)) + + def test_scrollbar_visibility(self): + def screen(policy, history=True): + s = self.create_screen(cols=10, lines=3, scrollback=50, options={'scrollbar': policy}) + for i in range(40 if history else 1): + parse_bytes(s, f'row{i:03d}\r\n'.encode()) + return s + + def visibility(s): + return s.has_scrollbar(False), s.has_scrollbar(True) + + # (not scrolled, scrolled) -> (not hovering, hovering) + policies = { + 'never': ((False, False), (False, False)), + 'always': ((True, True), (True, True)), + 'scrolled': ((False, False), (True, True)), + 'hovered': ((False, True), (False, True)), + 'scrolled-and-hovered': ((False, False), (False, True)), + 'scrolled-or-hovered': ((False, True), (True, True)), + } + for policy, (at_bottom, scrolled) in policies.items(): + with self.subTest(policy=policy): + s = screen(policy) + self.ae(visibility(s), at_bottom) + s.scroll(3, True) + self.ae(visibility(s), scrolled) + # No scrollback or the alternate screen: never a scrollbar + self.ae(visibility(screen(policy, history=False)), (False, False)) + s = screen(policy) + parse_bytes(s, b'\x1b[?1049h') + self.ae(visibility(s), (False, False)) + + # Filling from the scrollback during a pause brings the live view to + # the bottom, but the frozen frame is still scrolled back + s = screen('scrolled') + s.scroll(1, True) + self.assertTrue(s.has_scrollbar(False)) + parse_bytes(s, b'\x1b[?2026h\x1b[+T') + self.ae(s.scrolled_by, 0) + self.assertTrue(s.has_scrollbar(False)) + parse_bytes(s, b'\x1b[?2026l') + self.assertFalse(s.has_scrollbar(False)) + + # Clearing the scrollback during a pause + s = screen('always') + parse_bytes(s, b'\x1b[?2026h\x1b[3J') + self.ae(s.historybuf.count, 0) + self.assertTrue(s.has_scrollbar(False)) + parse_bytes(s, b'\x1b[?2026l') + self.assertFalse(s.has_scrollbar(False)) + + # Switching screens during a pause + s = screen('always') + parse_bytes(s, b'\x1b[?2026h\x1b[?1049h') + self.assertTrue(s.has_scrollbar(False)) + parse_bytes(s, b'\x1b[?2026l') + self.assertFalse(s.has_scrollbar(False)) + parse_bytes(s, b'\x1b[?2026h\x1b[?1049l') + self.assertFalse(s.has_scrollbar(False)) + parse_bytes(s, b'\x1b[?2026l') + self.assertTrue(s.has_scrollbar(False)) + def test_selection_not_dirty_after_render(self): # Regression test for https://github.com/kovidgoyal/kitty/issues/10714 # selections were considered dirty on every frame when scrolled back,