Skip to content

pop_until_active does not pop screens #5166

Open
@jakubziebin

Description

@jakubziebin

Hello, consider the following MRE:

from __future__ import annotations

from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.screen import Screen
from textual.widgets import Footer, Label


class FirstScreen(Screen):
    BINDINGS = [
        Binding("n", "second_screen", "Second screen"),
    ]

    def compose(self) -> ComposeResult:
        yield Label("First screen. Press 'n' to go to the second screen.")
        yield Footer()

    def action_second_screen(self) -> None:
        self.app.push_screen(SecondScreen())


class SecondScreen(Screen):
    BINDINGS = [
        Binding("n", "third_screen", "Third screen"),
    ]

    def compose(self) -> ComposeResult:
        yield Label("Second screen. Press 'n' to go to the third screen.")
        yield Footer()

    async def action_third_screen(self) -> None:
        # going back to first screen and then replacing it with the third screen
        # self.app.get_screen works only with "installed" screens
        self.app.screen_stack[-1].pop_until_active()
        await self.app.switch_screen(ThirdScreen())


class ThirdScreen(Screen):
    def compose(self) -> ComposeResult:
        yield Label("Third screen.")
        yield Label(f"The screen stack looks like: {self.app.screen_stack}")
        yield Label("but should be: [Screen(id='_default'), ThirdScreen()]")


class MyApp(App):
    def on_mount(self) -> None:
        self.push_screen(FirstScreen())


MyApp().run()

version: 0.83.0

The problem is observed in situations where the next line must have the screen stack updated using the pop_until_active method.
In general, we are looking for a simple way to clear the screen stack. As it is read-only, we do not want to modify any protected attributes. It would be great to have a method to do this, it would be very helpful in applications with an onboarding process for example.

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