Skip to content

Commit

Permalink
Ensure that the command palette only cancels its own workers
Browse files Browse the repository at this point in the history
See #3615.

Co-authored-by: Rodrigo Girão Serrão <[email protected]>
  • Loading branch information
davep and rodrigogiraoserrao committed Oct 31, 2023
1 parent 7cbba66 commit 449d3a6
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/textual/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit 449d3a6

Please sign in to comment.