Skip to content

Commit baf6309

Browse files
authored
Merge pull request #5522 from Textualize/fix-command-dismiss
fix dismiss
2 parents 7699aca + 5c9e4f4 commit baf6309

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/textual/command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ def _select_or_command(
12321232
self._cancel_gather_commands()
12331233
self.app.post_message(CommandPalette.Closed(option_selected=True))
12341234
self.dismiss()
1235-
self.call_later(self._selected_command.command)
1235+
self.app.call_later(self._selected_command.command)
12361236

12371237
@on(OptionList.OptionHighlighted)
12381238
def _stop_event_leak(self, event: OptionList.OptionHighlighted) -> None:

tests/snapshot_tests/test_snapshots.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2950,7 +2950,12 @@ def on_mount(self) -> None:
29502950
def on_resize(self) -> None:
29512951
self.add_class("narrow")
29522952

2953-
assert snap_compare(SCApp())
2953+
async def run_before(pilot: Pilot):
2954+
await pilot.pause()
2955+
await pilot.wait_for_animation()
2956+
await pilot.pause()
2957+
2958+
assert snap_compare(SCApp(), run_before=run_before)
29542959

29552960

29562961
def test_add_remove_tabs(snap_compare):

tests/test_command.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from __future__ import annotations
2+
3+
from typing import Iterable
4+
5+
from textual.app import App, ComposeResult, SystemCommand
6+
from textual.containers import Grid
7+
from textual.screen import ModalScreen, Screen
8+
from textual.widgets import Button, Label
9+
10+
11+
class QuitScreen(ModalScreen[bool]):
12+
"""Screen with a dialog to quit."""
13+
14+
def compose(self) -> ComposeResult:
15+
yield Grid(
16+
Label("Are you sure you want to quit?", id="question"),
17+
Button("Quit", variant="error", id="quit"),
18+
Button("Cancel", variant="primary", id="cancel"),
19+
id="dialog",
20+
)
21+
22+
def on_button_pressed(self, event: Button.Pressed) -> None:
23+
if event.button.id == "quit":
24+
self.dismiss(True)
25+
else:
26+
self.dismiss(False)
27+
28+
29+
class ModalApp(App):
30+
"""An app with a modal dialog."""
31+
32+
BINDINGS = [("q", "request_quit", "Quit")]
33+
34+
def __init__(self) -> None:
35+
self.check_quit_called = False
36+
super().__init__()
37+
38+
def get_system_commands(self, screen: Screen) -> Iterable[SystemCommand]:
39+
yield from super().get_system_commands(screen)
40+
yield SystemCommand(
41+
"try a modal quit dialog", "this should work", self.action_request_quit
42+
)
43+
44+
def action_request_quit(self) -> None:
45+
"""Action to display the quit dialog."""
46+
47+
def check_quit(quit: bool | None) -> None:
48+
"""Called when QuitScreen is dismissed."""
49+
self.check_quit_called = True
50+
51+
self.push_screen(QuitScreen(), check_quit)
52+
53+
54+
async def test_command_dismiss():
55+
"""Regression test for https://github.com/Textualize/textual/issues/5512"""
56+
app = ModalApp()
57+
58+
async with app.run_test() as pilot:
59+
await pilot.press("ctrl+p", *"modal quit", "enter")
60+
await pilot.pause()
61+
await pilot.press("enter")
62+
await pilot.pause()
63+
assert app.check_quit_called

0 commit comments

Comments
 (0)