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

Aperta
#6,783 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
40/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
frontend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
37.7k
Fork
1.5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/textual

Tutte le issue di Textualize/textual

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.