diff --git a/src/textual/widgets/_tabs.py b/src/textual/widgets/_tabs.py index 123ade6c13..0123e17688 100644 --- a/src/textual/widgets/_tabs.py +++ b/src/textual/widgets/_tabs.py @@ -5,6 +5,7 @@ from typing import ClassVar import rich.repr +from rich.console import RenderableType from rich.style import Style from rich.text import Text, TextType @@ -143,6 +144,9 @@ class Disabled(TabMessage): class Enabled(TabMessage): """A tab was enabled.""" + class Relabelled(TabMessage): + """A tab was relabelled.""" + def __init__( self, label: TextType, @@ -159,9 +163,23 @@ def __init__( classes: Space separated list of class names. disabled: Whether the tab is disabled or not. """ - self.label = Text.from_markup(label) if isinstance(label, str) else label super().__init__(id=id, classes=classes, disabled=disabled) - self.update(label) + self._label: Text + self.label = label + + @property + def label(self) -> Text: + """The label for the tab.""" + return self._label + + @label.setter + def label(self, label: TextType) -> None: + self._label = Text.from_markup(label) if isinstance(label, str) else label + self.update(self._label) + + def update(self, renderable: RenderableType = "") -> None: + self.post_message(self.Relabelled(self)) + return super().update(renderable) @property def label_text(self) -> str: @@ -693,6 +711,11 @@ def _on_tab_enabled(self, event: Tab.Enabled) -> None: event.stop() self.post_message(self.TabEnabled(self, event.tab)) + def _on_tab_relabelled(self, event: Tab.Relabelled) -> None: + """Redraw the highlight when tab is relabelled.""" + event.stop() + self._highlight_active() + def disable(self, tab_id: str) -> Tab: """Disable the indicated tab.