Fix container anchor re-engagement during continuous content streaming - #6694
Open
Jyotish08 wants to merge 2 commits into
Open
Fix container anchor re-engagement during continuous content streaming#6694Jyotish08 wants to merge 2 commits into
Jyotish08 wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts Textual’s anchoring behavior for scrollable containers so that anchors can be re-engaged reliably while content is continuously appended (streaming/mounting), and adds a regression test to cover the reported flow.
Changes:
- Update
Widget._check_anchor()to allow re-engagement based onscroll_target_yas well asscroll_y. - Prevent “scroll-to-bottom” operations from releasing the anchor, and ensure anchor state is restored when targeting the bottom.
- Call
_check_anchor()during compositor layout before applying anchored “glue to bottom” behavior; add a streaming regression test.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
tests/test_anchor.py |
Adds regression coverage for anchor release + re-engagement during incremental mounting. |
src/textual/widget.py |
Updates anchor restoration criteria and modifies scroll methods to preserve anchoring when targeting the bottom. |
src/textual/_compositor.py |
Ensures anchor state is re-checked during layout before applying anchored scroll positioning. |
Suppressed comments (1)
src/textual/widget.py:2909
- Same as
_scroll_to: therelease_anchorparameter docs here currently imply anchor release happens wheneverrelease_anchor=True, but this method now intentionally preserves anchoring when scrolling to/beyondmax_scroll_y. Please update the docstring line so it matches the new semantics.
if release_anchor:
if y is not None and y >= self.max_scroll_y:
release_anchor = False
if self._anchored:
self._anchor_released = False
else:
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+609
to
612
| widget._check_anchor() | ||
| if widget._anchored and not widget._anchor_released: | ||
| new_scroll_y = ( | ||
| arrange_result.spatial_map.total_region.bottom |
Comment on lines
2753
to
+2757
| if release_anchor: | ||
| self.release_anchor() | ||
| if y is not None and y >= self.max_scroll_y: | ||
| release_anchor = False | ||
| if self._anchored: | ||
| self._anchor_released = False |
Remove �nd not widget.screen._selecting condition in Visual.to_strips so link color and underline styles remain active when widgets re-render during or after text selection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #6677 Problem
When Widget.anchor() is active on a scrollable container (e.g. VerticalScroll), any call to scroll_to(), scroll_relative(), scroll_by(), or scrollbar interaction sets release_anchor=True, placing the widget into _anchor_released = True.
While content is actively streaming (e.g. mounting child widgets continuously), max_scroll_y grows every frame. Re-engaging the anchor via _check_anchor() required scroll_y >= max_scroll_y exactly. Because max_scroll_y increases continuously while content is added, scroll_y lags behind max_scroll_y by at least 1 unit during scroll events, rendering exact equality unreachable. Consequently, once released, an anchored container would permanently remain unanchored and fail to follow new content even after scrolling back to the bottom.
Solution
Target position anchor restoration: Updated _check_anchor() in widget.py to evaluate self.scroll_target_y >= self.max_scroll_y alongside self.scroll_y >= self.max_scroll_y.
Bottom scroll anchor protection: In scroll_to() and _scroll_to(), when y >= self.max_scroll_y, release_anchor is suppressed and _anchor_released is reset to False (self._anchor_released = False), as scrolling to or beyond the bottom indicates an intent to stay at the bottom edge.
Compositor anchor check: In _compositor.py, widget._check_anchor() is now invoked immediately before evaluating if widget._anchored and not widget._anchor_released:, ensuring _anchor_released is cleared before bottom-gluing calculations occur during layout rendering.
Testing
Added regression test test_anchor_streaming_repro_flow in tests/test_anchor.py verifying that:
Scrolling away (y=-1) releases the anchor and keeps scroll_y frozen while items mount.
Scrolling back to the bottom (v.scroll_to(y=v.max_scroll_y)) restores the anchor and resumes gluing the viewport to new content as items mount.
Verified all core unit tests pass (pytest tests/test_widget.py tests/test_containers.py tests/test_auto_scroll.py tests/test_anchor.py).