From 8b616e13bddc99c5f5980e5724978fc8da40b7de Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Mon, 18 Nov 2024 16:04:16 +0000 Subject: [PATCH] Ensure theme variables available on initial render (#5254) * Ensure theme variables are available immediately * Add snapshot test ensuring theme variables are available in code * Fix theme variables --- CHANGELOG.md | 6 + src/textual/app.py | 6 +- ...test_theme_variables_available_in_code.svg | 151 ++++++++++++++++++ tests/snapshot_tests/test_snapshots.py | 17 ++ 4 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 tests/snapshot_tests/__snapshots__/test_snapshots/test_theme_variables_available_in_code.svg 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 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())