Skip to content

Commit bb9cc62

Browse files
authored
tooltips in compound widgets (#2801)
* tooltips in compound widgets * snapshot tweak * test fixes
1 parent e7531f4 commit bb9cc62

File tree

5 files changed

+200
-2
lines changed

5 files changed

+200
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## Unreleased
9+
10+
### Changed
11+
12+
- Tooltips are now inherited, so will work with compound widgets
13+
814
## [0.28.0] - 2023-06-19
915

1016
### Added
@@ -18,7 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1824
- Added `origin_visible` parameter to `Widget.scroll_to_region`
1925
- Added `origin_visible` parameter to `Widget.scroll_to_center`
2026
- Added `TabbedContent.tab_count` https://github.com/Textualize/textual/pull/2751
21-
- Added `TabbedContnet.add_pane` https://github.com/Textualize/textual/pull/2751
27+
- Added `TabbedContent.add_pane` https://github.com/Textualize/textual/pull/2751
2228
- Added `TabbedContent.remove_pane` https://github.com/Textualize/textual/pull/2751
2329
- Added `TabbedContent.clear_panes` https://github.com/Textualize/textual/pull/2751
2430
- Added `TabbedContent.Cleared` https://github.com/Textualize/textual/pull/2751

src/textual/screen.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,14 @@ def _handle_tooltip_timer(self, widget: Widget) -> None:
768768
except NoMatches:
769769
pass
770770
else:
771-
tooltip_content = widget.tooltip
771+
tooltip_content: RenderableType | None = None
772+
for node in widget.ancestors_with_self:
773+
if not isinstance(node, Widget):
774+
break
775+
if node.tooltip is not None:
776+
tooltip_content = node.tooltip
777+
break
778+
772779
if tooltip_content is None:
773780
tooltip.display = False
774781
else:

tests/snapshot_tests/__snapshots__/test_snapshots.ambr

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26255,6 +26255,164 @@
2625526255

2625626256
'''
2625726257
# ---
26258+
# name: test_tooltips_in_compound_widgets
26259+
'''
26260+
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
26261+
<!-- Generated with Rich https://www.textualize.io -->
26262+
<style>
26263+
26264+
@font-face {
26265+
font-family: "Fira Code";
26266+
src: local("FiraCode-Regular"),
26267+
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
26268+
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
26269+
font-style: normal;
26270+
font-weight: 400;
26271+
}
26272+
@font-face {
26273+
font-family: "Fira Code";
26274+
src: local("FiraCode-Bold"),
26275+
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
26276+
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
26277+
font-style: bold;
26278+
font-weight: 700;
26279+
}
26280+
26281+
.terminal-3455460968-matrix {
26282+
font-family: Fira Code, monospace;
26283+
font-size: 20px;
26284+
line-height: 24.4px;
26285+
font-variant-east-asian: full-width;
26286+
}
26287+
26288+
.terminal-3455460968-title {
26289+
font-size: 18px;
26290+
font-weight: bold;
26291+
font-family: arial;
26292+
}
26293+
26294+
.terminal-3455460968-r1 { fill: #fea62b }
26295+
.terminal-3455460968-r2 { fill: #323232 }
26296+
.terminal-3455460968-r3 { fill: #c5c8c6 }
26297+
.terminal-3455460968-r4 { fill: #e1e1e1 }
26298+
.terminal-3455460968-r5 { fill: #e2e3e3 }
26299+
</style>
26300+
26301+
<defs>
26302+
<clipPath id="terminal-3455460968-clip-terminal">
26303+
<rect x="0" y="0" width="975.0" height="584.5999999999999" />
26304+
</clipPath>
26305+
<clipPath id="terminal-3455460968-line-0">
26306+
<rect x="0" y="1.5" width="976" height="24.65"/>
26307+
</clipPath>
26308+
<clipPath id="terminal-3455460968-line-1">
26309+
<rect x="0" y="25.9" width="976" height="24.65"/>
26310+
</clipPath>
26311+
<clipPath id="terminal-3455460968-line-2">
26312+
<rect x="0" y="50.3" width="976" height="24.65"/>
26313+
</clipPath>
26314+
<clipPath id="terminal-3455460968-line-3">
26315+
<rect x="0" y="74.7" width="976" height="24.65"/>
26316+
</clipPath>
26317+
<clipPath id="terminal-3455460968-line-4">
26318+
<rect x="0" y="99.1" width="976" height="24.65"/>
26319+
</clipPath>
26320+
<clipPath id="terminal-3455460968-line-5">
26321+
<rect x="0" y="123.5" width="976" height="24.65"/>
26322+
</clipPath>
26323+
<clipPath id="terminal-3455460968-line-6">
26324+
<rect x="0" y="147.9" width="976" height="24.65"/>
26325+
</clipPath>
26326+
<clipPath id="terminal-3455460968-line-7">
26327+
<rect x="0" y="172.3" width="976" height="24.65"/>
26328+
</clipPath>
26329+
<clipPath id="terminal-3455460968-line-8">
26330+
<rect x="0" y="196.7" width="976" height="24.65"/>
26331+
</clipPath>
26332+
<clipPath id="terminal-3455460968-line-9">
26333+
<rect x="0" y="221.1" width="976" height="24.65"/>
26334+
</clipPath>
26335+
<clipPath id="terminal-3455460968-line-10">
26336+
<rect x="0" y="245.5" width="976" height="24.65"/>
26337+
</clipPath>
26338+
<clipPath id="terminal-3455460968-line-11">
26339+
<rect x="0" y="269.9" width="976" height="24.65"/>
26340+
</clipPath>
26341+
<clipPath id="terminal-3455460968-line-12">
26342+
<rect x="0" y="294.3" width="976" height="24.65"/>
26343+
</clipPath>
26344+
<clipPath id="terminal-3455460968-line-13">
26345+
<rect x="0" y="318.7" width="976" height="24.65"/>
26346+
</clipPath>
26347+
<clipPath id="terminal-3455460968-line-14">
26348+
<rect x="0" y="343.1" width="976" height="24.65"/>
26349+
</clipPath>
26350+
<clipPath id="terminal-3455460968-line-15">
26351+
<rect x="0" y="367.5" width="976" height="24.65"/>
26352+
</clipPath>
26353+
<clipPath id="terminal-3455460968-line-16">
26354+
<rect x="0" y="391.9" width="976" height="24.65"/>
26355+
</clipPath>
26356+
<clipPath id="terminal-3455460968-line-17">
26357+
<rect x="0" y="416.3" width="976" height="24.65"/>
26358+
</clipPath>
26359+
<clipPath id="terminal-3455460968-line-18">
26360+
<rect x="0" y="440.7" width="976" height="24.65"/>
26361+
</clipPath>
26362+
<clipPath id="terminal-3455460968-line-19">
26363+
<rect x="0" y="465.1" width="976" height="24.65"/>
26364+
</clipPath>
26365+
<clipPath id="terminal-3455460968-line-20">
26366+
<rect x="0" y="489.5" width="976" height="24.65"/>
26367+
</clipPath>
26368+
<clipPath id="terminal-3455460968-line-21">
26369+
<rect x="0" y="513.9" width="976" height="24.65"/>
26370+
</clipPath>
26371+
<clipPath id="terminal-3455460968-line-22">
26372+
<rect x="0" y="538.3" width="976" height="24.65"/>
26373+
</clipPath>
26374+
</defs>
26375+
26376+
<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-3455460968-title" fill="#c5c8c6" text-anchor="middle" x="496" y="27">TooltipApp</text>
26377+
<g transform="translate(26,22)">
26378+
<circle cx="0" cy="0" r="7" fill="#ff5f57"/>
26379+
<circle cx="22" cy="0" r="7" fill="#febc2e"/>
26380+
<circle cx="44" cy="0" r="7" fill="#28c840"/>
26381+
</g>
26382+
26383+
<g transform="translate(9, 41)" clip-path="url(#terminal-3455460968-clip-terminal)">
26384+
<rect fill="#1e1e1e" x="0" y="1.5" width="36.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="36.6" y="1.5" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="48.8" y="1.5" width="341.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="390.4" y="1.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="414.8" y="1.5" width="36.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="451.4" y="1.5" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="463.6" y="1.5" width="97.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="561.2" y="1.5" width="414.8" 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="#24292f" x="24.4" y="25.9" width="231.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="256.2" y="25.9" width="719.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="50.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#24292f" x="24.4" y="50.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#24292f" x="48.8" y="50.3" width="183" height="24.65" shape-rendering="crispEdges"/><rect fill="#24292f" x="231.8" y="50.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="256.2" y="50.3" width="719.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="74.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#24292f" x="24.4" y="74.7" width="231.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="256.2" y="74.7" width="719.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="99.1" width="976" 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"/>
26385+
<g class="terminal-3455460968-matrix">
26386+
<text class="terminal-3455460968-r1" x="0" y="20" textLength="36.6" clip-path="url(#terminal-3455460968-line-0)">━━━</text><text class="terminal-3455460968-r2" x="36.6" y="20" textLength="12.2" clip-path="url(#terminal-3455460968-line-0)">╺</text><text class="terminal-3455460968-r2" x="48.8" y="20" textLength="341.6" clip-path="url(#terminal-3455460968-line-0)">━━━━━━━━━━━━━━━━━━━━━━━━━━━━</text><text class="terminal-3455460968-r4" x="414.8" y="20" textLength="36.6" clip-path="url(#terminal-3455460968-line-0)">10%</text><text class="terminal-3455460968-r4" x="463.6" y="20" textLength="97.6" clip-path="url(#terminal-3455460968-line-0)">--:--:--</text><text class="terminal-3455460968-r3" x="976" y="20" textLength="12.2" clip-path="url(#terminal-3455460968-line-0)">
26387+
</text><text class="terminal-3455460968-r3" x="976" y="44.4" textLength="12.2" clip-path="url(#terminal-3455460968-line-1)">
26388+
</text><text class="terminal-3455460968-r5" x="48.8" y="68.8" textLength="183" clip-path="url(#terminal-3455460968-line-2)">Hello,&#160;Tooltip!</text><text class="terminal-3455460968-r3" x="976" y="68.8" textLength="12.2" clip-path="url(#terminal-3455460968-line-2)">
26389+
</text><text class="terminal-3455460968-r3" x="976" y="93.2" textLength="12.2" clip-path="url(#terminal-3455460968-line-3)">
26390+
</text><text class="terminal-3455460968-r3" x="976" y="117.6" textLength="12.2" clip-path="url(#terminal-3455460968-line-4)">
26391+
</text><text class="terminal-3455460968-r3" x="976" y="142" textLength="12.2" clip-path="url(#terminal-3455460968-line-5)">
26392+
</text><text class="terminal-3455460968-r3" x="976" y="166.4" textLength="12.2" clip-path="url(#terminal-3455460968-line-6)">
26393+
</text><text class="terminal-3455460968-r3" x="976" y="190.8" textLength="12.2" clip-path="url(#terminal-3455460968-line-7)">
26394+
</text><text class="terminal-3455460968-r3" x="976" y="215.2" textLength="12.2" clip-path="url(#terminal-3455460968-line-8)">
26395+
</text><text class="terminal-3455460968-r3" x="976" y="239.6" textLength="12.2" clip-path="url(#terminal-3455460968-line-9)">
26396+
</text><text class="terminal-3455460968-r3" x="976" y="264" textLength="12.2" clip-path="url(#terminal-3455460968-line-10)">
26397+
</text><text class="terminal-3455460968-r3" x="976" y="288.4" textLength="12.2" clip-path="url(#terminal-3455460968-line-11)">
26398+
</text><text class="terminal-3455460968-r3" x="976" y="312.8" textLength="12.2" clip-path="url(#terminal-3455460968-line-12)">
26399+
</text><text class="terminal-3455460968-r3" x="976" y="337.2" textLength="12.2" clip-path="url(#terminal-3455460968-line-13)">
26400+
</text><text class="terminal-3455460968-r3" x="976" y="361.6" textLength="12.2" clip-path="url(#terminal-3455460968-line-14)">
26401+
</text><text class="terminal-3455460968-r3" x="976" y="386" textLength="12.2" clip-path="url(#terminal-3455460968-line-15)">
26402+
</text><text class="terminal-3455460968-r3" x="976" y="410.4" textLength="12.2" clip-path="url(#terminal-3455460968-line-16)">
26403+
</text><text class="terminal-3455460968-r3" x="976" y="434.8" textLength="12.2" clip-path="url(#terminal-3455460968-line-17)">
26404+
</text><text class="terminal-3455460968-r3" x="976" y="459.2" textLength="12.2" clip-path="url(#terminal-3455460968-line-18)">
26405+
</text><text class="terminal-3455460968-r3" x="976" y="483.6" textLength="12.2" clip-path="url(#terminal-3455460968-line-19)">
26406+
</text><text class="terminal-3455460968-r3" x="976" y="508" textLength="12.2" clip-path="url(#terminal-3455460968-line-20)">
26407+
</text><text class="terminal-3455460968-r3" x="976" y="532.4" textLength="12.2" clip-path="url(#terminal-3455460968-line-21)">
26408+
</text><text class="terminal-3455460968-r3" x="976" y="556.8" textLength="12.2" clip-path="url(#terminal-3455460968-line-22)">
26409+
</text>
26410+
</g>
26411+
</g>
26412+
</svg>
26413+
26414+
'''
26415+
# ---
2625826416
# name: test_tree_example
2625926417
'''
2626026418
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from textual.app import App, ComposeResult
2+
from textual.widgets import ProgressBar
3+
4+
5+
class TooltipApp(App[None]):
6+
def compose(self) -> ComposeResult:
7+
progress_bar = ProgressBar(100)
8+
progress_bar.advance(10)
9+
progress_bar.tooltip = "Hello, Tooltip!"
10+
yield progress_bar
11+
12+
13+
if __name__ == "__main__":
14+
app = TooltipApp()
15+
app.run()

tests/snapshot_tests/test_snapshots.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,11 @@ def test_tabbed_content(snap_compare):
213213
def test_option_list_strings(snap_compare):
214214
assert snap_compare(WIDGET_EXAMPLES_DIR / "option_list_strings.py")
215215

216+
216217
def test_option_list_options(snap_compare):
217218
assert snap_compare(WIDGET_EXAMPLES_DIR / "option_list_options.py")
218219

220+
219221
def test_option_list_tables(snap_compare):
220222
assert snap_compare(WIDGET_EXAMPLES_DIR / "option_list_tables.py")
221223

@@ -562,3 +564,13 @@ def test_blur_on_disabled(snap_compare):
562564
SNAPSHOT_APPS_DIR / "blur_on_disabled.py",
563565
press=[*"foo", "f3", *"this should not appear"],
564566
)
567+
568+
569+
def test_tooltips_in_compound_widgets(snap_compare):
570+
# https://github.com/Textualize/textual/issues/2641
571+
async def run_before(pilot) -> None:
572+
await pilot.hover("ProgressBar")
573+
await pilot.pause(0.3)
574+
await pilot.pause()
575+
576+
assert snap_compare(SNAPSHOT_APPS_DIR / "tooltips.py", run_before=run_before)

0 commit comments

Comments
 (0)