A new widget gets no Resize or Show if its region is read before its first layout (after a scroll)

Open
#6,783 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
40/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
frontend

Research direction

Start in Compositor.reflow and Compositor.reflow_visible, and read the full_map property where the map is rebuilt lazily. The bug is that reflow computes shown_widgets against a map that a region read has already rebuilt, so a new widget is never counted as shown. Run the repro script with and without probe.region; done means both runs report the same Resize and Show events.

Written by the indexing model from the issue text.

Description

Hi! I hit this while writing generated tests against Textual's widgets. A Placeholder(variant="size") sometimes showed no size text at all, and I traced it to the compositor.

After a scroll, Compositor.reflow_visible marks the full map invalid, and the full_map property rebuilds it lazily the next time something asks for a widget's region. If that happens for a newly mounted widget before the next layout, the rebuilt map already contains it. Compositor.reflow then compares the new layout against that map (shown_widgets = new_widgets - old_widgets), so the widget doesn't count as shown, its size hasn't changed, and it never gets a Resize or a Show.

Repro with a plain widget that records its events:

import asyncio

import textual
from textual import events
from textual.app import App, ComposeResult
from textual.containers import VerticalScroll
from textual.widgets import Static

class Probe(Static):
    def __init__(self) -> None:
        super().__init__("probe")
        self.seen: list[str] = []

    def on_resize(self, event: events.Resize) -> None:
        self.seen.append(f"Resize{tuple(event.size)}")

    def on_show(self) -> None:
        self.seen.append("Show")

class ProbeApp(App):
    CSS = "Static { height: 5; }"

    def compose(self) -> ComposeResult:
        with VerticalScroll():
            for n in range(5):
                yield Static(f"line {n}")

async def run(read_region: bool) -> None:
    app = ProbeApp()
    async with app.run_test(size=(40, 12)) as pilot:
        scroll = app.query_one(VerticalScroll)
        scroll.scroll_to(y=2, animate=False)
        await pilot.pause()
        probe = Probe()
        await scroll.mount(probe)
        if read_region:
            probe.region
        await pilot.pause()
        await pilot.pause()
        print(f"read region before layout={read_region}: events {probe.seen}")

async def main() -> None:
    await run(False)
    await run(True)
    print("Textual", textual.__version__)

asyncio.run(main())

Output on Textual 8.2.7 (and the same on main at 5e5b7ef):

read region before layout=False: events ['Resize(38, 5)', 'Show']
read region before layout=True: events []

The only difference between the two runs is reading probe.region once before the layout. In a real app the read can come from anything that calls find_widget, such as scroll_visible, widget.region in a handler, or a worker finishing at the wrong moment, so whether a widget gets its events depends on timing. Widgets that set state in on_resize show it: here, a Placeholder(variant="size") mounted the same way stays blank:

size text: '' actual size: (38, 5)

(without the region read it shows [b]38 x 5[/b]). Tree._on_resize is skipped the same way, if a DirectoryTree load finishes between a scroll and the next layout.

I expected the result not to depend on whether something read the region first. My suggested fix is for reflow to compare against the map from the last reflow, rather than whatever full_map was last rebuilt to. I have that as a PR with a test, but it's in the compositor, so I'm happy to take a different approach if you'd rather.

Dominant language
Python
Stars
37.7k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/textual

All issues in Textualize/textual

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.