From 2b96bac3bf2e50c40fbebd28fce29c06b240773e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BBebrak?= Date: Mon, 16 Sep 2024 12:05:02 +0200 Subject: [PATCH] Remove the need of overriding textual screen changing methods --- clive/__private/ui/app.py | 50 ++---------------------------- clive/__private/ui/clive_screen.py | 3 +- 2 files changed, 4 insertions(+), 49 deletions(-) diff --git a/clive/__private/ui/app.py b/clive/__private/ui/app.py index d34397c95..a6dd3a8ec 100644 --- a/clive/__private/ui/app.py +++ b/clive/__private/ui/app.py @@ -4,7 +4,7 @@ import math import traceback from contextlib import asynccontextmanager, contextmanager -from typing import TYPE_CHECKING, Any, TypeVar, cast, overload +from typing import TYPE_CHECKING, Any, TypeVar, cast from textual import on, work from textual._context import active_app @@ -29,11 +29,9 @@ if TYPE_CHECKING: from collections.abc import AsyncGenerator, Callable, Iterator - from typing import Literal from textual.message import Message - from textual.screen import Screen, ScreenResultCallbackType, ScreenResultType - from textual.widget import AwaitMount + from textual.screen import Screen, ScreenResultType UpdateScreenResultT = TypeVar("UpdateScreenResultT") @@ -196,35 +194,6 @@ def __should_enter_onboarding() -> bool: else: self.push_screen(Dashboard()) - @overload - def push_screen( - self, - screen: Screen[ScreenResultType] | str, - callback: ScreenResultCallbackType[ScreenResultType] | None = None, - wait_for_dismiss: Literal[False] = False, # noqa: FBT002 - ) -> AwaitMount: ... - - @overload - def push_screen( - self, - screen: Screen[ScreenResultType] | str, - callback: ScreenResultCallbackType[ScreenResultType] | None = None, - wait_for_dismiss: Literal[True] = True, # noqa: FBT002 - ) -> asyncio.Future[ScreenResultType]: ... - - def push_screen( - self, - screen: Screen[ScreenResultType] | str, - callback: ScreenResultCallbackType[ScreenResultType] | None = None, - wait_for_dismiss: bool = False, # noqa: FBT001, FBT002 - ) -> AwaitMount | asyncio.Future[ScreenResultType]: - fun = super().push_screen - return self.__update_screen(lambda: fun(screen=screen, callback=callback, wait_for_dismiss=wait_for_dismiss)) # type: ignore[no-any-return, call-overload] - - def pop_screen(self) -> AwaitComplete: - fun = super().pop_screen - return self.__update_screen(lambda: fun()) - def pop_screen_until(self, *screens: str | type[Screen[ScreenResultType]]) -> AwaitComplete: """ Pop all screens until one of the given screen is on top of the stack. @@ -252,21 +221,6 @@ async def _pop_screen_until() -> None: return AwaitComplete(_pop_screen_until()).call_next(self) - def switch_screen(self, screen: Screen[ScreenResultType] | str) -> AwaitComplete: - fun = super().switch_screen - return self.__update_screen(lambda: fun(screen)) - - def __update_screen(self, callback: Callable[[], UpdateScreenResultT]) -> UpdateScreenResultT: - """ - Auxiliary function to override the default push_screen, switch_screen and pop_screen methods. - - Because of Textual's event ScreenResume not being bubbled up, we can't easily hook on it via - `def on_screen_resume` so we have to override the push_screen, switch_screen and pop_screen methods. - """ - reply = callback() - self.title = f"{self.__class__.__name__} ({self.screen.__class__.__name__})" - return reply - def action_help(self) -> None: self.push_screen(Help()) diff --git a/clive/__private/ui/clive_screen.py b/clive/__private/ui/clive_screen.py index 95f67549c..5f8ddf2e7 100644 --- a/clive/__private/ui/clive_screen.py +++ b/clive/__private/ui/clive_screen.py @@ -128,8 +128,9 @@ def _post_suspended(self) -> None: self._post_to_children(self, self.Suspended()) @on(ScreenResume) - def _post_resumed(self) -> None: + def _handle_screen_resume(self) -> None: """Look in the docstring of _post_suspended.""" + self.app.title = f"Clive ({self.__class__.__name__})" self._post_to_children(self, self.Resumed()) def _post_to_children(self, node: Widget, message: Message) -> None: