Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Sep 27, 2024
1 parent 7008165 commit 5c5f2b7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
28 changes: 21 additions & 7 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2634,16 +2634,30 @@ 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")
def _pop_to_screen(self, screen: Screen) -> None:
"""Pop screens until the given screen is active.
async def pop_to(screen: Screen) -> None:
Args:
screen: desired active screen
Raises:
ValueError: If the screen doesn't exist in the stack.
"""
screens_to_pop: list[Screen] = []
for pop_screen in reversed(self.screen_stack):
if pop_screen is not screen:
screens_to_pop.append(pop_screen)
else:
break
else:
raise ScreenError("Screen {screen!r} not in screen stack")

async def pop_screens() -> None:
with self.batch_update():
while self.screen is not screen:
await self.pop_screen()
for screen in screens_to_pop:
await screen.dismiss()

self.call_later(pop_to, screen)
self.call_later(pop_screens)

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.
Expand Down
16 changes: 15 additions & 1 deletion src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,21 @@ def pre_await() -> None:
return await_pop

def pop_until_active(self) -> None:
self.app.pop_to_screen(self)
"""Pop any screens on top of this one, until this screen is active.
Raises:
ScreenError: If this screen is not in the current mode.
"""
from textual.app import ScreenError

try:
self.app._pop_to_screen(self)
except ScreenError:
# More specific error message
raise ScreenError(
f"Can't make {self} active as it is not in the current stack."
) from None

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.
Expand Down

0 comments on commit 5c5f2b7

Please sign in to comment.