Skip to content

Commit

Permalink
Merge pull request Textualize#3979 from davep/rename-tabs
Browse files Browse the repository at this point in the history
Better tab relabelling
  • Loading branch information
davep authored Jan 8, 2024
2 parents b61b878 + d9605d2 commit b4ba573
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Breaking change: `DOMNode.has_pseudo_class` now accepts a single name only https://github.com/Textualize/textual/pull/3970
- Made `textual.cache` (formerly `textual._cache`) public https://github.com/Textualize/textual/pull/3976
- `Tab.label` can now be used to change the label of a tab https://github.com/Textualize/textual/pull/3979

### Added

Expand Down
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
159 changes: 159 additions & 0 deletions tests/snapshot_tests/__snapshots__/test_snapshots.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -32386,6 +32386,165 @@

'''
# ---
# name: test_tab_rename
'''
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
<!-- Generated with Rich https://www.textualize.io -->
<style>

@font-face {
font-family: "Fira Code";
src: local("FiraCode-Regular"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
font-style: normal;
font-weight: 400;
}
@font-face {
font-family: "Fira Code";
src: local("FiraCode-Bold"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
font-style: bold;
font-weight: 700;
}

.terminal-1525284282-matrix {
font-family: Fira Code, monospace;
font-size: 20px;
line-height: 24.4px;
font-variant-east-asian: full-width;
}

.terminal-1525284282-title {
font-size: 18px;
font-weight: bold;
font-family: arial;
}

.terminal-1525284282-r1 { fill: #c5c8c6 }
.terminal-1525284282-r2 { fill: #e1e1e1 }
.terminal-1525284282-r3 { fill: #e1e1e1;font-weight: bold }
.terminal-1525284282-r4 { fill: #737373 }
.terminal-1525284282-r5 { fill: #474747 }
.terminal-1525284282-r6 { fill: #0178d4 }
</style>

<defs>
<clipPath id="terminal-1525284282-clip-terminal">
<rect x="0" y="0" width="975.0" height="584.5999999999999" />
</clipPath>
<clipPath id="terminal-1525284282-line-0">
<rect x="0" y="1.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-1">
<rect x="0" y="25.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-2">
<rect x="0" y="50.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-3">
<rect x="0" y="74.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-4">
<rect x="0" y="99.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-5">
<rect x="0" y="123.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-6">
<rect x="0" y="147.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-7">
<rect x="0" y="172.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-8">
<rect x="0" y="196.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-9">
<rect x="0" y="221.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-10">
<rect x="0" y="245.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-11">
<rect x="0" y="269.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-12">
<rect x="0" y="294.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-13">
<rect x="0" y="318.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-14">
<rect x="0" y="343.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-15">
<rect x="0" y="367.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-16">
<rect x="0" y="391.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-17">
<rect x="0" y="416.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-18">
<rect x="0" y="440.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-19">
<rect x="0" y="465.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-20">
<rect x="0" y="489.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-21">
<rect x="0" y="513.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-1525284282-line-22">
<rect x="0" y="538.3" width="976" height="24.65"/>
</clipPath>
</defs>

<rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="992" height="633.6" rx="8"/><text class="terminal-1525284282-title" fill="#c5c8c6" text-anchor="middle" x="496" y="27">TabRenameApp</text>
<g transform="translate(26,22)">
<circle cx="0" cy="0" r="7" fill="#ff5f57"/>
<circle cx="22" cy="0" r="7" fill="#febc2e"/>
<circle cx="44" cy="0" r="7" fill="#28c840"/>
</g>

<g transform="translate(9, 41)" clip-path="url(#terminal-1525284282-clip-terminal)">
<rect fill="#1e1e1e" x="0" y="1.5" width="512.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="512.4" y="1.5" width="48.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="561.2" y="1.5" width="61" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="622.2" y="1.5" width="73.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="695.4" y="1.5" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="780.8" y="1.5" width="97.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="878.4" y="1.5" width="97.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="24.4" y="25.9" width="475.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="500.2" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="512.4" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="536.8" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="549" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="561.2" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="585.6" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="610" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="622.2" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="646.6" y="25.9" width="36.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="683.2" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="695.4" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="719.8" y="25.9" width="48.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="768.6" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="780.8" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="805.2" y="25.9" width="61" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="866.2" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="878.4" y="25.9" width="97.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="50.3" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="50.3" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="24.4" y="50.3" width="475.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="500.2" y="50.3" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="512.4" y="50.3" width="463.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="74.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="99.1" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="24.4" y="99.1" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="109.8" y="99.1" width="61" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="170.8" y="99.1" width="780.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="951.6" y="99.1" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="123.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="147.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="172.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="196.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="221.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="245.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="269.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="294.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="318.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="343.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="367.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="391.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="416.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="440.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="465.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="489.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="513.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="538.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="562.7" width="976" height="24.65" shape-rendering="crispEdges"/>
<g class="terminal-1525284282-matrix">
<text class="terminal-1525284282-r1" x="976" y="20" textLength="12.2" clip-path="url(#terminal-1525284282-line-0)">
</text><text class="terminal-1525284282-r3" x="24.4" y="44.4" textLength="475.8" clip-path="url(#terminal-1525284282-line-1)">This&#160;is&#160;a&#160;much&#160;longer&#160;label&#160;for&#160;the&#160;tab</text><text class="terminal-1525284282-r4" x="536.8" y="44.4" textLength="12.2" clip-path="url(#terminal-1525284282-line-1)">0</text><text class="terminal-1525284282-r4" x="585.6" y="44.4" textLength="24.4" clip-path="url(#terminal-1525284282-line-1)">11</text><text class="terminal-1525284282-r4" x="646.6" y="44.4" textLength="36.6" clip-path="url(#terminal-1525284282-line-1)">222</text><text class="terminal-1525284282-r4" x="719.8" y="44.4" textLength="48.8" clip-path="url(#terminal-1525284282-line-1)">3333</text><text class="terminal-1525284282-r4" x="805.2" y="44.4" textLength="61" clip-path="url(#terminal-1525284282-line-1)">44444</text><text class="terminal-1525284282-r1" x="976" y="44.4" textLength="12.2" clip-path="url(#terminal-1525284282-line-1)">
</text><text class="terminal-1525284282-r5" x="0" y="68.8" textLength="12.2" clip-path="url(#terminal-1525284282-line-2)">━</text><text class="terminal-1525284282-r5" x="12.2" y="68.8" textLength="12.2" clip-path="url(#terminal-1525284282-line-2)">╸</text><text class="terminal-1525284282-r6" x="24.4" y="68.8" textLength="475.8" clip-path="url(#terminal-1525284282-line-2)">━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━</text><text class="terminal-1525284282-r5" x="500.2" y="68.8" textLength="12.2" clip-path="url(#terminal-1525284282-line-2)">╺</text><text class="terminal-1525284282-r5" x="512.4" y="68.8" textLength="463.6" clip-path="url(#terminal-1525284282-line-2)">━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━</text><text class="terminal-1525284282-r1" x="976" y="68.8" textLength="12.2" clip-path="url(#terminal-1525284282-line-2)">
</text><text class="terminal-1525284282-r1" x="976" y="93.2" textLength="12.2" clip-path="url(#terminal-1525284282-line-3)">
</text><text class="terminal-1525284282-r2" x="24.4" y="117.6" textLength="85.4" clip-path="url(#terminal-1525284282-line-4)">TabPane</text><text class="terminal-1525284282-r3" x="109.8" y="117.6" textLength="61" clip-path="url(#terminal-1525284282-line-4)">#test</text><text class="terminal-1525284282-r1" x="976" y="117.6" textLength="12.2" clip-path="url(#terminal-1525284282-line-4)">
</text><text class="terminal-1525284282-r1" x="976" y="142" textLength="12.2" clip-path="url(#terminal-1525284282-line-5)">
</text><text class="terminal-1525284282-r1" x="976" y="166.4" textLength="12.2" clip-path="url(#terminal-1525284282-line-6)">
</text><text class="terminal-1525284282-r1" x="976" y="190.8" textLength="12.2" clip-path="url(#terminal-1525284282-line-7)">
</text><text class="terminal-1525284282-r1" x="976" y="215.2" textLength="12.2" clip-path="url(#terminal-1525284282-line-8)">
</text><text class="terminal-1525284282-r1" x="976" y="239.6" textLength="12.2" clip-path="url(#terminal-1525284282-line-9)">
</text><text class="terminal-1525284282-r1" x="976" y="264" textLength="12.2" clip-path="url(#terminal-1525284282-line-10)">
</text><text class="terminal-1525284282-r1" x="976" y="288.4" textLength="12.2" clip-path="url(#terminal-1525284282-line-11)">
</text><text class="terminal-1525284282-r1" x="976" y="312.8" textLength="12.2" clip-path="url(#terminal-1525284282-line-12)">
</text><text class="terminal-1525284282-r1" x="976" y="337.2" textLength="12.2" clip-path="url(#terminal-1525284282-line-13)">
</text><text class="terminal-1525284282-r1" x="976" y="361.6" textLength="12.2" clip-path="url(#terminal-1525284282-line-14)">
</text><text class="terminal-1525284282-r1" x="976" y="386" textLength="12.2" clip-path="url(#terminal-1525284282-line-15)">
</text><text class="terminal-1525284282-r1" x="976" y="410.4" textLength="12.2" clip-path="url(#terminal-1525284282-line-16)">
</text><text class="terminal-1525284282-r1" x="976" y="434.8" textLength="12.2" clip-path="url(#terminal-1525284282-line-17)">
</text><text class="terminal-1525284282-r1" x="976" y="459.2" textLength="12.2" clip-path="url(#terminal-1525284282-line-18)">
</text><text class="terminal-1525284282-r1" x="976" y="483.6" textLength="12.2" clip-path="url(#terminal-1525284282-line-19)">
</text><text class="terminal-1525284282-r1" x="976" y="508" textLength="12.2" clip-path="url(#terminal-1525284282-line-20)">
</text><text class="terminal-1525284282-r1" x="976" y="532.4" textLength="12.2" clip-path="url(#terminal-1525284282-line-21)">
</text><text class="terminal-1525284282-r1" x="976" y="556.8" textLength="12.2" clip-path="url(#terminal-1525284282-line-22)">
</text>
</g>
</g>
</svg>

'''
# ---
# name: test_tabbed_content
'''
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
Expand Down
16 changes: 16 additions & 0 deletions tests/snapshot_tests/snapshot_apps/tab_rename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from textual.app import App, ComposeResult
from textual.widgets import TabbedContent, TabPane

class TabRenameApp(App[None]):

def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("!", id="test")
for n in range(5):
yield TabPane(str(n) * (n+1))

def on_mount(self) -> None:
self.query_one(TabbedContent).get_tab("test").label = "This is a much longer label for the tab"

if __name__ == "__main__":
TabRenameApp().run()
5 changes: 5 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,3 +979,8 @@ def test_nested_specificity(snap_compare):
"""Test specificity of nested rules is working."""
# https://github.com/Textualize/textual/issues/3961
assert snap_compare(SNAPSHOT_APPS_DIR / "nested_specificity.py")


def test_tab_rename(snap_compare):
"""Test setting a new label for a tab amongst a TabbedContent."""
assert snap_compare(SNAPSHOT_APPS_DIR / "tab_rename.py")
8 changes: 8 additions & 0 deletions tests/test_tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ async def test_tab_label():
assert Tab("Pilot").label_text == "Pilot"


async def test_tab_relabel():
"""It should be possible to relabel a tab."""
tab = Tab("Pilot")
assert tab.label_text == "Pilot"
tab.label = "Aeryn"
assert tab.label_text == "Aeryn"


async def test_compose_empty_tabs():
"""It should be possible to create an empty Tabs."""

Expand Down

0 comments on commit b4ba573

Please sign in to comment.