From 8d83efe74b67bdead4af66b74385772cb594f79b Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Mon, 18 Nov 2024 12:00:28 +0000 Subject: [PATCH 1/3] Ensure theme variables are available immediately --- src/textual/app.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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"}: From 884fd535bcb5e323373ac383e2c4c755539bf745 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Mon, 18 Nov 2024 12:04:40 +0000 Subject: [PATCH 2/3] Add snapshot test ensuring theme variables are available in code --- ...test_theme_variables_available_in_code.svg | 151 ++++++++++++++++++ tests/snapshot_tests/test_snapshots.py | 17 ++ 2 files changed, 168 insertions(+) create mode 100644 tests/snapshot_tests/__snapshots__/test_snapshots/test_theme_variables_available_in_code.svg 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()) From 64d2175b4e6beb7823147d891826b42e8e982664 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Mon, 18 Nov 2024 14:38:15 +0000 Subject: [PATCH 3/3] Fix theme variables --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02cbb06664..5e94fa68a0 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 theme variables being unavailable in code until refresh_css was called https://github.com/Textualize/textual/pull/5254 + ## [0.86.1] - 2024-11-16 ### Fixed