Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to rename tabs (TabPane). #3901

Closed
ggozad opened this issue Dec 19, 2023 · 6 comments · Fixed by #3979
Closed

Ability to rename tabs (TabPane). #3901

ggozad opened this issue Dec 19, 2023 · 6 comments · Fixed by #3979
Assignees
Labels
enhancement New feature or request

Comments

@ggozad
Copy link
Contributor

ggozad commented Dec 19, 2023

Currently this is not supported AFAIK. If one wants to change the title of a tab, they need to remove and add it again.
Fetching the Tab within TabPane and updating it doesn't fully work, for instance the underline does not adapt.

@davep
Copy link
Contributor

davep commented Dec 19, 2023

To add to the above, this can pretty much be done right now with code like this:

from textual.app import App, ComposeResult
from textual.widgets import TabbedContent, TabPane, Label

class TCRenameApp(App[None]):

    BINDINGS = [
        ("space", "rename"),
    ]

    def compose(self) -> ComposeResult:
        with TabbedContent():
            with TabPane("One", id="one"):
                yield Label("Press space to change my Tab's label")
            with TabPane("Two", id="two"):
                yield Label("This one won't rename")

    def action_rename(self) -> None:
        self.query_one(TabbedContent).get_tab("one").update("Renamed!")

if __name__ == "__main__":
    TCRenameApp().run()

but the Tab.update doesn't refresh the underline, so you go from this:

Screenshot 2023-12-19 at 07 48 17

to this:

Screenshot 2023-12-19 at 07 48 23

At its most basic level this could be an issue to ensure that the underline gets a refresh; but it could also be an issue for adding a TabbedContent API call for relabelling a tab.


@ggozad Thinking about it some more this morning, you can do what you want like this:

from textual.app import App, ComposeResult
from textual.widgets import TabbedContent, TabPane, Label, Tabs

class TCRenameApp(App[None]):

    BINDINGS = [
        ("space", "rename"),
    ]

    def compose(self) -> ComposeResult:
        with TabbedContent():
            with TabPane("One", id="one"):
                yield Label("Press space to change my Tab's label")
            with TabPane("Two", id="two"):
                yield Label("This one won't rename")

    def action_rename(self) -> None:
        tabbed_content = self.query_one(TabbedContent)
        tabbed_content.get_tab("one").update("Renamed!")
        tabbed_content.query_one(Tabs).show("one")

if __name__ == "__main__":
    TCRenameApp().run()

that call to show the tab again will cause it to refresh (although I do feel a more coherent API call for this would make sense).

@ggozad
Copy link
Contributor Author

ggozad commented Dec 19, 2023

Oooh thank you @davep !
This covers me, at least I no longer need to recreate the tab.
I will leave this open in case you decide you want to wrap it up in a documented API, but if you feel this is too much of an edge case please close the issue.

@davep
Copy link
Contributor

davep commented Dec 19, 2023

@ggozad My pleasure. I'm inclined to leave this open as I feel this is an obvious thing to want to do and it should probably have a very obvious API. Thanks for raising it.

@rodrigogiraoserrao rodrigogiraoserrao added the enhancement New feature or request label Dec 19, 2023
@Textualize Textualize deleted a comment from github-actions bot Dec 19, 2023
@willmcgugan
Copy link
Collaborator

Let's add a more elegant API for this.

@davep davep self-assigned this Jan 8, 2024
davep added a commit to davep/textual that referenced this issue Jan 8, 2024
@davep
Copy link
Contributor

davep commented Jan 8, 2024

@ggozad A PR to address this is incoming: #3979

Copy link

github-actions bot commented Jan 8, 2024

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

willmcgugan pushed a commit that referenced this issue Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants