From 9c1da2fa12fa2f4977a30a3c6ee8978a0a3732a0 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Wed, 17 Jul 2024 00:10:17 +0100 Subject: [PATCH 1/3] Ensure input is focused when command palette launches --- examples/color_command.py | 1 + src/textual/command.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/color_command.py b/examples/color_command.py index bd4148657b..6481d6ff3b 100644 --- a/examples/color_command.py +++ b/examples/color_command.py @@ -47,6 +47,7 @@ class ColorBlock(Static): class ColorApp(App): """Experiment with the command palette.""" + AUTO_FOCUS = None COMMANDS = App.COMMANDS | {ColorCommands} TITLE = "Press ctrl + \\ and type a color" diff --git a/src/textual/command.py b/src/textual/command.py index 2cd523767a..85507f5cf1 100644 --- a/src/textual/command.py +++ b/src/textual/command.py @@ -625,7 +625,9 @@ def compose(self) -> ComposeResult: with Vertical(): with Horizontal(id="--input"): yield SearchIcon() - yield CommandInput(placeholder="Search for commands…") + input = CommandInput(placeholder="Search for commands…") + input.focus() + yield input if not self.run_on_select: yield Button("\u25b6") with Vertical(id="--results"): From c5969b6c95a35889d6024939e1d7350f797e84e8 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Wed, 17 Jul 2024 00:12:11 +0100 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 1 + examples/color_command.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a8732bd92..11522debcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed `Tree` and `DirectoryTree` horizontal scrolling off-by-2 https://github.com/Textualize/textual/pull/4744 - Fixed text-opacity in component styles https://github.com/Textualize/textual/pull/4747 - Ensure `Tree.select_node` sends `NodeSelected` message https://github.com/Textualize/textual/pull/4753 +- Fixed `CommandPalette` not focusing the input when opened when `App.AUTO_FOCUS` doesn't match the input https://github.com/Textualize/textual/pull/4763 ### Changed diff --git a/examples/color_command.py b/examples/color_command.py index 6481d6ff3b..bd4148657b 100644 --- a/examples/color_command.py +++ b/examples/color_command.py @@ -47,7 +47,6 @@ class ColorBlock(Static): class ColorApp(App): """Experiment with the command palette.""" - AUTO_FOCUS = None COMMANDS = App.COMMANDS | {ColorCommands} TITLE = "Press ctrl + \\ and type a color" From bbeccdea9f14a963af7ce41af7d6d4b0e08df3a6 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Wed, 17 Jul 2024 13:21:50 +0100 Subject: [PATCH 3/3] Use AUTO_FOCUS to focus the CommandInput in the CommandPalette --- src/textual/command.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/textual/command.py b/src/textual/command.py index 85507f5cf1..10b1fbbcf6 100644 --- a/src/textual/command.py +++ b/src/textual/command.py @@ -421,6 +421,8 @@ class CommandInput(Input): class CommandPalette(SystemModalScreen[CallbackType]): """The Textual command palette.""" + AUTO_FOCUS = "CommandInput" + COMPONENT_CLASSES: ClassVar[set[str]] = { "command-palette--help-text", "command-palette--highlight", @@ -625,9 +627,7 @@ def compose(self) -> ComposeResult: with Vertical(): with Horizontal(id="--input"): yield SearchIcon() - input = CommandInput(placeholder="Search for commands…") - input.focus() - yield input + yield CommandInput(placeholder="Search for commands…") if not self.run_on_select: yield Button("\u25b6") with Vertical(id="--results"):