From 888d8d92837e593908de8e954d24e2ad5e653f23 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 14 Aug 2024 16:39:08 +0100 Subject: [PATCH] Actions --- examples/markdown.py | 3 +-- src/textual/app.py | 13 +++++++++++++ src/textual/system_commands.py | 21 ++++++++++++++++++--- src/textual/widgets/__init__.pyi | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/examples/markdown.py b/examples/markdown.py index caad2920d..eb8bd1639 100644 --- a/examples/markdown.py +++ b/examples/markdown.py @@ -6,7 +6,7 @@ from sys import argv from textual.app import App, ComposeResult from textual.binding import Binding from textual.reactive import var -from textual.widgets import Footer, KeyPanel, MarkdownViewer +from textual.widgets import Footer, MarkdownViewer class MarkdownApp(App): @@ -33,7 +33,6 @@ class MarkdownApp(App): def compose(self) -> ComposeResult: yield Footer() yield MarkdownViewer() - yield KeyPanel() async def on_mount(self) -> None: """Go to the first path when the app starts.""" diff --git a/src/textual/app.py b/src/textual/app.py index 7d0d2cec4..aa8595bb5 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -3504,6 +3504,19 @@ class App(Generic[ReturnType], DOMNode): """An [action](/guide/actions) to focus the previous widget.""" self.screen.focus_previous() + def action_hide_keys(self) -> None: + """Hide the keys panel (if present).""" + self.screen.query("KeyPanel").remove() + + def action_show_keys(self) -> None: + """Show the keys panel.""" + from .widgets import KeyPanel + + try: + self.query_one(KeyPanel) + except NoMatches: + self.mount(KeyPanel()) + def _on_terminal_supports_synchronized_output( self, message: messages.TerminalSupportsSynchronizedOutput ) -> None: diff --git a/src/textual/system_commands.py b/src/textual/system_commands.py index 7a10b3fa6..a5c1011a1 100644 --- a/src/textual/system_commands.py +++ b/src/textual/system_commands.py @@ -24,9 +24,9 @@ class SystemCommands(Provider): """ @property - def _system_commands(self) -> tuple[tuple[str, IgnoreReturnCallbackType, str], ...]: + def _system_commands(self) -> list[tuple[str, IgnoreReturnCallbackType, str]]: """The system commands to reveal to the command palette.""" - return ( + commands = [ ( "Toggle light/dark mode", self.app.action_toggle_dark, @@ -37,7 +37,22 @@ class SystemCommands(Provider): self.app.action_quit, "Quit the application as soon as possible", ), - ) + ] + if self.screen.query("KeyPanel"): + commands.append( + ("Hide keys", self.app.action_hide_keys, "Hide the keys panel") + ) + else: + commands.append( + ( + "Show keys", + self.app.action_show_keys, + "Show a summary of available keys", + ) + ) + commands.sort(key=lambda command: command[0]) + + return commands async def discover(self) -> Hits: """Handle a request for the discovery commands for this provider. diff --git a/src/textual/widgets/__init__.pyi b/src/textual/widgets/__init__.pyi index 61332e635..f09f042d9 100644 --- a/src/textual/widgets/__init__.pyi +++ b/src/textual/widgets/__init__.pyi @@ -10,7 +10,7 @@ from ._directory_tree import DirectoryTree as DirectoryTree from ._footer import Footer as Footer from ._header import Header as Header from ._input import Input as Input -from ._key_panel import KeyPanel +from ._key_panel import KeyPanel as KeyPanel from ._label import Label as Label from ._list_item import ListItem as ListItem from ._list_view import ListView as ListView