mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Merge branch 'datatable-cell-keys' into datatable-select-headers
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TypeVar, Generic
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
Key = TypeVar("Key")
|
||||
Value = TypeVar("Value")
|
||||
@@ -8,6 +8,8 @@ Value = TypeVar("Value")
|
||||
|
||||
class TwoWayDict(Generic[Key, Value]):
|
||||
"""
|
||||
A two-way mapping offering O(1) access in both directions.
|
||||
|
||||
Wraps two dictionaries and uses them to provide efficient access to
|
||||
both values (given keys) and keys (given values).
|
||||
"""
|
||||
|
||||
@@ -41,7 +41,9 @@ CellType = TypeVar("CellType")
|
||||
|
||||
|
||||
class CellDoesNotExist(Exception):
|
||||
"""Raised when the user supplies coordinates or cell keys which
|
||||
"""The cell key/index was invalid.
|
||||
|
||||
Raised when the user supplies coordinates or cell keys which
|
||||
do not exist in the DataTable."""
|
||||
|
||||
|
||||
@@ -56,13 +58,17 @@ class ColumnDoesNotExist(Exception):
|
||||
|
||||
|
||||
class DuplicateKey(Exception):
|
||||
"""Raised when the RowKey or ColumnKey provided already refers to
|
||||
"""The key supplied already exists.
|
||||
|
||||
Raised when the RowKey or ColumnKey provided already refers to
|
||||
an existing row or column in the DataTable. Keys must be unique."""
|
||||
|
||||
|
||||
@functools.total_ordering
|
||||
class StringKey:
|
||||
"""An object used as a key in a mapping. It can optionally wrap a string,
|
||||
"""An object used as a key in a mapping.
|
||||
|
||||
It can optionally wrap a string,
|
||||
and lookups into a map using the object behave the same as lookups using
|
||||
the string itself."""
|
||||
|
||||
@@ -97,19 +103,25 @@ class StringKey:
|
||||
|
||||
|
||||
class RowKey(StringKey):
|
||||
"""Uniquely identifies a row in the DataTable. Even if the visual location
|
||||
"""Uniquely identifies a row in the DataTable.
|
||||
|
||||
Even if the visual location
|
||||
of the row changes due to sorting or other modifications, a key will always
|
||||
refer to the same row."""
|
||||
|
||||
|
||||
class ColumnKey(StringKey):
|
||||
"""Uniquely identifies a column in the DataTable. Even if the visual location
|
||||
"""Uniquely identifies a column in the DataTable.
|
||||
|
||||
Even if the visual location
|
||||
of the column changes due to sorting or other modifications, a key will always
|
||||
refer to the same column."""
|
||||
|
||||
|
||||
class CellKey(NamedTuple):
|
||||
"""A unique identifier for a cell in the DataTable. Even if the cell changes
|
||||
"""A unique identifier for a cell in the DataTable.
|
||||
|
||||
Even if the cell changes
|
||||
visual location (i.e. moves to a different coordinate in the table), this key
|
||||
can still be used to retrieve it, regardless of where it currently is."""
|
||||
|
||||
@@ -122,8 +134,7 @@ class CellKey(NamedTuple):
|
||||
|
||||
|
||||
def default_cell_formatter(obj: object) -> RenderableType:
|
||||
"""Given an object stored in a DataTable cell, return a Rich
|
||||
renderable type which displays that object.
|
||||
"""Convert a cell into a Rich renderable for display.
|
||||
|
||||
Args:
|
||||
obj: Data for a cell.
|
||||
@@ -275,8 +286,10 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
||||
hover_coordinate: Reactive[Coordinate] = Reactive(Coordinate(0, 0), repaint=False)
|
||||
|
||||
class CellHighlighted(Message, bubble=True):
|
||||
"""Posted when the cursor moves to highlight a new cell. It's only relevant
|
||||
when the `cursor_type` is `"cell"`. It's also posted when the cell cursor is
|
||||
"""Posted when the cursor moves to highlight a new cell.
|
||||
|
||||
This is only relevant when the `cursor_type` is `"cell"`.
|
||||
It's also posted when the cell cursor is
|
||||
re-enabled (by setting `show_cursor=True`), and when the cursor type is
|
||||
changed to `"cell"`. Can be handled using `on_data_table_cell_highlighted` in
|
||||
a subclass of `DataTable` or in a parent widget in the DOM.
|
||||
@@ -307,7 +320,8 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
||||
|
||||
class CellSelected(Message, bubble=True):
|
||||
"""Posted by the `DataTable` widget when a cell is selected.
|
||||
It's only relevant when the `cursor_type` is `"cell"`. Can be handled using
|
||||
|
||||
This is only relevant when the `cursor_type` is `"cell"`. Can be handled using
|
||||
`on_data_table_cell_selected` in a subclass of `DataTable` or in a parent
|
||||
widget in the DOM.
|
||||
|
||||
@@ -336,7 +350,9 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
||||
yield "cell_key", self.cell_key
|
||||
|
||||
class RowHighlighted(Message, bubble=True):
|
||||
"""Posted when a row is highlighted. This message is only posted when the
|
||||
"""Posted when a row is highlighted.
|
||||
|
||||
This message is only posted when the
|
||||
`cursor_type` is set to `"row"`. Can be handled using
|
||||
`on_data_table_row_highlighted` in a subclass of `DataTable` or in a parent
|
||||
widget in the DOM.
|
||||
@@ -357,7 +373,9 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
||||
yield "row_key", self.row_key
|
||||
|
||||
class RowSelected(Message, bubble=True):
|
||||
"""Posted when a row is selected. This message is only posted when the
|
||||
"""Posted when a row is selected.
|
||||
|
||||
This message is only posted when the
|
||||
`cursor_type` is set to `"row"`. Can be handled using
|
||||
`on_data_table_row_selected` in a subclass of `DataTable` or in a parent
|
||||
widget in the DOM.
|
||||
@@ -378,7 +396,9 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
||||
yield "row_key", self.row_key
|
||||
|
||||
class ColumnHighlighted(Message, bubble=True):
|
||||
"""Posted when a column is highlighted. This message is only posted when the
|
||||
"""Posted when a column is highlighted.
|
||||
|
||||
This message is only posted when the
|
||||
`cursor_type` is set to `"column"`. Can be handled using
|
||||
`on_data_table_column_highlighted` in a subclass of `DataTable` or in a parent
|
||||
widget in the DOM.
|
||||
@@ -401,7 +421,9 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
||||
yield "column_key", self.column_key
|
||||
|
||||
class ColumnSelected(Message, bubble=True):
|
||||
"""Posted when a column is selected. This message is only posted when the
|
||||
"""Posted when a column is selected.
|
||||
|
||||
This message is only posted when the
|
||||
`cursor_type` is set to `"column"`. Can be handled using
|
||||
`on_data_table_column_selected` in a subclass of `DataTable` or in a parent
|
||||
widget in the DOM.
|
||||
@@ -552,8 +574,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
||||
*,
|
||||
update_width: bool = False,
|
||||
) -> None:
|
||||
"""Update the content inside the cell with the specified row key
|
||||
and column key.
|
||||
"""Update the cell identified by the specified row key and column key.
|
||||
|
||||
Args:
|
||||
row_key: The key identifying the row.
|
||||
@@ -797,8 +818,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
||||
)
|
||||
|
||||
def coordinate_to_cell_key(self, coordinate: Coordinate) -> CellKey:
|
||||
"""Return the key for the cell currently occupying this coordinate in the
|
||||
DataTable
|
||||
"""Return the key for the cell currently occupying this coordinate.
|
||||
|
||||
Args:
|
||||
coordinate: The coordinate to exam the current cell key of.
|
||||
@@ -1113,8 +1133,9 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
||||
return row_keys
|
||||
|
||||
def on_idle(self) -> None:
|
||||
"""Runs when the message pump is empty. We use this for
|
||||
some expensive calculations like re-computing dimensions of the
|
||||
"""Runs when the message pump is empty.
|
||||
|
||||
We use this for some expensive calculations like re-computing dimensions of the
|
||||
whole DataTable and re-computing column widths after some cells
|
||||
have been updated. This is more efficient in the case of high
|
||||
frequency updates, ensuring we only do expensive computations once."""
|
||||
@@ -1200,8 +1221,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
||||
return 0 <= column_index < len(self.columns)
|
||||
|
||||
def is_valid_coordinate(self, coordinate: Coordinate) -> bool:
|
||||
"""Return a boolean indicating whether the given coordinate is within table
|
||||
bounds.
|
||||
"""Return a boolean indicating whether the given coordinate is valid.
|
||||
|
||||
Args:
|
||||
coordinate: The coordinate to validate.
|
||||
|
||||
Reference in New Issue
Block a user