Footer recomposes twice for each focus change

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

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
frontend

Direzione di ricerca

Start by tracing Screen.set_focus, Screen._watch_focused, and Footer.bindings_changed to confirm when each bindings_updated_signal is published. Run the provided run_test reproduction and check whether the deferred refresh is needed after focus handling. Done means the intended recompose behavior is established and covered by a regression test.

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

Descrizione

Hi! I noticed this while tracing messages in tests: each focus change makes the Footer recompose twice.

Screen.set_focus sets self.focused, and Screen._watch_focused calls refresh_bindings(). Then, at the end of set_focus, self.call_after_refresh(self.refresh_bindings) publishes bindings_updated_signal a second time. Footer.bindings_changed answers each one with call_after_refresh(self.recompose), so the Footer rebuilds all its keys twice.

import asyncio
import collections
import sys

import textual
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.widgets import Button, Footer

class CountingFooter(Footer):
    recomposes = 0

    async def recompose(self) -> None:
        CountingFooter.recomposes += 1
        await super().recompose()

class FirstButton(Button):
    BINDINGS = [Binding("f", "first", "First")]

class SecondButton(Button):
    BINDINGS = [Binding("s", "second", "Second")]

class FooterApp(App):
    def compose(self) -> ComposeResult:
        yield FirstButton("one")
        yield SecondButton("two")
        yield CountingFooter()

async def once() -> int:
    app = FooterApp()
    async with app.run_test() as pilot:
        await pilot.pause(0.2)
        CountingFooter.recomposes = 0
        await pilot.press("tab")
        await pilot.pause(0.2)
        return CountingFooter.recomposes

async def main() -> None:
    runs = int(sys.argv[1]) if len(sys.argv) > 1 else 20
    tally = collections.Counter()
    for _ in range(runs):
        tally[await once()] += 1
    print(f"Textual {textual.__version__}, {runs} runs")
    print("Footer recomposes after one Tab -> runs:", dict(sorted(tally.items())))

asyncio.run(main())

Output on main (5e5b7ef), Python 3.13, macOS (8.2.7 is the same):

Textual 8.2.8, 30 runs
Footer recomposes after one Tab -> runs: {2: 30}

I expected one recompose per focus change. The two requests come from different points in time (one when focused changes, one after the next refresh), so I don't think the Footer can simply coalesce them. Is the deferred refresh_bindings in set_focus still needed now that _watch_focused refreshes bindings? If it's there so that bindings are checked again after the newly focused widget has handled Focus, that would explain it, and the double recompose may just be the cost of that. I wanted to check before suggesting a change.

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.