Skip to content

A screen that is pushed via a command palette callback does not receive the push_screen callback #5512

Closed
@gunslingerfry

Description

@gunslingerfry

Have you checked closed issues? Yes

Have you checked against the most recent version of Textual? Yes

Please give a brief but clear explanation of the issue. If you can, include a complete working example that demonstrates the bug. Check it can run without modifications.

A screen pushed inside a CommandPalette SystemCommand callback does not receive the dismiss result.

Here is a slightly tweaked version of the QuitScreen example where it also calls the same function from the command palette. You can see that the key binding still quits the app as normal. If you do it from the command palette it does not quit. Does not matter if it is Modal or not.

from textual.app import App, ComposeResult, SystemCommand
from textual.containers import Grid
from textual.screen import ModalScreen, Screen
from textual.widgets import Button, Footer, Header, 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."""

    # CSS_PATH = "modal01.tcss"
    BINDINGS = [("q", "request_quit", "Quit")]

    def compose(self) -> ComposeResult:
        yield Header()
        yield Footer()

    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."""
            if quit:
                self.exit()

        self.push_screen(QuitScreen(), check_quit)


if __name__ == "__main__":
    app = ModalApp()
    app.run()

Textual Diagnostics

Versions

Name Value
Textual 1.0.0
Rich 13.9.4

Python

Name Value
Version 3.13.0
Implementation CPython
Compiler GCC 14.2.1 20240910
Executable /home/matthew/.pyenv/versions/3.13.0/bin/python3.13

Operating System

Name Value
System Linux
Release 6.13.1-arch1-1
Version #1 SMP PREEMPT_DYNAMIC Sun, 02 Feb 2025 01:02:29 +0000

Terminal

Name Value
Terminal Application Unknown
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=263, height=48
legacy_windows False
min_width 1
max_width 263
is_terminal True
encoding utf-8
max_height 48
justify None
overflow None
no_wrap False
highlight None
markup None
height None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions