diff --git a/CHANGELOG.md b/CHANGELOG.md index 705eb5dd3e..00dd0c6a23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Ensuring `TextArea.SelectionChanged` message only sends when the updated selection is different https://github.com/Textualize/textual/pull/3933 - Fixed declaration after nested rule set causing a parse error https://github.com/Textualize/textual/pull/4012 - ID and class validation was too lenient https://github.com/Textualize/textual/issues/3954 +- Fixed `MarkdownFence` not adapting to app theme changes https://github.com/Textualize/textual/issues/3997 ## [0.47.1] - 2023-01-05 diff --git a/src/textual/widgets/_markdown.py b/src/textual/widgets/_markdown.py index f8ac0fdaff..cdfd5113df 100644 --- a/src/textual/widgets/_markdown.py +++ b/src/textual/widgets/_markdown.py @@ -7,6 +7,7 @@ from markdown_it.token import Token from rich import box from rich.style import Style +from rich.syntax import Syntax from rich.table import Table from rich.text import Text from typing_extensions import TypeAlias @@ -500,11 +501,27 @@ class MarkdownFence(MarkdownBlock): def __init__(self, markdown: Markdown, code: str, lexer: str) -> None: self.code = code self.lexer = lexer + self.theme = "solarized-dark" if self.app.dark else "solarized-light" super().__init__(markdown) - def compose(self) -> ComposeResult: - from rich.syntax import Syntax + def _retheme(self) -> None: + """Swap between a dark and light theme when the mode changes.""" + self.theme = "solarized-dark" if self.app.dark else "solarized-light" + code_block = self.query_one(Static) + code_block.renderable = Syntax( + self.code, + lexer=self.lexer, + word_wrap=False, + indent_guides=True, + padding=(1, 2), + theme=self.theme, + ) + def _on_mount(self, _: Mount) -> None: + """Watch app theme switching.""" + self.watch(self.app, "dark", self._retheme) + + def compose(self) -> ComposeResult: yield Static( Syntax( self.code, @@ -512,7 +529,7 @@ def compose(self) -> ComposeResult: word_wrap=False, indent_guides=True, padding=(1, 2), - theme="material", + theme=self.theme, ), expand=True, shrink=False,