Skip to content

Commit d935c03

Browse files
authored
Merge pull request #5993 from Textualize/anchor-header
release anchor
2 parents e3bae00 + dd5b3ab commit d935c03

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

src/textual/widget.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,7 @@ def _get_style(self, style: VisualStyle | str) -> VisualStyle:
11811181
"""
11821182
if isinstance(style, VisualStyle):
11831183
return style
1184+
visual_style = VisualStyle.null()
11841185
if style.startswith("."):
11851186
for node in self.ancestors_with_self:
11861187
if not isinstance(node, Widget):
@@ -1196,7 +1197,7 @@ def _get_style(self, style: VisualStyle | str) -> VisualStyle:
11961197
try:
11971198
visual_style = VisualStyle.parse(style)
11981199
except Exception:
1199-
visual_style = VisualStyle.null()
1200+
pass
12001201
return visual_style
12011202

12021203
@overload
@@ -2542,6 +2543,7 @@ def _scroll_to(
25422543
force: bool = False,
25432544
on_complete: CallbackType | None = None,
25442545
level: AnimationLevel = "basic",
2546+
release_anchor: bool = True,
25452547
) -> bool:
25462548
"""Scroll to a given (absolute) coordinate, optionally animating.
25472549
@@ -2555,10 +2557,13 @@ def _scroll_to(
25552557
force: Force scrolling even when prohibited by overflow styling.
25562558
on_complete: A callable to invoke when the animation is finished.
25572559
level: Minimum level required for the animation to take place (inclusive).
2560+
release_anchor: If `True` call `release_anchor`.
25582561
25592562
Returns:
25602563
`True` if the scroll position changed, otherwise `False`.
25612564
"""
2565+
if release_anchor:
2566+
self.release_anchor()
25622567
maybe_scroll_x = x is not None and (self.allow_horizontal_scroll or force)
25632568
maybe_scroll_y = y is not None and (self.allow_vertical_scroll or force)
25642569
scrolled_x = scrolled_y = False
@@ -2676,6 +2681,7 @@ def scroll_to(
26762681
on_complete: CallbackType | None = None,
26772682
level: AnimationLevel = "basic",
26782683
immediate: bool = False,
2684+
release_anchor: bool = True,
26792685
) -> None:
26802686
"""Scroll to a given (absolute) coordinate, optionally animating.
26812687
@@ -2691,10 +2697,13 @@ def scroll_to(
26912697
level: Minimum level required for the animation to take place (inclusive).
26922698
immediate: If `False` the scroll will be deferred until after a screen refresh,
26932699
set to `True` to scroll immediately.
2700+
release_anchor: If `True` call `release_anchor`.
26942701
26952702
Note:
26962703
The call to scroll is made after the next refresh.
26972704
"""
2705+
if release_anchor:
2706+
self.release_anchor()
26982707
animator = self.app.animator
26992708
if x is not None:
27002709
animator.force_stop_animation(self, "scroll_x")
@@ -2869,6 +2878,7 @@ def _lazily_scroll_end() -> None:
28692878
force=force,
28702879
on_complete=scroll_end_on_complete,
28712880
level=level,
2881+
release_anchor=False,
28722882
)
28732883

28742884
if self._anchored and self._anchor_released:
@@ -4548,54 +4558,45 @@ def _on_blur(self, event: events.Blur) -> None:
45484558
def _on_mouse_scroll_down(self, event: events.MouseScrollDown) -> None:
45494559
if event.ctrl or event.shift:
45504560
if self.allow_horizontal_scroll:
4551-
self.release_anchor()
45524561
if self._scroll_right_for_pointer(animate=False):
45534562
event.stop()
45544563
else:
45554564
if self.allow_vertical_scroll:
4556-
self.release_anchor()
45574565
if self._scroll_down_for_pointer(animate=False):
45584566
event.stop()
45594567

45604568
def _on_mouse_scroll_up(self, event: events.MouseScrollUp) -> None:
45614569
if event.ctrl or event.shift:
45624570
if self.allow_horizontal_scroll:
4563-
self.release_anchor()
45644571
if self._scroll_left_for_pointer(animate=False):
45654572
event.stop()
45664573
else:
45674574
if self.allow_vertical_scroll:
4568-
self.release_anchor()
45694575
if self._scroll_up_for_pointer(animate=False):
45704576
event.stop()
45714577

45724578
def _on_scroll_to(self, message: ScrollTo) -> None:
45734579
if self._allow_scroll:
4574-
self.release_anchor()
45754580
self.scroll_to(message.x, message.y, animate=message.animate, duration=0.1)
45764581
message.stop()
45774582

45784583
def _on_scroll_up(self, event: ScrollUp) -> None:
45794584
if self.allow_vertical_scroll:
4580-
self.release_anchor()
45814585
self.scroll_page_up()
45824586
event.stop()
45834587

45844588
def _on_scroll_down(self, event: ScrollDown) -> None:
45854589
if self.allow_vertical_scroll:
4586-
self.release_anchor()
45874590
self.scroll_page_down()
45884591
event.stop()
45894592

45904593
def _on_scroll_left(self, event: ScrollLeft) -> None:
45914594
if self.allow_horizontal_scroll:
4592-
self.release_anchor()
45934595
self.scroll_page_left()
45944596
event.stop()
45954597

45964598
def _on_scroll_right(self, event: ScrollRight) -> None:
45974599
if self.allow_horizontal_scroll:
4598-
self.release_anchor()
45994600
self.scroll_page_right()
46004601
event.stop()
46014602

@@ -4623,7 +4624,6 @@ def _on_unmount(self) -> None:
46234624
def action_scroll_home(self) -> None:
46244625
if not self._allow_scroll:
46254626
raise SkipAction()
4626-
self.release_anchor()
46274627
self.scroll_home(x_axis=self.scroll_y == 0)
46284628

46294629
def action_scroll_end(self) -> None:
@@ -4634,49 +4634,41 @@ def action_scroll_end(self) -> None:
46344634
def action_scroll_left(self) -> None:
46354635
if not self.allow_horizontal_scroll:
46364636
raise SkipAction()
4637-
self.release_anchor()
46384637
self.scroll_left()
46394638

46404639
def action_scroll_right(self) -> None:
46414640
if not self.allow_horizontal_scroll:
46424641
raise SkipAction()
4643-
self.release_anchor()
46444642
self.scroll_right()
46454643

46464644
def action_scroll_up(self) -> None:
46474645
if not self.allow_vertical_scroll:
46484646
raise SkipAction()
4649-
self.release_anchor()
46504647
self.scroll_up()
46514648

46524649
def action_scroll_down(self) -> None:
46534650
if not self.allow_vertical_scroll:
46544651
raise SkipAction()
4655-
self.release_anchor()
46564652
self.scroll_down()
46574653

46584654
def action_page_down(self) -> None:
46594655
if not self.allow_vertical_scroll:
46604656
raise SkipAction()
4661-
self.release_anchor()
46624657
self.scroll_page_down()
46634658

46644659
def action_page_up(self) -> None:
46654660
if not self.allow_vertical_scroll:
46664661
raise SkipAction()
4667-
self.release_anchor()
46684662
self.scroll_page_up()
46694663

46704664
def action_page_left(self) -> None:
46714665
if not self.allow_horizontal_scroll:
46724666
raise SkipAction()
4673-
self.release_anchor()
46744667
self.scroll_page_left()
46754668

46764669
def action_page_right(self) -> None:
46774670
if not self.allow_horizontal_scroll:
46784671
raise SkipAction()
4679-
self.release_anchor()
46804672
self.scroll_page_right()
46814673

46824674
def notify(

src/textual/widgets/_markdown.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,8 @@ def table_of_contents(self) -> TableOfContentsType:
999999
if self._table_of_contents is None:
10001000
self._table_of_contents = [
10011001
(header.LEVEL, header._content.plain, header.id)
1002-
for header in self.query_children(MarkdownHeader)
1002+
for header in self.children
1003+
if isinstance(header, MarkdownHeader)
10031004
]
10041005
return self._table_of_contents
10051006

@@ -1413,6 +1414,9 @@ async def await_append() -> None:
14131414
break
14141415

14151416
new_blocks = list(self._parse_markdown(tokens))
1417+
any_headers = any(
1418+
isinstance(block, MarkdownHeader) for block in new_blocks
1419+
)
14161420
for block in new_blocks:
14171421
start, end = block.source_range
14181422
block.source_range = (
@@ -1434,7 +1438,7 @@ async def await_append() -> None:
14341438
if new_blocks:
14351439
await self.mount_all(new_blocks)
14361440

1437-
if any(isinstance(block, MarkdownHeader) for block in new_blocks):
1441+
if any_headers:
14381442
self._table_of_contents = None
14391443
self.post_message(
14401444
Markdown.TableOfContentsUpdated(

0 commit comments

Comments
 (0)