Skip to content

Commit 1c903a2

Browse files
authored
Merge pull request #5322 from Textualize/cp-disable
Cp disable
2 parents afbe07a + 65034bf commit 1c903a2

File tree

3 files changed

+192
-15
lines changed

3 files changed

+192
-15
lines changed

src/textual/widgets/_footer.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,10 @@ def __init__(
207207
def compose(self) -> ComposeResult:
208208
if not self._bindings_ready:
209209
return
210+
active_bindings = self.screen.active_bindings
210211
bindings = [
211212
(binding, enabled, tooltip)
212-
for (_, binding, enabled, tooltip) in self.screen.active_bindings.values()
213+
for (_, binding, enabled, tooltip) in active_bindings.values()
213214
if binding.show
214215
]
215216
action_to_bindings: defaultdict[str, list[tuple[Binding, bool, str]]]
@@ -229,20 +230,22 @@ def compose(self) -> ComposeResult:
229230
tooltip=tooltip,
230231
).data_bind(Footer.compact)
231232
if self.show_command_palette and self.app.ENABLE_COMMAND_PALETTE:
232-
for key, binding in self.app._bindings:
233-
if binding.action in (
234-
"app.command_palette",
235-
"command_palette",
236-
):
237-
yield FooterKey(
238-
key,
239-
self.app.get_key_display(binding),
240-
binding.description,
241-
binding.action,
242-
classes="-command-palette",
243-
tooltip=binding.tooltip or binding.description,
244-
)
245-
break
233+
try:
234+
_node, binding, enabled, tooltip = active_bindings[
235+
self.app.COMMAND_PALETTE_BINDING
236+
]
237+
except KeyError:
238+
pass
239+
else:
240+
yield FooterKey(
241+
binding.key,
242+
self.app.get_key_display(binding),
243+
binding.description,
244+
binding.action,
245+
classes="-command-palette",
246+
disabled=not enabled,
247+
tooltip=binding.tooltip or binding.description,
248+
)
246249

247250
async def bindings_changed(self, screen: Screen) -> None:
248251
self._bindings_ready = True
Lines changed: 154 additions & 0 deletions
Loading

tests/snapshot_tests/test_snapshots.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2858,3 +2858,23 @@ async def run_before(pilot: Pilot) -> None:
28582858
await pilot.click(Select)
28592859

28602860
snap_compare(SelectApp(), run_before=run_before)
2861+
2862+
2863+
def test_disable_command_palette(snap_compare):
2864+
"""Test command palette may be disabled by check_action.
2865+
You should see a footer with an enabled binding, and the command palette binding greyed out."""
2866+
2867+
class FooterApp(App):
2868+
BINDINGS = [("b", "bell", "Bell")]
2869+
2870+
def compose(self) -> ComposeResult:
2871+
yield Footer()
2872+
2873+
def check_action(
2874+
self, action: str, parameters: tuple[object, ...]
2875+
) -> bool | None:
2876+
if action == "command_palette":
2877+
return None
2878+
return True
2879+
2880+
snap_compare(FooterApp())

0 commit comments

Comments
 (0)