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

Abierto
#6,783 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
40/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
frontend

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
37.7k
Forks
1.5k
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Textualize/textual

Todos los issues de Textualize/textual

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.