From 3728555fbdfa5d3cf91a12f36c9ea8adbc025cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Wed, 3 May 2023 13:57:04 +0100 Subject: [PATCH] Scroll cursor into view. (#2464) * Scroll cursor into view. Related issues: #2459. * Add regression test. * Update changelog. --- CHANGELOG.md | 6 ++++++ src/textual/widgets/_data_table.py | 1 + tests/test_data_table.py | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4acf4a763..56b6aff249 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## Unreleased + +### Changed + +- The DataTable cursor is now scrolled into view when the cursor coordinate is changed programmatically https://github.com/Textualize/textual/issues/2459 + ## [0.23.0] - 2023-05-03 ### Fixed diff --git a/src/textual/widgets/_data_table.py b/src/textual/widgets/_data_table.py index 0681c39935..5991c98db9 100644 --- a/src/textual/widgets/_data_table.py +++ b/src/textual/widgets/_data_table.py @@ -950,6 +950,7 @@ def watch_cursor_coordinate( elif self.cursor_type == "column": self.refresh_column(old_coordinate.column) self._highlight_column(new_coordinate.column) + self._scroll_cursor_into_view() def _highlight_coordinate(self, coordinate: Coordinate) -> None: """Apply highlighting to the cell at the coordinate, and post event.""" diff --git a/tests/test_data_table.py b/tests/test_data_table.py index 56b148d562..0bba677cf7 100644 --- a/tests/test_data_table.py +++ b/tests/test_data_table.py @@ -991,3 +991,28 @@ def test_key_string_lookup(): assert dictionary[RowKey("foo")] == "bar" assert dictionary["hello"] == "world" assert dictionary[RowKey("hello")] == "world" + + +async def test_scrolling_cursor_into_view(): + """Regression test for https://github.com/Textualize/textual/issues/2459""" + + class TableApp(App): + CSS = "DataTable { height: 100%; }" + + def compose(self): + yield DataTable() + + def on_mount(self) -> None: + table = self.query_one(DataTable) + table.add_column("n") + table.add_rows([(n,) for n in range(300)]) + + def key_c(self): + self.query_one(DataTable).cursor_coordinate = Coordinate(200, 0) + + app = TableApp() + + async with app.run_test() as pilot: + await pilot.press("c") + await pilot.pause() + assert app.query_one(DataTable).scroll_y > 100