Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure theme variables available on initial render #5254

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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"}:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Loading