Skip to content

Commit

Permalink
Allow Tab.label to be set and have the display redraw
Browse files Browse the repository at this point in the history
In support of a better interface for Textualize#3901.
  • Loading branch information
davep committed Jan 8, 2024
1 parent b61b878 commit 318a436
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/textual/widgets/_tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 318a436

Please sign in to comment.