Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.8.0] - Unreleased

### Fixed
### Fixed

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

### Added

Expand Down
9 changes: 8 additions & 1 deletion src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 44 additions & 0 deletions tests/test_dark_toggle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from textual.app import App


class OnLoadDarkSwitch(App[None]):
"""App for testing toggling dark mode in on_load."""

def on_load(self) -> None:
self.dark = not self.dark


async def test_toggle_dark_on_load() -> None:
"""It should be possible to toggle dark mode in on_load."""
async with OnLoadDarkSwitch().run_test() as pilot:
assert not pilot.app.dark


class OnMountDarkSwitch(App[None]):
"""App for testing toggling dark mode in on_mount."""

def on_mount(self) -> None:
self.dark = not self.dark


async def test_toggle_dark_on_mount() -> None:
"""It should be possible to toggle dark mode in on_mount."""
async with OnMountDarkSwitch().run_test() as pilot:
assert not pilot.app.dark


class ActionDarkSwitch(App[None]):
"""App for testing toggling dark mode from an action."""

BINDINGS = [("d", "toggle", "Toggle Dark Mode")]

def action_toggle(self) -> None:
self.dark = not self.dark


async def test_toggle_dark_in_action() -> None:
"""It should be possible to toggle dark mode with an action."""
async with OnMountDarkSwitch().run_test() as pilot:
await pilot.press("d")
await pilot.pause(2 / 100)
assert not pilot.app.dark