Skip to content

Commit

Permalink
Merge pull request #4859 from Textualize/binding-tooltip
Browse files Browse the repository at this point in the history
Add tooltip
  • Loading branch information
willmcgugan authored Aug 9, 2024
2 parents ca3ade4 + 0135a20 commit c39641e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added

- Added `tooltip` to Binding https://github.com/Textualize/textual/pull/4859

## [0.76.0]

### Changed
Expand Down
5 changes: 5 additions & 0 deletions src/textual/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Binding:
"""How the key should be shown in footer."""
priority: bool = False
"""Enable priority binding for this key."""
tooltip: str = ""
"""Optional tooltip to show in footer."""


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


@rich.repr.auto
Expand Down Expand Up @@ -112,6 +116,7 @@ def make_bindings(bindings: Iterable[BindingType]) -> Iterable[Binding]:
show=bool(binding.description and binding.show),
key_display=binding.key_display,
priority=binding.priority,
tooltip=binding.tooltip,
)

self.key_to_bindings: dict[str, list[Binding]] = {}
Expand Down
8 changes: 6 additions & 2 deletions src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,14 @@ def active_bindings(self) -> dict[str, ActiveBinding]:
binding.priority
and not existing_key_and_binding.binding.priority
):
bindings_map[key] = ActiveBinding(namespace, binding, enabled)
bindings_map[key] = ActiveBinding(
namespace, binding, enabled, binding.tooltip
)
else:
# New binding
bindings_map[key] = ActiveBinding(namespace, binding, enabled)
bindings_map[key] = ActiveBinding(
namespace, binding, enabled, binding.tooltip
)

return bindings_map

Expand Down
6 changes: 3 additions & 3 deletions src/textual/widgets/_classic_footer.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ def _make_key_text(self) -> Text:
description_style = self.get_component_rich_style("footer--description")

bindings = [
(binding, enabled)
for (_, binding, enabled) in self.screen.active_bindings.values()
(binding, enabled, tooltip)
for (_, binding, enabled, tooltip) in self.screen.active_bindings.values()
if binding.show
]

action_to_bindings: defaultdict[str, list[tuple[Binding, bool]]] = defaultdict(
list
)
for binding, enabled in bindings:
for binding, enabled, _ in bindings:
action_to_bindings[binding.action].append((binding, enabled))

app_focus = self.app.app_focus
Expand Down
16 changes: 10 additions & 6 deletions src/textual/widgets/_footer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ def __init__(
description: str,
action: str,
disabled: bool = False,
tooltip: str = "",
) -> None:
self.key = key
self.key_display = key_display
self.description = description
self.action = action
self._disabled = disabled
super().__init__(classes="-disabled" if disabled else "")
if tooltip:
self.tooltip = tooltip

def render(self) -> Text:
key_style = self.get_component_rich_style("footer-key--key")
Expand Down Expand Up @@ -160,24 +163,25 @@ def compose(self) -> ComposeResult:
if not self._bindings_ready:
return
bindings = [
(binding, enabled)
for (_, binding, enabled) in self.screen.active_bindings.values()
(binding, enabled, tooltip)
for (_, binding, enabled, tooltip) in self.screen.active_bindings.values()
if binding.show
]
action_to_bindings: defaultdict[str, list[tuple[Binding, bool]]]
action_to_bindings: defaultdict[str, list[tuple[Binding, bool, str]]]
action_to_bindings = defaultdict(list)
for binding, enabled in bindings:
action_to_bindings[binding.action].append((binding, enabled))
for binding, enabled, tooltip in bindings:
action_to_bindings[binding.action].append((binding, enabled, tooltip))

self.styles.grid_size_columns = len(action_to_bindings)
for multi_bindings in action_to_bindings.values():
binding, enabled = multi_bindings[0]
binding, enabled, tooltip = multi_bindings[0]
yield FooterKey(
binding.key,
binding.key_display or self.app.get_key_display(binding.key),
binding.description,
binding.action,
disabled=not enabled,
tooltip=tooltip,
).data_bind(
Footer.upper_case_keys,
Footer.ctrl_to_caret,
Expand Down

0 comments on commit c39641e

Please sign in to comment.