Skip to content

Commit ce841f7

Browse files
authored
Merge pull request #1421 from davep/main
Allow `App.dark` to be set as early as `App.on_load`
2 parents 192e3f9 + 159f94a commit ce841f7

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [0.8.0] - Unreleased
99

10-
### Fixed
10+
### Fixed
1111

1212
- Fixed issues with nested auto dimensions https://github.com/Textualize/textual/issues/1402
1313
- Fixed watch method incorrectly running on first set when value hasn't changed and init=False https://github.com/Textualize/textual/pull/1367
14+
- `App.dark` can now be set from `App.on_load` without an error being raised https://github.com/Textualize/textual/issues/1369
1415

1516
### Added
1617

src/textual/app.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,14 @@ def watch_dark(self, dark: bool) -> None:
480480
"""Watches the dark bool."""
481481
self.set_class(dark, "-dark-mode")
482482
self.set_class(not dark, "-light-mode")
483-
self.refresh_css()
483+
try:
484+
self.refresh_css()
485+
except ScreenStackError:
486+
# It's possible that `dark` can be set before we have a default
487+
# screen, in an app's `on_load`, for example. So let's eat the
488+
# ScreenStackError -- the above styles will be handled once the
489+
# screen is spun up anyway.
490+
pass
484491

485492
def get_driver_class(self) -> Type[Driver]:
486493
"""Get a driver class for this platform.

tests/test_dark_toggle.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from textual.app import App
2+
3+
4+
class OnLoadDarkSwitch(App[None]):
5+
"""App for testing toggling dark mode in on_load."""
6+
7+
def on_load(self) -> None:
8+
self.dark = not self.dark
9+
10+
11+
async def test_toggle_dark_on_load() -> None:
12+
"""It should be possible to toggle dark mode in on_load."""
13+
async with OnLoadDarkSwitch().run_test() as pilot:
14+
assert not pilot.app.dark
15+
16+
17+
class OnMountDarkSwitch(App[None]):
18+
"""App for testing toggling dark mode in on_mount."""
19+
20+
def on_mount(self) -> None:
21+
self.dark = not self.dark
22+
23+
24+
async def test_toggle_dark_on_mount() -> None:
25+
"""It should be possible to toggle dark mode in on_mount."""
26+
async with OnMountDarkSwitch().run_test() as pilot:
27+
assert not pilot.app.dark
28+
29+
30+
class ActionDarkSwitch(App[None]):
31+
"""App for testing toggling dark mode from an action."""
32+
33+
BINDINGS = [("d", "toggle", "Toggle Dark Mode")]
34+
35+
def action_toggle(self) -> None:
36+
self.dark = not self.dark
37+
38+
39+
async def test_toggle_dark_in_action() -> None:
40+
"""It should be possible to toggle dark mode with an action."""
41+
async with OnMountDarkSwitch().run_test() as pilot:
42+
await pilot.press("d")
43+
await pilot.pause(2 / 100)
44+
assert not pilot.app.dark

0 commit comments

Comments
 (0)