Textualize / textual

The lean application framework for Python. Build sophisticated user interfaces with a simple Python API. Run your apps in the terminal and a web browser.
https://textual.textualize.io/
MIT License
25.56k stars 785 forks source link

`Tabs.remove_tab` does not update the highlighting #5218

Open TomJGooding opened 2 hours ago

TomJGooding commented 2 hours ago

Originally posted in https://github.com/Textualize/textual/issues/5215#issuecomment-2462555201, but this is actually an issue with Tabs where remove_tab does not update the highlighting.

from textual.app import App, ComposeResult
from textual.widgets import Footer, Tab, Tabs

class TabsApp(App):
    BINDINGS = [("r", "remove_foo", "Remove foo")]

    def compose(self) -> ComposeResult:
        yield Tabs(
            Tab("foo", id="foo"),
            Tab("bar", id="bar"),
            active="bar",
        )
        yield Footer()

    def action_remove_foo(self) -> None:
        tabs = self.query_one(Tabs)
        tabs.remove_tab("foo")

if __name__ == "__main__":
    app = TabsApp()
    app.run()
github-actions[bot] commented 2 hours ago

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

TomJGooding commented 1 hour ago

I created a similar snapshot test while trying to fix this, but strangely that looks as expected before any changes?

EDIT: Hm, do I need to add pilot.pause() before removing the tab?

def test_tabs_updates_highlighting_when_tab_removed(snap_compare):
    """Regression test for https://github.com/Textualize/textual/issues/5218"""

    class TabsApp(App):
        def compose(self) -> ComposeResult:
            yield Tabs(
                Tab("foo", id="foo"),
                Tab("bar", id="bar"),
                active="bar",
            )

    async def run_before(pilot: Pilot):
        pilot.app.query_one(Tabs).remove_tab("foo")

    app = TabsApp()
    assert snap_compare(app, run_before=run_before)