Skip to content

Commit 752b4ae

Browse files
committed
fix dismiss
1 parent 7699aca commit 752b4ae

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
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/test_command.py

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

0 commit comments

Comments
 (0)