diff --git a/src/textual/command.py b/src/textual/command.py index b389096e7b..1922c296ea 100644 --- a/src/textual/command.py +++ b/src/textual/command.py @@ -515,7 +515,7 @@ def _on_click(self, event: Click) -> None: method of dismissing the palette. """ if self.get_widget_at(event.screen_x, event.screen_y)[0] is self: - self.workers.cancel_all() + self._cancel_gather_commands() self.dismiss() def on_mount(self, _: Mount) -> None: @@ -774,7 +774,10 @@ def _refresh_command_list( _NO_MATCHES: Final[str] = "--no-matches" """The ID to give the disabled option that shows there were no matches.""" - @work(exclusive=True) + _GATHER_COMMANDS_GROUP: Final[str] = "--textual-command-palette-gather-commands" + """The group name of the command gathering worker.""" + + @work(exclusive=True, group=_GATHER_COMMANDS_GROUP) async def _gather_commands(self, search_value: str) -> None: """Gather up all of the commands that match the search value. @@ -895,6 +898,10 @@ async def _gather_commands(self, search_value: str) -> None: if command_list.option_count == 0 and not worker.is_cancelled: self._start_no_matches_countdown() + def _cancel_gather_commands(self) -> None: + """Cancel any operation that is gather commands.""" + self.workers.cancel_group(self, self._GATHER_COMMANDS_GROUP) + @on(Input.Changed) def _input(self, event: Input.Changed) -> None: """React to input in the command palette. @@ -903,7 +910,7 @@ def _input(self, event: Input.Changed) -> None: event: The input event. """ event.stop() - self.workers.cancel_all() + self._cancel_gather_commands() self._stop_no_matches_countdown() search_value = event.value.strip() @@ -921,7 +928,7 @@ def _select_command(self, event: OptionList.OptionSelected) -> None: event: The option selection event. """ event.stop() - self.workers.cancel_all() + self._cancel_gather_commands() input = self.query_one(CommandInput) with self.prevent(Input.Changed): assert isinstance(event.option, Command) @@ -958,7 +965,7 @@ def _select_or_command( if self._selected_command is not None: # ...we should return it to the parent screen and let it # decide what to do with it (hopefully it'll run it). - self.workers.cancel_all() + self._cancel_gather_commands() self.dismiss(self._selected_command.command) @on(OptionList.OptionHighlighted) @@ -971,7 +978,7 @@ def _action_escape(self) -> None: if self._list_visible: self._list_visible = False else: - self.workers.cancel_all() + self._cancel_gather_commands() self.dismiss() def _action_command_list(self, action: str) -> None: