Skip to content

Commit 8b616e1

Browse files
authored
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
1 parent 86fc105 commit 8b616e1

File tree

4 files changed

+177
-3
lines changed

4 files changed

+177
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [Unreleased]
9+
10+
### Fixed
11+
12+
- Fixed theme variables being unavailable in code until refresh_css was called https://github.com/Textualize/textual/pull/5254
13+
814
## [0.86.1] - 2024-11-16
915

1016
### Fixed

src/textual/app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ def __init__(
636636

637637
self._css_has_errors = False
638638

639+
self.theme_variables: dict[str, str] = {}
640+
"""Variables generated from the current theme."""
641+
639642
# Note that the theme must be set *before* self.get_css_variables() is called
640643
# to ensure that the variables are retrieved from the currently active theme.
641644
self.stylesheet = Stylesheet(variables=self.get_css_variables())
@@ -765,9 +768,6 @@ def __init__(
765768
self._css_update_count: int = 0
766769
"""Incremented when CSS is invalidated."""
767770

768-
self.theme_variables: dict[str, str] = {}
769-
"""Variables generated from the current theme."""
770-
771771
if self.ENABLE_COMMAND_PALETTE:
772772
for _key, binding in self._bindings:
773773
if binding.action in {"command_palette", "app.command_palette"}:
Lines changed: 151 additions & 0 deletions
Loading

tests/snapshot_tests/test_snapshots.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,3 +2571,20 @@ def action_remove_foo(self) -> None:
25712571

25722572
app = TabsApp()
25732573
assert snap_compare(app, press="r")
2574+
2575+
2576+
def test_theme_variables_available_in_code(snap_compare):
2577+
"""Test that theme variables are available in code."""
2578+
2579+
class ThemeVariablesApp(App):
2580+
def compose(self) -> ComposeResult:
2581+
yield Label("Hello")
2582+
2583+
def on_mount(self) -> None:
2584+
variables = self.theme_variables
2585+
label = self.query_one(Label)
2586+
label.update(f"$text-primary = {variables['text-primary']}")
2587+
label.styles.background = variables["primary-muted"]
2588+
label.styles.color = variables["text-primary"]
2589+
2590+
assert snap_compare(ThemeVariablesApp())

0 commit comments

Comments
 (0)