Footer recomposes twice for each focus change

オープン
#6,791 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
52/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
python
領域
frontend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
37.7k
フォーク
1.5k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Textualize/textual のほかの issue

Textualize/textual の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。