From be0c7636a1f32d5c95903065b991686bf7c8168c Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Wed, 21 Dec 2022 10:35:29 +0000 Subject: [PATCH] Allow App.dark to be set before there's a screen This commit addresses #1369 by swallowing any `ScreenStackError` when calling `refresh_css` after setting the -{dark,light}-mode class on the app. The idea here being that if there is no screen, that's fine, the style will be taken up once there is one. --- src/textual/app.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/textual/app.py b/src/textual/app.py index 1462eeb3ef..3578e2d751 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -480,7 +480,14 @@ def watch_dark(self, dark: bool) -> None: """Watches the dark bool.""" self.set_class(dark, "-dark-mode") self.set_class(not dark, "-light-mode") - self.refresh_css() + try: + self.refresh_css() + except ScreenStackError: + # It's possible that `dark` can be set before we have a default + # screen, in an app's `on_load`, for example. So let's eat the + # ScreenStackError -- the above styles will be handled once the + # screen is spun up anyway. + pass def get_driver_class(self) -> Type[Driver]: """Get a driver class for this platform.