diff --git a/src/textual/widget.py b/src/textual/widget.py index fef4d011d7..482df3b3ad 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -1181,6 +1181,7 @@ def _get_style(self, style: VisualStyle | str) -> VisualStyle: """ if isinstance(style, VisualStyle): return style + visual_style = VisualStyle.null() if style.startswith("."): for node in self.ancestors_with_self: if not isinstance(node, Widget): @@ -1196,7 +1197,7 @@ def _get_style(self, style: VisualStyle | str) -> VisualStyle: try: visual_style = VisualStyle.parse(style) except Exception: - visual_style = VisualStyle.null() + pass return visual_style @overload @@ -2542,6 +2543,7 @@ def _scroll_to( force: bool = False, on_complete: CallbackType | None = None, level: AnimationLevel = "basic", + release_anchor: bool = True, ) -> bool: """Scroll to a given (absolute) coordinate, optionally animating. @@ -2555,10 +2557,13 @@ def _scroll_to( force: Force scrolling even when prohibited by overflow styling. on_complete: A callable to invoke when the animation is finished. level: Minimum level required for the animation to take place (inclusive). + release_anchor: If `True` call `release_anchor`. Returns: `True` if the scroll position changed, otherwise `False`. """ + if release_anchor: + self.release_anchor() maybe_scroll_x = x is not None and (self.allow_horizontal_scroll or force) maybe_scroll_y = y is not None and (self.allow_vertical_scroll or force) scrolled_x = scrolled_y = False @@ -2676,6 +2681,7 @@ def scroll_to( on_complete: CallbackType | None = None, level: AnimationLevel = "basic", immediate: bool = False, + release_anchor: bool = True, ) -> None: """Scroll to a given (absolute) coordinate, optionally animating. @@ -2691,10 +2697,13 @@ def scroll_to( level: Minimum level required for the animation to take place (inclusive). immediate: If `False` the scroll will be deferred until after a screen refresh, set to `True` to scroll immediately. + release_anchor: If `True` call `release_anchor`. Note: The call to scroll is made after the next refresh. """ + if release_anchor: + self.release_anchor() animator = self.app.animator if x is not None: animator.force_stop_animation(self, "scroll_x") @@ -2869,6 +2878,7 @@ def _lazily_scroll_end() -> None: force=force, on_complete=scroll_end_on_complete, level=level, + release_anchor=False, ) if self._anchored and self._anchor_released: @@ -4548,54 +4558,45 @@ def _on_blur(self, event: events.Blur) -> None: def _on_mouse_scroll_down(self, event: events.MouseScrollDown) -> None: if event.ctrl or event.shift: if self.allow_horizontal_scroll: - self.release_anchor() if self._scroll_right_for_pointer(animate=False): event.stop() else: if self.allow_vertical_scroll: - self.release_anchor() if self._scroll_down_for_pointer(animate=False): event.stop() def _on_mouse_scroll_up(self, event: events.MouseScrollUp) -> None: if event.ctrl or event.shift: if self.allow_horizontal_scroll: - self.release_anchor() if self._scroll_left_for_pointer(animate=False): event.stop() else: if self.allow_vertical_scroll: - self.release_anchor() if self._scroll_up_for_pointer(animate=False): event.stop() def _on_scroll_to(self, message: ScrollTo) -> None: if self._allow_scroll: - self.release_anchor() self.scroll_to(message.x, message.y, animate=message.animate, duration=0.1) message.stop() def _on_scroll_up(self, event: ScrollUp) -> None: if self.allow_vertical_scroll: - self.release_anchor() self.scroll_page_up() event.stop() def _on_scroll_down(self, event: ScrollDown) -> None: if self.allow_vertical_scroll: - self.release_anchor() self.scroll_page_down() event.stop() def _on_scroll_left(self, event: ScrollLeft) -> None: if self.allow_horizontal_scroll: - self.release_anchor() self.scroll_page_left() event.stop() def _on_scroll_right(self, event: ScrollRight) -> None: if self.allow_horizontal_scroll: - self.release_anchor() self.scroll_page_right() event.stop() @@ -4623,7 +4624,6 @@ def _on_unmount(self) -> None: def action_scroll_home(self) -> None: if not self._allow_scroll: raise SkipAction() - self.release_anchor() self.scroll_home(x_axis=self.scroll_y == 0) def action_scroll_end(self) -> None: @@ -4634,49 +4634,41 @@ def action_scroll_end(self) -> None: def action_scroll_left(self) -> None: if not self.allow_horizontal_scroll: raise SkipAction() - self.release_anchor() self.scroll_left() def action_scroll_right(self) -> None: if not self.allow_horizontal_scroll: raise SkipAction() - self.release_anchor() self.scroll_right() def action_scroll_up(self) -> None: if not self.allow_vertical_scroll: raise SkipAction() - self.release_anchor() self.scroll_up() def action_scroll_down(self) -> None: if not self.allow_vertical_scroll: raise SkipAction() - self.release_anchor() self.scroll_down() def action_page_down(self) -> None: if not self.allow_vertical_scroll: raise SkipAction() - self.release_anchor() self.scroll_page_down() def action_page_up(self) -> None: if not self.allow_vertical_scroll: raise SkipAction() - self.release_anchor() self.scroll_page_up() def action_page_left(self) -> None: if not self.allow_horizontal_scroll: raise SkipAction() - self.release_anchor() self.scroll_page_left() def action_page_right(self) -> None: if not self.allow_horizontal_scroll: raise SkipAction() - self.release_anchor() self.scroll_page_right() def notify( diff --git a/src/textual/widgets/_markdown.py b/src/textual/widgets/_markdown.py index d8889be375..d0bb90ed68 100644 --- a/src/textual/widgets/_markdown.py +++ b/src/textual/widgets/_markdown.py @@ -999,7 +999,8 @@ def table_of_contents(self) -> TableOfContentsType: if self._table_of_contents is None: self._table_of_contents = [ (header.LEVEL, header._content.plain, header.id) - for header in self.query_children(MarkdownHeader) + for header in self.children + if isinstance(header, MarkdownHeader) ] return self._table_of_contents @@ -1413,6 +1414,9 @@ async def await_append() -> None: break new_blocks = list(self._parse_markdown(tokens)) + any_headers = any( + isinstance(block, MarkdownHeader) for block in new_blocks + ) for block in new_blocks: start, end = block.source_range block.source_range = ( @@ -1434,7 +1438,7 @@ async def await_append() -> None: if new_blocks: await self.mount_all(new_blocks) - if any(isinstance(block, MarkdownHeader) for block in new_blocks): + if any_headers: self._table_of_contents = None self.post_message( Markdown.TableOfContentsUpdated(