Skip to content

Commit

Permalink
Make MarkdownFence respond to app theme changes. Closes Textualize#3997
Browse files Browse the repository at this point in the history
  • Loading branch information
ggozad committed Jan 30, 2024
1 parent 81808d9 commit 599feb0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 20 additions & 3 deletions src/textual/widgets/_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -500,19 +501,35 @@ 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,
lexer=self.lexer,
word_wrap=False,
indent_guides=True,
padding=(1, 2),
theme="material",
theme=self.theme,
),
expand=True,
shrink=False,
Expand Down

0 comments on commit 599feb0

Please sign in to comment.