diff --git a/CHANGELOG.md b/CHANGELOG.md index 19b78b6104..63bf7dd9df 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 + +### Fixed + +- Fixed issue with the "transparent" CSS value not being transparent when set using python + ## [3.5.0] - 2025-06-20 ### Changed diff --git a/src/textual/css/_style_properties.py b/src/textual/css/_style_properties.py index 472e5fd26c..7860eb32e0 100644 --- a/src/textual/css/_style_properties.py +++ b/src/textual/css/_style_properties.py @@ -991,7 +991,7 @@ def __set__(self, obj: StylesBase, color: Color | str | None) -> None: self.name, context="inline", error=error, value=token ), ) - parsed_color = parsed_color.with_alpha(alpha) + parsed_color = parsed_color.multiply_alpha(alpha) if obj.set_rule(self.name, parsed_color): obj.refresh(children=True) diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots/test_setting_transparency.svg b/tests/snapshot_tests/__snapshots__/test_snapshots/test_setting_transparency.svg new file mode 100644 index 0000000000..9067444ae6 --- /dev/null +++ b/tests/snapshot_tests/__snapshots__/test_snapshots/test_setting_transparency.svg @@ -0,0 +1,154 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + TransparentPythonApp + + + + + + + + + + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ +Baseline normal TextArea, not transparent      + + +▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ +▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▎ +This TextArea made transparent by adding a     +CSS class                                      + +▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▎ +▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▎ +This TextArea made transparent by setting      +style with python                              + +▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▎ +▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▎ +1) This is an OptionList + +2) With a transparent background + +3) Made transparent by setting style with +python +▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▎ + + + + diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 5b9db68929..fea3b0c5a4 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -3540,6 +3540,55 @@ def compose(self) -> ComposeResult: assert snap_compare(FocusWithinTransparentApp(), press=["tab"]) + +def test_setting_transparency(snap_compare): + """Check that setting a widget's background color to transparent + works as expected using python. + Regression test for https://github.com/Textualize/textual/pull/5890""" + + class TransparentPythonApp(App): + + CSS = """ + Screen { + background: darkblue; + align: center middle; + } + + TextArea { + height: 5; + width: 50; + &.-transparent { background: transparent; } + } + + OptionList { + height: 8; + width: 50; + } + """ + + def compose(self): + + yield TextArea("Baseline normal TextArea, not transparent") + text_area2 = TextArea( + "This TextArea made transparent by adding a CSS class", classes="-transparent" + ) + yield text_area2 + text_area3 = TextArea( + "This TextArea made transparent by setting style with python" + ) + text_area3.styles.background = "transparent" + yield text_area3 + option_list = OptionList( + "1) This is an OptionList\n", + "2) With a transparent background\n", + "3) Made transparent by setting style with python", + ) + option_list.styles.background = "transparent" + yield option_list + + assert snap_compare(TransparentPythonApp()) + + def test_option_list_wrapping(snap_compare): """You should see a 40 cell wide Option list with a single line, ending in an ellipsis."""