Skip to content

Commit c39641e

Browse files
authored
Merge pull request Textualize#4859 from Textualize/binding-tooltip
Add tooltip
2 parents ca3ade4 + 0135a20 commit c39641e

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

CHANGELOG.md

+6
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+
### Added
11+
12+
- Added `tooltip` to Binding https://github.com/Textualize/textual/pull/4859
13+
814
## [0.76.0]
915

1016
### Changed

src/textual/binding.py

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class Binding:
5050
"""How the key should be shown in footer."""
5151
priority: bool = False
5252
"""Enable priority binding for this key."""
53+
tooltip: str = ""
54+
"""Optional tooltip to show in footer."""
5355

5456

5557
class ActiveBinding(NamedTuple):
@@ -61,6 +63,8 @@ class ActiveBinding(NamedTuple):
6163
"""The binding information."""
6264
enabled: bool
6365
"""Is the binding enabled? (enabled bindings are typically rendered dim)"""
66+
tooltip: str = ""
67+
"""Optional tooltip shown in Footer."""
6468

6569

6670
@rich.repr.auto
@@ -112,6 +116,7 @@ def make_bindings(bindings: Iterable[BindingType]) -> Iterable[Binding]:
112116
show=bool(binding.description and binding.show),
113117
key_display=binding.key_display,
114118
priority=binding.priority,
119+
tooltip=binding.tooltip,
115120
)
116121

117122
self.key_to_bindings: dict[str, list[Binding]] = {}

src/textual/screen.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,14 @@ def active_bindings(self) -> dict[str, ActiveBinding]:
348348
binding.priority
349349
and not existing_key_and_binding.binding.priority
350350
):
351-
bindings_map[key] = ActiveBinding(namespace, binding, enabled)
351+
bindings_map[key] = ActiveBinding(
352+
namespace, binding, enabled, binding.tooltip
353+
)
352354
else:
353355
# New binding
354-
bindings_map[key] = ActiveBinding(namespace, binding, enabled)
356+
bindings_map[key] = ActiveBinding(
357+
namespace, binding, enabled, binding.tooltip
358+
)
355359

356360
return bindings_map
357361

src/textual/widgets/_classic_footer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ def _make_key_text(self) -> Text:
108108
description_style = self.get_component_rich_style("footer--description")
109109

110110
bindings = [
111-
(binding, enabled)
112-
for (_, binding, enabled) in self.screen.active_bindings.values()
111+
(binding, enabled, tooltip)
112+
for (_, binding, enabled, tooltip) in self.screen.active_bindings.values()
113113
if binding.show
114114
]
115115

116116
action_to_bindings: defaultdict[str, list[tuple[Binding, bool]]] = defaultdict(
117117
list
118118
)
119-
for binding, enabled in bindings:
119+
for binding, enabled, _ in bindings:
120120
action_to_bindings[binding.action].append((binding, enabled))
121121

122122
app_focus = self.app.app_focus

src/textual/widgets/_footer.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,16 @@ def __init__(
8484
description: str,
8585
action: str,
8686
disabled: bool = False,
87+
tooltip: str = "",
8788
) -> None:
8889
self.key = key
8990
self.key_display = key_display
9091
self.description = description
9192
self.action = action
9293
self._disabled = disabled
9394
super().__init__(classes="-disabled" if disabled else "")
95+
if tooltip:
96+
self.tooltip = tooltip
9497

9598
def render(self) -> Text:
9699
key_style = self.get_component_rich_style("footer-key--key")
@@ -160,24 +163,25 @@ def compose(self) -> ComposeResult:
160163
if not self._bindings_ready:
161164
return
162165
bindings = [
163-
(binding, enabled)
164-
for (_, binding, enabled) in self.screen.active_bindings.values()
166+
(binding, enabled, tooltip)
167+
for (_, binding, enabled, tooltip) in self.screen.active_bindings.values()
165168
if binding.show
166169
]
167-
action_to_bindings: defaultdict[str, list[tuple[Binding, bool]]]
170+
action_to_bindings: defaultdict[str, list[tuple[Binding, bool, str]]]
168171
action_to_bindings = defaultdict(list)
169-
for binding, enabled in bindings:
170-
action_to_bindings[binding.action].append((binding, enabled))
172+
for binding, enabled, tooltip in bindings:
173+
action_to_bindings[binding.action].append((binding, enabled, tooltip))
171174

172175
self.styles.grid_size_columns = len(action_to_bindings)
173176
for multi_bindings in action_to_bindings.values():
174-
binding, enabled = multi_bindings[0]
177+
binding, enabled, tooltip = multi_bindings[0]
175178
yield FooterKey(
176179
binding.key,
177180
binding.key_display or self.app.get_key_display(binding.key),
178181
binding.description,
179182
binding.action,
180183
disabled=not enabled,
184+
tooltip=tooltip,
181185
).data_bind(
182186
Footer.upper_case_keys,
183187
Footer.ctrl_to_caret,

0 commit comments

Comments
 (0)