Skip to content

Commit

Permalink
Fix #3342: consider text opacity in partial rich styles.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigogiraoserrao committed Oct 4, 2023
1 parent de6a479 commit 247ed83
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
24 changes: 18 additions & 6 deletions src/textual/css/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,12 +627,24 @@ def partial_rich_style(self) -> Style:
Returns:
Rich Style object.
"""
style = Style(
color=(self.color.rich_color if self.has_rule("color") else None),
bgcolor=(
self.background.rich_color if self.has_rule("background") else None
),
)
if self.has_rule("color"):
# If there is text opacity, we need to get the background color from the
# parents to compute the correct final text color.
if self.has_rule("text_opacity") and self.text_opacity < 1:
reference_background = (
self.node.background_colors[1]
if self.node is not None
else self.background
)
color = (
reference_background + self.color.multiply_alpha(self.text_opacity)
).rich_color
else:
color = self.color.rich_color
else:
color = None
bgcolor = self.background.rich_color if self.has_rule("background") else None
style = Style(color=color, bgcolor=bgcolor)
style += self.text_style
return style

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ComponentClassesOpacity(App[None]):
Footer > .footer--key,
Input > .input--placeholder,
OptionList > .option-list--option-highlighted,
ProgressBar Bar > .bar--indeterminate,
ProgressBar Bar > .bar--bar,
RadioButton > .toggle--label,
SelectionList > .selection-list--button-highlighted,
Switch > .switch--slider,
Expand All @@ -50,7 +50,9 @@ def compose(self) -> ComposeResult:
yield Footer()
yield Input(placeholder="this should be invisible")
yield OptionList(Option("this should be invisible"))
yield ProgressBar()
pb = ProgressBar(total=100)
pb.advance(50)
yield pb
yield RadioButton("this should be invisible")
yield SelectionList(("this should be invisible", 0))
yield Switch()
Expand Down
26 changes: 22 additions & 4 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,9 @@ def test_nested_fr(snap_compare) -> None:
assert snap_compare(SNAPSHOT_APPS_DIR / "nested_fr.py")


@pytest.mark.skipif(sys.version_info < (3, 8), reason="tree-sitter requires python3.8 or higher")
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="tree-sitter requires python3.8 or higher"
)
@pytest.mark.parametrize("language", BUILTIN_LANGUAGES)
def test_text_area_language_rendering(language, snap_compare):
# This test will fail if we're missing a snapshot test for a valid
Expand Down Expand Up @@ -760,9 +762,12 @@ def setup_selection(pilot):
)


@pytest.mark.skipif(sys.version_info < (3, 8), reason="tree-sitter requires python3.8 or higher")
@pytest.mark.parametrize("theme_name",
[theme.name for theme in TextAreaTheme.builtin_themes()])
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="tree-sitter requires python3.8 or higher"
)
@pytest.mark.parametrize(
"theme_name", [theme.name for theme in TextAreaTheme.builtin_themes()]
)
def test_text_area_themes(snap_compare, theme_name):
"""Each theme should have its own snapshot with at least some Python
to check that the rendering is sensible. This also ensures that theme
Expand Down Expand Up @@ -807,3 +812,16 @@ def test_scoped_css(snap_compare) -> None:

def test_unscoped_css(snap_compare) -> None:
assert snap_compare(SNAPSHOT_APPS_DIR / "unscoped_css.py")


def test_component_classes_opacity(snap_compare) -> None:
"""Regression test for
- https://github.com/Textualize/textual/issues/3304
- https://github.com/Textualize/textual/issues/3342
- https://github.com/Textualize/textual/issues/3413
"""
# We _need_ a height of at least 27 to show all the widgets that we're testing.
assert snap_compare(
SNAPSHOT_APPS_DIR / "component_classes_opacity.py", terminal_size=(80, 30)
)

0 comments on commit 247ed83

Please sign in to comment.