|
| 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