Files
textual/tests/command_palette/test_run_on_select.py
Dave Pearson 51dc134a13 Isolate the command palette from app-based queries
This fixes #3633 by ensuring that if a query is made against the app while
the command palette is active, the query trickles down to the
previously-active screen rather than into the command palette modal screen.

This also updates the command palette unit tests to take this change into
account.
2023-11-06 13:57:44 +00:00

68 lines
2.3 KiB
Python

from functools import partial
from textual.app import App
from textual.command import CommandPalette, Hit, Hits, Provider
from textual.widgets import Input
class SimpleSource(Provider):
async def search(self, _: str) -> Hits:
def goes_nowhere_does_nothing(selection: int) -> None:
assert isinstance(self.app, CommandPaletteRunOnSelectApp)
self.app.selection = selection
for n in range(100):
yield Hit(
n + 1 / 100,
str(n),
partial(goes_nowhere_does_nothing, n),
str(n),
f"This is help for {n}",
)
class CommandPaletteRunOnSelectApp(App[None]):
COMMANDS = {SimpleSource}
def __init__(self) -> None:
super().__init__()
self.selection: int | None = None
async def test_with_run_on_select_on() -> None:
"""With run on select on, the callable should be instantly run."""
async with CommandPaletteRunOnSelectApp().run_test() as pilot:
save = CommandPalette.run_on_select
CommandPalette.run_on_select = True
assert isinstance(pilot.app, CommandPaletteRunOnSelectApp)
pilot.app.action_command_palette()
await pilot.press("0")
await pilot.app.screen.workers.wait_for_complete()
await pilot.press("down")
await pilot.press("enter")
assert pilot.app.selection is not None
CommandPalette.run_on_select = save
class CommandPaletteDoNotRunOnSelectApp(CommandPaletteRunOnSelectApp):
def __init__(self) -> None:
super().__init__()
async def test_with_run_on_select_off() -> None:
"""With run on select off, the callable should not be instantly run."""
async with CommandPaletteDoNotRunOnSelectApp().run_test() as pilot:
save = CommandPalette.run_on_select
CommandPalette.run_on_select = False
assert isinstance(pilot.app, CommandPaletteDoNotRunOnSelectApp)
pilot.app.action_command_palette()
await pilot.press("0")
await pilot.app.screen.workers.wait_for_complete()
await pilot.press("down")
await pilot.press("enter")
assert pilot.app.selection is None
assert pilot.app.screen.query_one(Input).value != ""
await pilot.press("enter")
assert pilot.app.selection is not None
CommandPalette.run_on_select = save