diff --git a/CHANGELOG.md b/CHANGELOG.md index 858b1c0819..649b368099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,14 @@ 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/). + ## [0.86.2] - Unreleased ### Fixed - Fixed visibility glitch for widgets with an offset https://github.com/Textualize/textual/pull/5253 +- Fixed theme variables being unavailable in code until refresh_css was called https://github.com/Textualize/textual/pull/5254 + ## [0.86.1] - 2024-11-16 diff --git a/src/textual/app.py b/src/textual/app.py index 1ac01a85f4..e94c9595b0 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -636,6 +636,9 @@ def __init__( self._css_has_errors = False + self.theme_variables: dict[str, str] = {} + """Variables generated from the current theme.""" + # Note that the theme must be set *before* self.get_css_variables() is called # to ensure that the variables are retrieved from the currently active theme. self.stylesheet = Stylesheet(variables=self.get_css_variables()) @@ -765,9 +768,6 @@ def __init__( self._css_update_count: int = 0 """Incremented when CSS is invalidated.""" - self.theme_variables: dict[str, str] = {} - """Variables generated from the current theme.""" - if self.ENABLE_COMMAND_PALETTE: for _key, binding in self._bindings: if binding.action in {"command_palette", "app.command_palette"}: diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots/test_theme_variables_available_in_code.svg b/tests/snapshot_tests/__snapshots__/test_snapshots/test_theme_variables_available_in_code.svg new file mode 100644 index 0000000000..d1428874a0 --- /dev/null +++ b/tests/snapshot_tests/__snapshots__/test_snapshots/test_theme_variables_available_in_code.svg @@ -0,0 +1,151 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ThemeVariablesApp + + + + + + + + + + $text-primary = #57A5E2 + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 1b94057834..f952c1f3d6 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -2571,3 +2571,20 @@ def action_remove_foo(self) -> None: app = TabsApp() assert snap_compare(app, press="r") + + +def test_theme_variables_available_in_code(snap_compare): + """Test that theme variables are available in code.""" + + class ThemeVariablesApp(App): + def compose(self) -> ComposeResult: + yield Label("Hello") + + def on_mount(self) -> None: + variables = self.theme_variables + label = self.query_one(Label) + label.update(f"$text-primary = {variables['text-primary']}") + label.styles.background = variables["primary-muted"] + label.styles.color = variables["text-primary"] + + assert snap_compare(ThemeVariablesApp())