Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datatable style ordering #2736

Merged
merged 6 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- The devtools console now confirms when CSS files have been successfully loaded after a previous error https://github.com/Textualize/textual/pull/2716
- Added `cursor_foreground_priority` and `cursor_background_priority` to `DataTable` https://github.com/Textualize/textual/pull/2736

### Fixed

Expand Down
42 changes: 27 additions & 15 deletions src/textual/widgets/_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ def __init__(
zebra_stripes: bool = False,
header_height: int = 1,
show_cursor: bool = True,
cursor_foreground_priority: Literal["renderable", "css"] = "css",
cursor_background_priority: Literal["renderable", "css"] = "renderable",
name: str | None = None,
id: str | None = None,
classes: str | None = None,
Expand Down Expand Up @@ -660,6 +662,12 @@ def __init__(
"""Apply zebra effect on row backgrounds (light, dark, light, dark, ...)."""
self.show_cursor = show_cursor
"""Show/hide both the keyboard and hover cursor."""
self.cursor_foreground_priority = cursor_foreground_priority
"""Should we prioritize the cursor component class CSS foreground or the renderable foreground
in the event where a cell contains a renderable with a foreground color."""
self.cursor_background_priority = cursor_background_priority
"""Should we prioritize the cursor component class CSS background or the renderable background
in the event where a cell contains a renderable with a background color."""

@property
def hover_row(self) -> int:
Expand Down Expand Up @@ -1651,38 +1659,42 @@ def _render_cell(
else:
cell = row_cells[column_index]

get_component = self.get_component_styles
get_component = self.get_component_rich_style
show_cursor = self.show_cursor

component_style = Style()

if hover and show_cursor and self._show_hover_cursor:
component_style += get_component("datatable--hover").rich_style
component_style += get_component("datatable--hover")
if is_header_cell or is_row_label_cell:
# Apply subtle variation in style for the header/label (blue background by
# default) rows and columns affected by the cursor, to ensure we can
# still differentiate between the labels and the data.
component_style += get_component(
"datatable--header-hover"
).rich_style
component_style += get_component("datatable--header-hover")

if cursor and show_cursor:
cursor_style = get_component("datatable--cursor").rich_style
cursor_style = get_component("datatable--cursor")
component_style += cursor_style
if is_header_cell or is_row_label_cell:
component_style += get_component(
"datatable--header-cursor"
).rich_style
component_style += get_component("datatable--header-cursor")
elif is_fixed_style_cell:
component_style += get_component(
"datatable--fixed-cursor"
).rich_style
component_style += get_component("datatable--fixed-cursor")

post_foreground = (
Style.from_color(color=component_style.color)
if self.cursor_foreground_priority == "css"
else Style.null()
)
post_background = (
Style.from_color(bgcolor=component_style.bgcolor)
if self.cursor_background_priority == "css"
else Style.null()
)

lines = self.app.console.render_lines(
Styled(
Padding(cell, (0, 1)),
pre_style=base_style,
post_style=component_style,
pre_style=base_style + component_style,
post_style=post_foreground + post_background,
),
self.app.console.options.update_dimensions(width, height),
)
Expand Down
Loading