diff --git a/CHANGELOG.md b/CHANGELOG.md index b14cf83bbc..9b3c5682c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Fixed mouse targeting issue in `TextArea` when tabs were not fully expanded https://github.com/Textualize/textual/pull/3725 +- Fixed flicker when updating Markdown https://github.com/Textualize/textual/pull/3757 ### Added diff --git a/src/textual/screen.py b/src/textual/screen.py index 4387e62b44..25fc02404e 100644 --- a/src/textual/screen.py +++ b/src/textual/screen.py @@ -645,7 +645,7 @@ def _compositor_refresh(self) -> None: def _on_timer_update(self) -> None: """Called by the _update_timer.""" self._update_timer.pause() - if self.is_current: + if self.is_current and not self.app._batch_count: if self._layout_required: self._refresh_layout() self._layout_required = False diff --git a/src/textual/widgets/_markdown.py b/src/textual/widgets/_markdown.py index fa22634f8c..d820ccc723 100644 --- a/src/textual/widgets/_markdown.py +++ b/src/textual/widgets/_markdown.py @@ -13,11 +13,12 @@ from .._slug import TrackedSlugs from ..app import ComposeResult +from ..await_complete import AwaitComplete from ..containers import Horizontal, Vertical, VerticalScroll from ..events import Mount from ..message import Message from ..reactive import reactive, var -from ..widget import AwaitMount, Widget +from ..widget import Widget from ..widgets import Static, Tree TableOfContentsType: TypeAlias = "list[tuple[int, str, str | None]]" @@ -721,7 +722,7 @@ def unhandled_token(self, token: Token) -> MarkdownBlock | None: """ return None - def update(self, markdown: str) -> AwaitMount: + def update(self, markdown: str) -> AwaitComplete: """Update the document with new Markdown. Args: @@ -871,9 +872,16 @@ def update(self, markdown: str) -> AwaitMount: self.post_message( Markdown.TableOfContentsUpdated(self, self._table_of_contents) ) - with self.app.batch_update(): - self.query("MarkdownBlock").remove() - return self.mount_all(output) + markdown_block = self.query("MarkdownBlock") + + async def await_update() -> None: + """Update in a single batch.""" + + with self.app.batch_update(): + await markdown_block.remove() + await self.mount_all(output) + + return AwaitComplete(await_update()) class MarkdownTableOfContents(Widget, can_focus_children=True):