From 07e964d2ba6b67c39660ced64042f75b6758c113 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Wed, 1 Feb 2023 11:14:08 +0000 Subject: [PATCH] More docstrings for the DataTable, new private property refactor for total_row_height --- src/textual/widgets/_data_table.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/textual/widgets/_data_table.py b/src/textual/widgets/_data_table.py index 2bc819ab7..d2cabe73f 100644 --- a/src/textual/widgets/_data_table.py +++ b/src/textual/widgets/_data_table.py @@ -470,6 +470,10 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True): @property def _y_offsets(self) -> list[tuple[RowKey, int]]: + """Contains a 2-tuple for each line (not row!) of the DataTable. Given a y-coordinate, + we can index into this list to find which row that y-coordinate lands on, and the + y-offset *within* that row. The length of the returned list is therefore the total + height of all rows within the DataTable.""" y_offsets: list[tuple[RowKey, int]] = [] for row in self.ordered_rows: row_key = row.key @@ -477,6 +481,11 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True): y_offsets += [(row_key, y) for y in range(row_height)] return y_offsets + @property + def _total_row_height(self) -> int: + """The total height of all rows within the DataTable""" + return len(self._y_offsets) + def update_cell( self, row_key: RowKey | str, @@ -744,7 +753,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True): header_height = self.header_height if self.show_header else 0 self.virtual_size = Size( total_width, - len(self._y_offsets) + header_height, + self._total_row_height + header_height, ) def _get_cell_region(self, row_index: int, column_index: int) -> Region: @@ -796,7 +805,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True): column_key = self._column_locations.get_key(column_index) width = columns[column_key].render_width header_height = self.header_height if self.show_header else 0 - height = len(self._y_offsets) + header_height + height = self._total_row_height + header_height full_column_region = Region(x, 0, width, height) return full_column_region