Skip to content

fix dismiss #5522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/textual/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ def _select_or_command(
self._cancel_gather_commands()
self.app.post_message(CommandPalette.Closed(option_selected=True))
self.dismiss()
self.call_later(self._selected_command.command)
self.app.call_later(self._selected_command.command)

@on(OptionList.OptionHighlighted)
def _stop_event_leak(self, event: OptionList.OptionHighlighted) -> None:
Expand Down
7 changes: 6 additions & 1 deletion tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2950,7 +2950,12 @@ def on_mount(self) -> None:
def on_resize(self) -> None:
self.add_class("narrow")

assert snap_compare(SCApp())
async def run_before(pilot: Pilot):
await pilot.pause()
await pilot.wait_for_animation()
await pilot.pause()

assert snap_compare(SCApp(), run_before=run_before)


def test_add_remove_tabs(snap_compare):
Expand Down
63 changes: 63 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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")
await pilot.pause()
assert app.check_quit_called
Loading