File: test_command.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (62 lines) | stat: -rw-r--r-- 1,886 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import annotations

from typing import Iterable

from textual.app import App, ComposeResult, SystemCommand
from textual.containers import Grid
from textual.screen import ModalScreen, Screen
from textual.widgets import Button, Label


class QuitScreen(ModalScreen[bool]):
    """Screen with a dialog to quit."""

    def compose(self) -> ComposeResult:
        yield Grid(
            Label("Are you sure you want to quit?", id="question"),
            Button("Quit", variant="error", id="quit"),
            Button("Cancel", variant="primary", id="cancel"),
            id="dialog",
        )

    def on_button_pressed(self, event: Button.Pressed) -> None:
        if event.button.id == "quit":
            self.dismiss(True)
        else:
            self.dismiss(False)


class ModalApp(App):
    """An app with a modal dialog."""

    BINDINGS = [("q", "request_quit", "Quit")]

    def __init__(self) -> None:
        self.check_quit_called = False
        super().__init__()

    def get_system_commands(self, screen: Screen) -> Iterable[SystemCommand]:
        yield from super().get_system_commands(screen)
        yield SystemCommand(
            "try a modal quit dialog", "this should work", self.action_request_quit
        )

    def action_request_quit(self) -> None:
        """Action to display the quit dialog."""

        def check_quit(quit: bool | None) -> None:
            """Called when QuitScreen is dismissed."""
            self.check_quit_called = True

        self.push_screen(QuitScreen(), check_quit)


async def test_command_dismiss():
    """Regression test for https://github.com/Textualize/textual/issues/5512"""
    app = ModalApp()

    async with app.run_test() as pilot:
        await pilot.press("ctrl+p", *"modal quit", "enter")
        await pilot.pause()
        await pilot.press("enter")
        assert app.check_quit_called