From 700816562e65cbaf49b9ecff8043e4cf9a6d5608 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 27 Sep 2024 20:18:05 +0100 Subject: [PATCH] pop_to_screen --- src/textual/app.py | 11 +++++++++++ src/textual/screen.py | 3 +++ 2 files changed, 14 insertions(+) diff --git a/src/textual/app.py b/src/textual/app.py index 9a6267bc3f..bb9e6b2c77 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -2634,6 +2634,17 @@ async def do_pop() -> None: return AwaitComplete(do_pop()).call_next(self) + def pop_to_screen(self, screen: Screen) -> None: + if screen not in self.screen_stack: + raise ValueError("Screen {screen!r} not in screen stack") + + async def pop_to(screen: Screen) -> None: + with self.batch_update(): + while self.screen is not screen: + await self.pop_screen() + + self.call_later(pop_to, screen) + def set_focus(self, widget: Widget | None, scroll_visible: bool = True) -> None: """Focus (or unfocus) a widget. A focused widget will receive key events first. diff --git a/src/textual/screen.py b/src/textual/screen.py index 997d5c1c90..d29ea1ea33 100644 --- a/src/textual/screen.py +++ b/src/textual/screen.py @@ -1412,6 +1412,9 @@ def pre_await() -> None: return await_pop + def pop_until_active(self) -> None: + self.app.pop_to_screen(self) + async def action_dismiss(self, result: ScreenResultType | None = None) -> None: """A wrapper around [`dismiss`][textual.screen.Screen.dismiss] that can be called as an action.